Retrieve buffer with multiple overlapped I/O requests

380 views Asked by At

There is something I'd like to know about overlapped I/O under windows, both with and without I/O completion ports. I know in advance how many packets I will be receiving after using WSASend().

So I'd like to do that

for (int i = 0; i < n; i++)
   WSARecv(sock, &buffer_array[i], 1, NULL, 0, &overlapped, completion_routine);

My problem is : how can I know which buffer has been filled upon notification the buffer has been filled? I mean, without guessing by the order of the calls (buffer[0], buffer[1], buffer[2], etc.).

I would find an alternative solution that gives me the buffer pointer at the time of the notification much more clean for example, and more easily changeable/adaptable as the design of my application evolves.

Thanks.

2

There are 2 answers

0
Martin James On

When using a completion routine, the hEvent field in the OVERLAPPED block is unused and can be used to pass context info into the completion routine. Typically, this would be a pointer to a buffer class instance or an index to an array of buffer instances. Often, the OVL block would be a struct member of the instance since you need a separate OVL per call.

5
usr On

Right now you are starting n concurrent receive operations. Instead, start them one after the other. Start the next one when the previous one has completed.