user4592590
user4592590

Reputation:

Can WSARecv() return data immediately?

When calling WSARecv(), is it possible that the data will be returned immediately without going through the completion port phase. In the WSARecv() documentation, it says for the lpNumberOfBytesRecvd parameter:

A pointer to the number, in bytes, of data received by this call if the receive operation completes immediately.

Upvotes: 2

Views: 733

Answers (2)

Len Holgate
Len Holgate

Reputation: 21616

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 either directly or in your normal completion handler.

Upvotes: 4

MSalters
MSalters

Reputation: 179779

Yes, and this is not unusual. Remember that data arrives in IP packets whose boundaries in general do not align with your reads. If your WSARecv call requests data from packets that the OS already holds, there's no point to delay and Windows probably will return it immediately.

Upvotes: 2

Related Questions