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
| void WINAPI cConnection::getMessages( cConnection* connection )
{
HANDLE eventsArray[2];
bool stopThread = false;
DWORD result;
WSANETWORKEVENTS info;
while( !stopThread )
{
eventsArray[0] = connection->m_hStopThread;
eventsArray[1] = connection->m_event;
// wait for an event
result = WSAWaitForMultipleEvents( 2, eventsArray, FALSE, WSA_INFINITE, FALSE );
switch(result)
{
// m_hStopThread -> we finish the thread
case WSA_WAIT_EVENT_0 :
stopThread = true;
break;
// message from the server
case WSA_WAIT_EVENT_0+1 :
// get information about this event
WSAEnumNetworkEvents( connection->m_socket, connection->m_event, &info );
switch(info.lNetworkEvents)
{
// server connection closed
case FD_CLOSE:
// server close !
WaitForSingleObject( connection->m_hMutex, INFINITE );
connection->m_connected = false;
ReleaseMutex( connection->m_hMutex );
break;
case FD_READ:
char* buffer;
void* data;
DWORD bufferSize;
int dwordSize = sizeof(bufferSize);
// get the size of the buffer
getsockopt( connection->m_socket, SOL_SOCKET, SO_RCVBUF, (char*)&(bufferSize), &dwordSize );
buffer = new char[bufferSize];
if( (bufferSize = recv( connection->m_socket, buffer, bufferSize, 0)) == SOCKET_ERROR )
break; // TODO -> managing the error
// copy the data
data = new char[bufferSize];
memcpy( data, buffer, bufferSize);
delete [] buffer;
WaitForSingleObject( connection->m_hMutex, INFINITE );
connection->processMessage( data, bufferSize );
ReleaseMutex( connection->m_hMutex );
delete [] data;
break;
} // switch(info.lNetworkEvents)
break; // case WSA_WAIT_EVENT_0+1
} // switch(result)
} // while( !stopThread )
} |
Partager