0

I'm trying to develop req/res server by using overlapped io according to this example.
The problem is that in the same time there could be multiple sends to the same socket and I can't pass overlapped structure to the WSAGetOverlappedResult to manage send event properly. The main loop looks like:

while (TRUE) {
    index = WSAWaitForMultipleEvents(EventTotal, EventArray, FALSE, WSA_INFINITE, TRUE);

    if (index == WSA_WAIT_FAILED) {
        printf("Wait failed w/err %d\n", WSAGetLastError());
        continue;
    }

    index -= WSA_WAIT_EVENT_0;
    if (!WSAEnumNetworkEvents(SocketArray[index]->Socket, EventArray[index], &NetworkEvents)) { // Multiple events max exist
        if (NetworkEvents.lNetworkEvents & FD_ACCEPT)
            ManageAccept(index, NetworkEvents);

        if (NetworkEvents.lNetworkEvents & FD_READ)
            ManageRead(index, NetworkEvents);

        if (NetworkEvents.lNetworkEvents & FD_WRITE)
            ManageWrite(index, NetworkEvents);
    }
}

Then I send messages from method ManageRead to the client socket with

WSASend(SocketArray[index]->Socket, &(over->wsabuf), 1, &SendBytes, 0, over, NULL)

And when overlapped send completed I can't really realize in the method ManageSend which of the overlapped structures was actually sent with the call WSAGetOverlappedResult:

WSAGetOverlappedResult(SocketArray[index]->Socket, over, &SendBytes, FALSE, &flags)

I have to use WorkerRoutine temporary to gain control over sending but perhaps somebody knows how to address the issue. Or may there is some different method which returns the overlapped structure which was completed?

htonus
  • 629
  • 1
  • 9
  • 19
  • You have to pass `WSAGetOverlappedResult()` the same `WSAOVERLAPPED` that you passed to `WSASend()`. What is the problem you have doing that? If you have an array of `WSAOVERLAPPED` objects to go along with your socket and event arrays, you will know exactly which `WSAOVERLAPPED` completed. You might consider defining a struct to hold the `SOCKET`, `WSAEVENT` and `WSAOVERLAPPED` of a pending socket operation, and then have an array/list of those structs. Or, associate your sockets with an I/O Completion Port and then `GetQueuedCompletionStatus()` will give you the actual `OVERLAPPED` object. – Remy Lebeau Oct 25 '17 at 20:37
  • I can go either with APC or IOCP which are mutual exclusive. I just took ideas from example not entire implementation. The problem is that I can send many messages from different threads to the same client. So even if I created array with the `OVERLAPPED` structures I still won't be able to match them. – htonus Oct 26 '17 at 07:32

0 Answers0