Scatter/gather async socket I/O in .NET
-
02-07-2019 - |
Question
I'm trying to use the Stream.BeginWrite Async I/O API in .NET for a high-throughput situation with many short messages. As such, a scatter/gather API will reduce the number of context switches (and CPU usage) tremendously. Does this API use the LPBUFFERS Win32 API at all? Is there an alternative API for Scatter/Gather I/O?
Solution 4
There is no way to do socket scatter/gather I/O in .NET. According to a MSFT blog post, there may be a similar API in .NET 4.5 (whatever that is...)
OTHER TIPS
Looking at the .net sources, the accepted answer seems to be wrong.
SocketAsyncEventArgs
has a BufferList
attribute. When that is used, instead of the Buffer
attribute that can only hold a single contiguous block of memory, operations can make use of scatter/gather DMA, as Socket.SendAsync(SocketAsyncEventArgs)
uses WSASend
internally, that
allows multiple send buffers to be specified making it applicable to the scatter/gather type of I/O
and Socket.SendAsync(SocketAsyncEventArgs)
uses WSARecv
, that
allows multiple receive buffers to be specified making it applicable to the scatter/gather type of I/O
I don't have the .net 3.5 sources handy, but BufferList
exists since .net 3.5, so scatter/gather might have been supported since .net 3.5. The minimum OS requirements for WSASend and WSARecv exist are documented as Windows Vista / Server 2003.
N.B. I don't know what stream you are using, but NetworkStream.BeginWrite
sends a single buffer to the WSASend, so you cannot use that for scatter/gathering.
I would be surprised if you could get to the scatter/gather api's from the BCL (it's for the l33t w1n32 haxx0rz, you know?), but there's always P/Invoke (which is suprisingly easy to use, I've found).
If you want to dig into the guts of the framework, there are a few ways to do it:
1) Reflector
2) MS recently opened up the source for debugging purposes, you can step into it with VS2008 if you enable the option under Debugging/Options/General
3) Koders.com seem to be hosting the framework source too:
http://www.koders.com/csharp/fidCE09E83BE706D0BD370658C3785E82D3A13FC2CE.aspx?s=flush()#L109