64,683
社区成员
发帖
与我相关
我的任务
分享
unsigned int CSClient::WorkThreadFunction(void *pV)
{
int nError = 0;
CSClient* pThis = (CSClient*)pV;
while(1)
{
DWORD dwEvent = 0;
// Wait for one of the sockets to receive I/O notification and
if ((dwEvent = WSAWaitForMultipleEvents(1,&pThis->m_Event, FALSE,WSA_INFINITE, FALSE)) == WSA_WAIT_FAILED)
{
printf("WSAWaitForMultipleEvents failed with error %d\n", WSAGetLastError());
closesocket(pThis->m_Sock);
pThis->m_Sock = INVALID_SOCKET;
_endthreadex(0);
return 0;
}
if(WSA_WAIT_TIMEOUT == dwEvent)
{
}
else
{
WSANETWORKEVENTS NetworkEvents;
dwEvent = dwEvent - WAIT_OBJECT_0;
if (WSAEnumNetworkEvents(pThis->m_Sock, pThis->m_Event, &NetworkEvents) == SOCKET_ERROR)
{
printf("WSAEnumNetworkEvents failed with error %d\n", WSAGetLastError());
return 0;
}
if (NetworkEvents.lNetworkEvents & FD_READ)
{
if (NetworkEvents.iErrorCode[FD_READ_BIT] != 0)
{
printf("FD_READ failed with error %d\n", NetworkEvents.iErrorCode[FD_READ_BIT]);
break;
}
DWORD dwFlags = 0;
DWORD dwRecvBytes = 0;
WSABUF WSABuf;
WSABuf.len = DATA_BUFSIZE;
WSABuf.buf = (char*)(pThis->m_RingBuf.GetBuffer() + pThis->m_RingBuf.GetwritePos());
//dwFlags is nessary
if(WSARecv(pThis->m_Sock,&WSABuf,1,&dwRecvBytes,&dwFlags,NULL,NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
printf("WSASend() failed with error %d\n", WSAGetLastError());
}
}
pThis->m_LockBuffer.Enter();
pThis->m_RingBuf.Write((BYTE*)WSABuf.buf,dwRecvBytes);
pThis->GetMsg();
pThis->m_LockBuffer.Leave();
}
else if(NetworkEvents.lNetworkEvents & FD_CLOSE)
{
if (NetworkEvents.iErrorCode[FD_CLOSE_BIT] != 0)
{
printf("FD_CLOSE failed with error %d\n", NetworkEvents.iErrorCode[FD_CLOSE_BIT]);
break;
}
closesocket(pThis->m_Sock);
WSACleanup();
}
}
}
return 0;
}
unsigned int CSClient::WorkThreadFunction(void *pV)
{
int nError = 0;
CSClient* pThis = (CSClient*)pV;
while(1)
{
DWORD dwEvent = 0;
// Wait for one of the sockets to receive I/O notification and
if ((dwEvent = WSAWaitForMultipleEvents(1,&pThis->m_Event, FALSE,WSA_INFINITE, FALSE)) == WSA_WAIT_FAILED)
{
printf("WSAWaitForMultipleEvents failed with error %d\n", WSAGetLastError());
closesocket(pThis->m_Sock);
pThis->m_Sock = INVALID_SOCKET;
_endthreadex(0);
return 0;
}
if(WSA_WAIT_TIMEOUT == dwEvent)
{
}
else
{
WSANETWORKEVENTS NetworkEvents;
dwEvent = dwEvent - WAIT_OBJECT_0;
if (WSAEnumNetworkEvents(pThis->m_Sock, pThis->m_Event, &NetworkEvents) == SOCKET_ERROR)
{
printf("WSAEnumNetworkEvents failed with error %d\n", WSAGetLastError());
return 0;
}
if (NetworkEvents.lNetworkEvents & FD_READ)
{
if (NetworkEvents.iErrorCode[FD_READ_BIT] != 0)
{
printf("FD_READ failed with error %d\n", NetworkEvents.iErrorCode[FD_READ_BIT]);
break;
}
DWORD dwFlags = 0;
DWORD dwRecvBytes = 0;
WSABUF WSABuf;
WSABuf.len = DATA_BUFSIZE;
PBUFFER pBuffer = new BUFFER();
if(pBuffer)
{
WSABuf.buf = (char*)(pBuffer + 1);
if(WSARecv(pThis->m_Sock,&WSABuf,1,&dwRecvBytes,&dwFlags,NULL,NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
printf("WSASend() failed with error %d\n", WSAGetLastError());
}
}
}
}
else if(NetworkEvents.lNetworkEvents & FD_CLOSE)
{
if (NetworkEvents.iErrorCode[FD_CLOSE_BIT] != 0)
{
printf("FD_CLOSE failed with error %d\n", NetworkEvents.iErrorCode[FD_CLOSE_BIT]);
break;
}
closesocket(pThis->m_Sock);
WSACleanup();
}
}
}
return 0;
}