o: ActiveSupport::Cache::Entry :@compressedF:@value"�;"‰;
| mlab_xvcd.cpp | ||
|---|---|---|
| 12 | 12 |
// 1.01 2012_09 Added parameter for device selection |
| 13 | 13 |
// 1.02 2012_12 Error handling and debugged |
| 14 | 14 |
// 1.03 2012_12 Release version ready to publish |
| 15 |
// 1.04 2013_04 Socket Bind Error with eplanation (multiple instance of XVC Server) |
|
| 15 |
// 1.04 2013_04 Socket Bind Error with explanation (multiple instance of XVC Server) |
|
| 16 |
// 1.05 2013-04 Test FTDI cable during wait for Accept (to stop the server immediately when cable is disconnected) |
|
| 16 | 17 |
// |
| 17 | 18 |
// |
| 18 | 19 |
// Purpose: |
| ... | ... | |
| 58 | 59 |
// Ask Google about Xilinx Virtual Cable. |
| 59 | 60 |
// |
| 60 | 61 |
// |
| 61 |
// Translation:
|
|
| 62 |
// Compilation:
|
|
| 62 | 63 |
// |
| 63 | 64 |
// MS Visual C++ 2010 Express (free, registration required) |
| 64 | 65 |
// Create new empty project for Win32 Console Application and name project mlab_xvcd (to build mlab_xvcd.exe) |
| ... | ... | |
| 469 | 470 |
{
|
| 470 | 471 |
int LastError=WSAGetLastError(); |
| 471 | 472 |
fprintf(stderr, "Bind failed with error: %d\n", LastError); |
| 472 |
if (LastError==10048) fprintf(stderr, "Trying to start second instance of XVC Server?\n");
|
|
| 473 |
if (LastError==WSAEADDRINUSE) fprintf(stderr, "Trying to start second instance of XVC Server?\n");
|
|
| 473 | 474 |
freeaddrinfo(result); |
| 474 | 475 |
closesocket(ListenSocket); |
| 475 | 476 |
WSACleanup(); |
| ... | ... | |
| 505 | 506 |
printf(" Listen\n");
|
| 506 | 507 |
jtagSetLED(true); |
| 507 | 508 |
|
| 508 |
// Accept a client SOCKET |
|
| 509 |
// Set ListenSocket to non-blocking mode |
|
| 510 |
// We need during waiting for Accept to detect FTDI disconnect |
|
| 511 |
u_long iMode = 1; |
|
| 512 |
iResult = ioctlsocket(ListenSocket, FIONBIO, &iMode); |
|
| 513 |
if (iResult != NO_ERROR) |
|
| 514 |
{
|
|
| 515 |
fprintf(stderr, "ioctlsocket failed with error: %ld\n", iResult); |
|
| 516 |
WSACleanup(); |
|
| 517 |
jtagClosePort(); |
|
| 518 |
return -2; |
|
| 519 |
} |
|
| 520 |
|
|
| 521 |
// Accept a client SOCKET (wait for Accept) |
|
| 509 | 522 |
sockaddr ClientSocetAddr; |
| 510 | 523 |
int ClientSocetAddrLen = sizeof(sockaddr); |
| 511 |
ClientSocket = accept(ListenSocket, &ClientSocetAddr, &ClientSocetAddrLen); |
|
| 512 |
if (ClientSocket == INVALID_SOCKET) |
|
| 524 |
do |
|
| 513 | 525 |
{
|
| 514 |
fprintf(stderr, "accept failed with error: %d\n", WSAGetLastError()); |
|
| 515 |
closesocket(ListenSocket); |
|
| 526 |
// Try Accept (non-blocking) |
|
| 527 |
ClientSocket = accept(ListenSocket, &ClientSocetAddr, &ClientSocetAddrLen); |
|
| 528 |
if (ClientSocket == INVALID_SOCKET) |
|
| 529 |
{
|
|
| 530 |
// Accept Error |
|
| 531 |
if (WSAGetLastError() != WSAEWOULDBLOCK) |
|
| 532 |
{
|
|
| 533 |
fprintf(stderr, "accept failed with error: %d\n", WSAGetLastError()); |
|
| 534 |
closesocket(ListenSocket); |
|
| 535 |
WSACleanup(); |
|
| 536 |
jtagClosePort(); |
|
| 537 |
return -2; |
|
| 538 |
} |
|
| 539 |
// Not yet Accepted |
|
| 540 |
{
|
|
| 541 |
// Check FTDI |
|
| 542 |
if (!CheckCable()) |
|
| 543 |
{
|
|
| 544 |
fprintf(stderr, "XVC Cable unexpectadly disconnected\n"); |
|
| 545 |
closesocket(ListenSocket); |
|
| 546 |
WSACleanup(); |
|
| 547 |
jtagClosePort(); |
|
| 548 |
return -2; |
|
| 549 |
} |
|
| 550 |
// Sleep some time (do not eat CPU time for nothong) |
|
| 551 |
//nanosleep(); |
|
| 552 |
Sleep(100); //ms |
|
| 553 |
} |
|
| 554 |
} |
|
| 555 |
} |
|
| 556 |
while (ClientSocket == INVALID_SOCKET); |
|
| 557 |
|
|
| 558 |
// Set (Accepted) Socket to blocking mode |
|
| 559 |
iMode = 0; |
|
| 560 |
iResult = ioctlsocket(ClientSocket, FIONBIO, &iMode); |
|
| 561 |
if (iResult != NO_ERROR) |
|
| 562 |
{
|
|
| 563 |
fprintf(stderr, "ioctlsocket failed with error: %ld\n", iResult); |
|
| 516 | 564 |
WSACleanup(); |
| 517 | 565 |
jtagClosePort(); |
| 518 | 566 |
return -2; |