I assume this had something to do with IAsyncResult.CompletedSynchronously
[^] (possibly derived from Socket.Available
[^])? Interesting because under extreme and consistent load this means that the async loop could cause a StackOverflowException
(even with your fixed code); I wonder if there is a way to turn off this 'optimization' without having to resort to:
private void StartAsyncReadLoop()
{
ReadAsyncLoop(null);
}
private void ReadAsyncLoop(IAsyncResult state)
{
try
{
if (state != null)
{
var length = _socket.EndReceive(state);
// ...
}
// Ensure that only one level of recursion happens.
if (state == null || !state.CompletedSynchronously)
\_socket.BeginReceive(..., ReadAsyncLoop, null);
else
// Maybe it's best to just always do this. Although considering how sockets use the thread pool
// it's very likely overkill.
ThreadPool.QueueUserWorkItem(\_ => \_socket.BeginReceive(..., ReadAsyncLoop, null));
}
catch (SocketException ex)
{
// ...
}
}
I wonder if Socket.UseOnlyOverlappedIO
[^] would have an effect on this, considering Overlapped IO is my 'asyncy' than IOCP.
He who asks a question is a fool for five minutes. He who does not ask a question remains a fool forever. [Chinese Proverb] Jonathan C Dickinson (C# Software Engineer)