When an overlapped WSARecv()
completes with IO_PENDING
or SUCCESS
a completion packet is queued to the IOCP see this MSDN article for details.
With Vista or later you can change this by calling SetFileCompletionNotificationModes()
for the socket and passing FILE_SKIP_COMPLETION_PORT_ON_SUCCESS
(note you can read File as Socket in the docs, the concept of a file handle translates directly to a socket).
If you DO enable completion port skipping then when a WSARecv()
returns immediately with data (i.e. a SUCCESS
return rather than an IO_PENDING
return) then you MUST handle it directly at the WSARecv()
call site as you WILL NOT get a completion packet.
Note that enabling "skip completion port" processing is great for reducing context switching, but you DO now need to handle completions in two ways, either directly (whenWSARecv()
returns SUCCESS
) or in your normal completion handler (when WSARecv()
returns an error and the error is IO_PENDING
). Whereas before both results generated a completion packet.
So, in answer to your question...
UNLESS you have enabled "skip completion port" processing there is no reason to use the value of lpNumberOfBytesRecvd
at the call site for WSARecv()
because even if the call returns SUCCESS
because data is already available you will STILL get a completion queued to the IOCP which you MUST handle in the usual way.
If and ONLY if you HAVE enabled "skip completion port" processing should you process the data returned by a call to WSARecv()
that returns SUCCESS
at the point where you get the SUCCESS
result.