1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| typedef struct
{
OVERLAPPED oOverlap;
HANDLE hPipeInst;
TCHAR chRequest[BUFSIZE];
DWORD cbRead;
TCHAR chReply[BUFSIZE];
DWORD cbToWrite;
} PIPEINST, *LPPIPEINST;
HANDLE hPipe;
BOOL fSuccess, fPendingIO;
LPPIPEINST lpPipeInst;
DWORD dwWait, cbRet;
OVERLAPPED oConnect;
extern "C" EXPORT void cpaServerCommStart(cpaServerCommLibHandleT cpaServerCommLibHandle)
{
// struct timeval timeout;
cpaMessageHeaderT * mess=NULL;
// bool HandleExist;
oConnect.hEvent = CreateEvent( NULL, TRUE, TRUE, NULL);
if (oConnect.hEvent == NULL)
{
printf("CreateEvent failed with %d.\n", GetLastError());
char KTxtCountServer[] = "CPAServer";
printf ("CPAServer");
exit(-1);
}
fPendingIO = CreateAndConnectInstance(&oConnect);
printf ("Server Connected.\n");
while (1)
{
//timeout.tv_sec = 60;
//timeout.tv_usec = 0;
dwWait = WaitForSingleObjectEx( oConnect.hEvent, INFINITE, TRUE);
switch (dwWait)
{
// The wait conditions are satisfied by a completed connect
// operation.
case 0:
// If an operation is pending, get the result of the
// connect operation.
if (fPendingIO)
{
fSuccess = GetOverlappedResult(hPipe, &oConnect, &cbRet, FALSE);
if (!fSuccess)
{
printf("ConnectNamedPipe (%d)\n", GetLastError());
exit(-1);
// return 0;
}
}
// Allocate storage for this instance.
lpPipeInst = (LPPIPEINST) GlobalAlloc(GPTR, sizeof(PIPEINST));
if (lpPipeInst == NULL)
{
printf("GlobalAlloc failed (%d)\n", GetLastError());
exit(-1);
// return 0;
}
lpPipeInst->hPipeInst = hPipe;
lpPipeInst->cbToWrite = 0;
mess = cpaCommReadMessage(lpPipeInst->hPipeInst);
if (mess!=NULL){
printf("\nserver - Pipe Handle : %p \n", lpPipeInst->hPipeInst);
if(lib_handle.cpaClientConnectCB)
lib_handle.cpaClientConnectCB((cpaServerConnHandleT)lpPipeInst->hPipeInst);
if(lib_handle.cpaServerHandleMsgCB)
lib_handle.cpaServerHandleMsgCB((cpaServerConnHandleT)lpPipeInst->hPipeInst,mess);
}
// Create new pipe instance for the next client.
fPendingIO = CreateAndConnectInstance( &oConnect);
break;
// The wait is satisfied by a completed read or write
// operation. This allows the system to execute the
// completion routine.
case WAIT_IO_COMPLETION: break;
// An error occurred in the wait function.
default:
{
printf("WaitForSingleObjectEx (%d)\n", GetLastError());
// return 0;
}
}
}
} |
Partager