WSARecvFrom unclear return behavior with IOCP (UDP)

151 Views Asked by At

I am using winapi function WSARecvFrom to receive UDP data on a socket, I am using overlapped IO with IOCP ports.

In the documentation its stated:

If no error occurs and the receive operation has completed immediately, WSARecvFrom returns zero.

And in the remarks:

If an overlapped operation completes immediately, WSARecvFrom returns a value of zero and the lpNumberOfBytesRecvd parameter is updated with the number of bytes received and the flag bits pointed by the lpFlags parameter are also updated. If the overlapped operation is successfully initiated and will complete later, WSARecvFrom returns SOCKET_ERROR and indicates error code WSA_IO_PENDING. In this case, lpNumberOfBytesRecvd and lpFlags is not updated.

However if the function returns 0 the buffers are not updated with any new data, but the InternalHigh member of OVERLAPPED struct (which is the same as lpNumberOfBytesRecvd parameter, but for OVERLAPPED IO it should be NULL as per docs) is updated with a different number (which I assume is the correct number of bytes)

I have found in a forum from quite a while ago some people said when using WSARecvFrom with UDP and IOCP and it returns 0 it should just be ignored, is this really the case?

Should WSARecvFrom returning 0 for UDP with IOCP be ignored, if not, why would the documentation be so unclear?

I have also heard that IOCP is a superset of Overlapped IO and that is why im refering it to as such in this thread (im not sure if its really the case but this thread seems to confirm it)

1

There are 1 best solutions below

0
RbMm On

WSA api behavior not depend from - are socket bind to IOCP or not. it depend from asynchronous (frequently it called Overlapped) I/O used or not.

I have also heard that IOCP is a superset of Overlapped IO and that is why im refering it to as such in this thread (im not sure if its really the case but this thread seems to confirm it)

this is absolute nonsense. IOCP and Overlapped(asynchronous) I/O different things. IOCP (xor APC and Event) is means to receive a notification when I/O is complete.

Should WSARecvFrom returning 0 for UDP with IOCP be ignored

of course no. must not be ignored any return code.

why would the documentation be so unclear?

unclear what you mean.

However if the function returns 0 the buffers are not updated with any new data, but the InternalHigh member of OVERLAPPED struct is updated with a different number (which I assume is the correct number of bytes)

this is not true. when I/O is completed without error - buffers is updated. if you think it not updated - possible you bad check, pass wrong pointers to api, or may be you send data which already in buffers - say you send 0 bytes and 0 already in buffers. wihout view concrete code - impossible say.

the general rule for overlapped I/O:

  1. api return ERROR_IO_PENDING - I/O yet not completed. when it will be completed - some notification will be - event set or APC or packet pushed to IOCP. when you got this notification - I/O already completed. this final status will be in OVERLAPPED. it can be 0 or some error
  2. api return another error - this mean I/O is finished with this error. no data is received or sent. overlapped will be not updated in this case.
  3. api return 0 (noerror). I/O is finished. OVERLAPPED updated with actual bytes count received or send. buffers (if this recv) already updated with received data