none
I/O Completion ports without Threads RRS feed

  • Question

  • The question is about how to use just one while loop to handle all sockets requests inside the main program thread. This is possible using WSAEventSelect and WSAWaitForMultipleEvents but we are here restricted to 64 Events. I could also use WSAAsyncSelect but I would need a window to handle socket messages.

    What I do not find is how to make the listening socket also work asynchronously with I/O Completion. The function GetQueuedCompletionStatus seems to be intended to be used with data transmission but not to handle incoming connecting request.

    My idea would be the following:

    CompletionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 1)
    Listen = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED)
    bind(Listen)
    listen(Listen)
    myData[index]->s = Listen;
    CreateIoCompletionPort((HANDLE)Listen, CompletionPort, &myData[index], 1)
    index++;
    
    while(TRUE) {
       GetQueuedCompletionStatus(CompletionPort, &BytesTransferred, (LPDWORD)&PerHandleData, (LPOVERLAPPED *)&PerIoData, INFINITE)
       if(PerHandleData->s == Listen) 
           AcceptReq = new socket;
           myData[index]->s = Listen;
           CreateIoCompletionPort((HANDLE)AcceptReq, CompletionPort, &myData[index], 1)
           WSARecv(AcceptReq)
           index++;
       }
       else {
           // Process received/transmitted data
       }
    }
    

    Is that possible or is there another way to achieve that?

    Friday, March 3, 2017 3:41 PM