//Worker thread will be around to process requests, until a Shutdown event is not Signaled.
while (WAIT_OBJECT_0 != WaitForSingleObject(g_hShutdownEvent, 0))
{
BOOL bReturn = GetQueuedCompletionStatus(
g_hIOCompletionPort,
&dwBytesTransfered,
(LPDWORD)&lpContext,
&pOverlapped,
INFINITE);
if (NULL == lpContext)
{
//We are shutting down
break;
}
//Get the client context
pClientContext = (CClientContext *)lpContext;
if ((FALSE == bReturn) || ((TRUE == bReturn) && (0 == dwBytesTransfered)))
{
//Client connection gone, remove it.
RemoveFromClientListAndFreeMemory(pClientContext);
continue;
}
WSABUF *p_wbuf = pClientContext->GetWSABUFPtr();
OVERLAPPED *p_ol = pClientContext->GetOVERLAPPEDPtr();
switch (pClientContext->GetOpCode())
{
case OP_READ: //#define 0,定义为0,
pClientContext->IncrSentBytes(dwBytesTransfered);
//Write operation was finished, see if all the data was sent.
//Else post another write.
if(pClientContext->GetSentBytes() < pClientContext->GetTotalBytes())
{
pClientContext->SetOpCode(OP_READ);
if ((SOCKET_ERROR == nBytesSent) && (WSA_IO_PENDING != WSAGetLastError()))
{
//Let's not work with this client
RemoveFromClientListAndFreeMemory(pClientContext);
}
}
else //上面是因为没有发送完,这儿已经发送完成了,为什么要投递WSARecv呢,没看明白
{
//Once the data is successfully received, we will print it.
pClientContext->SetOpCode(OP_WRITE);
pClientContext->ResetWSABUF();