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
|
case FD_READ:
{
appendTextToEdit(hDebug, "FD_READ event\n");
int bytes_recv = recv(Socket, readBuffer, sizeof(readBuffer), 0); // char readBuffer[1024]
readQueueMutex.lock();
readQueue.push(readBuffer);
readQueueMutex.unlock();
char bytes[256];
_itoa_s(bytes_recv, bytes, 10);
appendTextToEdit(hDebug, "From FD_READ : bytes reveived : \n");
appendTextToEdit(hDebug, bytes);
appendTextToEdit(hDebug, "\n");
appendTextToEdit(hDebug, "End of FD_READ\n");
}
case FD_CONNECT: // Thread is launched here to run forever
{
appendTextToEdit(hDebug, "FD_CONNECT event\n");
connectStatus = TRUE;
statusText=TEXT("Connected");
HANDLE hIncDataThread = (HANDLE)_beginthread(incDataProcess, 0, 0); // Launch incoming data process thread
appendTextToEdit(hDebug, "From FD_CONNECT : hIncDataThread launched\n");
appendTextToEdit(hDebug, "End of FD_CONNECT\n");
}
void incDataProcess(void *pBuffer)
{
appendTextToEdit(hDebug, "From hIncDataThread : starting thread...\n");
for(;;)
{
appendTextToEdit(hDebug, "FOR\n");
while (!readQueue.empty())
{
appendTextToEdit(hDebug, "WHILE !QUEUE.EMPTY\n");
readQueueMutex.lock();
string sReadBuffer = readQueue.front(); // take the next queue element
char *debugBuffer = _strdup(sReadBuffer.c_str());
readQueue.pop(); // removes this element from the queue
readQueueMutex.unlock();
string var;
char *pVar = nullptr;
char *next_token = nullptr;
appendTextToEdit(hDebug, "From hIncDataThread : processing : ");
appendTextToEdit(hDebug, debugBuffer);
appendTextToEdit(hDebug, "\n");
istringstream iss(sReadBuffer); // Put into a stream
while (getline(iss, var)) // Default delimiter '\n'
{
pVar = _strdup(var.c_str()); // Cast string to char *
if(strstr(pVar, "id=") != NULL)
{
char *label = strtok_s(pVar, "=", &next_token);
char *pId = strtok_s(NULL, "\n", &next_token);
strcpy_s(id, pId);
}
if( strstr(pVar, "version") != NULL)
{
char *label = strtok_s(pVar, "=", &next_token);
char *pVersion = strtok_s(NULL, "\n", &next_token);
strcpy_s(version, pVersion);
}
if( strstr(pVar, "Qh57=") != NULL)
{
char *label = strtok_s(pVar, "=", &next_token);
char *pFoFd = strtok_s(NULL, "\n", &next_token);
strcpy_s(foFd, pFoFd);
appendTextToEdit(hDebug, "Qh57=");
appendTextToEdit(hDebug, foFd);
appendTextToEdit(hDebug, "\n");
}
} //while getline
} // while queue is not empty
} // infinite for loop
appendTextToEdit(hDebug, "From hIncDataThread : closing thread\n"); // Should never be printed
}
|