Hey everybody. I have been having some issues learning IOCP and I was hoping someone here might understand it well enough to help me out.
I'm currently trying to learn the basics of utilizing IOCP from this code sample. Note: This sample is not using AcceptEx but will look into that later.
This is an echo iocp server from Microsoft.
There are a couple things I am confused about.
When a new connection is accepted, a WSARecv is called and the overlapped structure is passed in.
From what I gathered, for this particular code, in one of the worker thread's, the Worker Thread Function will be able to then receive the data from this WSARecv call via the call to GetQueuedCompletionStatus.
From the Worker Thread Function, it looks like this server follows the following pattern per connection:
1. Check if there is a pending io operation for some connection
2. If pending io operation is a read operation, store the size/buffer in the overlapped context and queue up a WSASend call as a write operation using the overlapped structure.
3. If pending io operation is write operation, check if the # of bytes matched the total bytes. If all bytes were sent, update the overlapped structure and queue up WSARecv for next read operation. If not all bytes sent, queue up another WSASend to attempt to send the remaining bytes.
So it seems like the pattern is this server attempts to read data, then will echo that data back and once that data is echo'ed back, the server will read data again.
Here is where I am confused.
How would I handle the following situations:
- In this situation, I want the server to continue performing as an echo server. On top of its current functionality, I want it to also dispatch a 4 byte message every X seconds.
- In this situation, I want the server to echo all of the client's messages to the other clients as well. So if client #1 sends 'AABB', the server will send 'AABB' to client #1 and the server will send 'AABB' to client #2.
Let me explain why I am stuck on these situations.
For situation #1, The server is not guaranteed to send the full packet when echo'ing data back to the client. How would I guarantee that the server will echo back the full packet before dispatching the 4 byte message? I considered having a queue or something tied to the socket context, but I am also wondering about how would I send the 4 byte data if the client never sent anything to be echo'd? Will it be an issue if I have queued up a WSARecv on the socket, no data has been received, and I queue up a WSASend at the same time? Also, the call to WSASend to dispatch this 4 byte data will cause an extra call to WSARecv after the data is sent. How should this be avoided since there will already be an outstanding WSARecv from the server listening for packets to echo.
For situation #2, my confusion again comes across verifying that the packets will be sent in the correct order and not split up in case of partial sends and a similar confusion about if this new call to WSASend will cause an extra call to WSARecv after the data is sent.
Sorry the post is so long. I tried to include hyperlinks to the code in question to clarify what I am asking. Thank you so much for any input.