Problem with wsarecv while using with IOCP
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Problem with wsarecv while using with IOCP

  1. #1
    Join Date
    Sep 2011
    Posts
    3

    Problem with wsarecv while using with IOCP

    Hello All
    I am new to IOCP and struggling with this for last few weeks. I have pasted some core part of my code below related to IOCP.This may not be executed perfectly as I clipped some part to make it easy to understand. I am struggling while receiving the data.As it comes to wsarecv in worket thread, wsarecv returns WSA_IO_PENDING error code so I call WSAGetOverlappedResult to check operation to be completed. Twist comes here, rather it proceed and call my local function ProcessTelegramData after WSAGetOverlappedResult , same part of code(wsarecv called again) is executed again by another worker thread which try to call ProcessTelegramData and buffer value is invalid in it. I am unable to understand why another thread calling wsarecv again when WSAGetOverlappedResult is called and why buffer value is getting invalidated?


    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 &#37;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;
    }
    Last edited by nipungupta11; September 7th, 2011 at 10:20 AM. Reason: Code pasted was too long.

  2. #2
    Join Date
    Sep 2011
    Posts
    3

    Re: Problem with wsarecv while using with IOCP

    Hello
    Last edited by nipungupta11; September 7th, 2011 at 10:21 AM. Reason: Was able to edit original post, so removed.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center