1

我是 IOCP 的新手,过去几周一直在为此苦苦挣扎。我在下面粘贴了与 IOCP 相关的代码的一些核心部分。这可能无法完美执行,因为我剪掉了一些部分以使其易于理解。我在接收数据时很挣扎。当涉及到工作线程中的 wsarecv 时,wsarecv 返回 WSA_IO_PENDING 错误代码,所以我调用 WSAGetOverlappedResult 来检查要完成的操作。Twist 来到这里,而是在 WSAGetOverlappedResult 之后继续并调用我的本地函数 ProcessTelegramData ,代码的相同部分(再次调用 wsarecv)由另一个尝试调用 ProcessTelegramData 的工作线程再次执行,并且缓冲区值在其中无效。我无法理解为什么另一个线程在调用 WSAGetOverlappedResult 时再次调用 wsarecv 以及为什么缓冲区值变得无效?

unsigned LicTCPServer::WorkerThread(LPVOID lpParam)
{
    //int nThreadNo = (int)lpParam;
    LicTCPServer* pThis = reinterpret_cast<LicTCPServer*>(lpParam);
    void *lpContext = NULL;
    OVERLAPPED       *pOverlapped = NULL;
    CClientContext   *pClientContext = NULL;
    DWORD            dwBytesTransfered = 0;
    int nBytesRecv = 0;
    int nBytesSent = 0;
    DWORD             dwBytes = 0, dwFlags = 0;

    //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.
            pThis->RemoveFromClientListAndFreeMemory(pClientContext);
            continue;
        }

        WSABUF *p_wbuf = pClientContext->GetWSABUFPtr();
        OVERLAPPED *p_ol = pClientContext->GetOVERLAPPEDPtr();
        //printf("reached %d\n",pClientContext->GetOpCode());
        printf("Get Queued received\n");

        switch (pClientContext->GetOpCode())
        {
        case OP_READ:
            {

                //Once the data is successfully received, we will print it.
                //pClientContext->SetOpCode(OP_WRITE);
                pClientContext->ResetWSABUF();

                dwFlags = 0;
                //int a = recv(pClientContext->GetSocket(), p_wbuf->buf, p_wbuf->len, 0);
                //Get the data.

                if(WSARecv(pClientContext->GetSocket(), p_wbuf, 1, &dwBytes, &dwFlags, p_ol, NULL) == SOCKET_ERROR)
                {
                    if (WSAGetLastError() != WSA_IO_PENDING)
                    {
                        printf("Error occured at WSARecv()\n");
                        return 0;
                    }

                }
                DWORD byteTr = 0;

                WSAGetOverlappedResult(pClientContext->GetSocket(),p_ol,&byteTr,TRUE,&dwFlags);

                if( byteTr > 0 )
                {
                    //doing some operatin on data received
                    printf("Process tele called\n");
                    g_pLicServFunc->ProcessTelegramData(pClientContext->GetSocket(), p_wbuf->buf, byteTr);
                }

                if ((SOCKET_ERROR == nBytesRecv) && (WSA_IO_PENDING != WSAGetLastError()))
                {
                    //WriteToConsole("\nThread %d: Error occurred while executing WSARecv().", nThreadNo);

                    //Let's not work with this client
                    //TBC
                    //RemoveFromClientListAndFreeMemory(pClientContext);
                }
            }

            break;

        case OP_WRITE:

            char szBuffer[MAX_BUFFER_LEN];
            //Send the message back to the client.
            pClientContext->SetOpCode(OP_READ);


            pClientContext->SetTotalBytes(dwBytesTransfered);
            pClientContext->SetSentBytes(0);

            //p_wbuf->len  = dwBytesTransfered;

            dwFlags = 0;
            DWORD temp;
            //Overlapped send
            printf("reached Going to send\n");
            //send(pClientContext->GetSocket(), p_wbuf->buf,p_wbuf->len, 0); 
            nBytesSent = WSASend(pClientContext->GetSocket(), p_wbuf, 1, 
                &temp, dwFlags, p_ol, NULL);

            if ((SOCKET_ERROR == nBytesSent) && (WSA_IO_PENDING != WSAGetLastError()))
            {
                //WriteToConsole("\nThread %d: Error occurred while executing WSASend().", nThreadNo);

                //Let's not work with this client
                //TBC
                //RemoveFromClientListAndFreeMemory(pClientContext);
            }

            break;

        default:
            printf("reached to default\n");
            //We should never be reaching here, under normal circumstances.
            break;
        } // switch
    } // while

    return 0;
}
4

1 回答 1

0

我有一个与 WSARecv 类似的问题,它总是排队到 io 完成队列,即使它立即成功。我不得不忽略指示成功的返回值,而是处理从 GetQueuedCompletionStatus 获得的排队结果。

于 2012-06-05T18:57:39.530 回答