private SocketError InternalSetBlocking(bool desired, out bool current)
{
if (NetEventSource.IsEnabled) NetEventSource.Enter(this, $"desired:{desired} willBlock:{_willBlock} willBlockInternal:{_willBlockInternal}");
if (CleanedUp)
{
if (NetEventSource.IsEnabled) NetEventSource.Exit(this, "ObjectDisposed");
current = _willBlock;
return SocketError.Success;
}
// Can we avoid this call if willBlockInternal is already correct?
bool willBlock = false;
SocketError errorCode;
try
{
errorCode = SocketPal.SetBlocking(_handle, desired, out willBlock);
}
catch (ObjectDisposedException)
{
errorCode = SocketError.NotSocket;
}
if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"Interop.Winsock.ioctlsocket returns errorCode:{errorCode}");
// We will update only internal state but only on successfull win32 call
// so if the native call fails, the state will remain the same.
if (errorCode == SocketError.Success)
{
_willBlockInternal = willBlock;
}
if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"errorCode:{errorCode} willBlock:{_willBlock} willBlockInternal:{_willBlockInternal}");
current = _willBlockInternal;
return errorCode;
}