Reputation:
When using IOCP, if I call WSASend()
, do I have to wait for the notification to arrive before making another call to it, or can I call it multiple times before receiving any notification, for example is something like this allowed:
WSASend();
// Call it again without waiting for notification of previous call
WSASend();
// Call it again without waiting for notification of previous call
WSASend();
Upvotes: 2
Views: 376
Reputation: 21616
Yes you can issue multiple overlapped operations on a single socket without needing to wait for any completions to occur.
One thing that you need to be aware of with multiple outstanding WSASend()
calls on a TCP socket is that you are effectively handing over resource management for the buffers used in the WSASend()
calls to the peer on the other end of the socket. The reason for this is that if you send data and the peer doesn't read it as fast as you are sending then you will eventually cause TCP's flow control to kick in. This doesn't prevent you issuing more WSASend()
calls and all you will notice is that the completions take longer and longer to occur. See here for more details.
Upvotes: 3
Reputation: 24847
Yes, you can make multiple I/O requests without waiting for completion notifications. Alternatively, you can WSASend() multiple buffers with one call.
Either way, or both, will work fine. The OVERLAPPED block for each call is, essentially, pointers for a linked-list of I/O requests, so they can all be queued up and executed by the kernel and comms stack/s when the I/O resource is available.
This applies to WSARecv etc. overlapped I/O too. This allows the kernel/stack to be loading buffers while user thread code is processing those notified earlier.
Note: the OVERLAPPED block, and the buffers, must be unique per call and their lifetime must extend to the completion notification. You must not let them be RAII'd or delete'd away before a user thread has handled the completion notification. It's usual for the buffer and OVERLAPPED to be members of one 'IOrequest' class, (with a 'SocketContext*' class pointer member to connect every IOrequest up with its bound socket).
Upvotes: 3