internal unsafe void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue, bool silent)
{
if (NetEventSource.IsEnabled) NetEventSource.Enter(this, $"optionLevel:{optionLevel} optionName:{optionName} optionValue:{optionValue} silent:{silent}");
if (silent && (CleanedUp || _handle.IsInvalid))
{
if (NetEventSource.IsEnabled) NetEventSource.Info(this, "skipping the call");
return;
}
SocketError errorCode = SocketError.Success;
try
{
errorCode = SocketPal.SetSockOpt(_handle, optionLevel, optionName, optionValue);
if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"Interop.Winsock.setsockopt returns errorCode:{errorCode}");
}
catch
{
if (silent && _handle.IsInvalid)
{
return;
}
throw;
}
// Keep the internal state in sync if the user manually resets this.
if (optionName == SocketOptionName.PacketInformation && optionValue == 0 &&
errorCode == SocketError.Success)
{
_receivingPacketInformation = false;
}
if (silent)
{
return;
}
// Throw an appropriate SocketException if the native call fails.
if (errorCode != SocketError.Success)
{
// Update the internal state of this socket according to the error before throwing.
SocketException socketException = new SocketException((int)errorCode);
UpdateStatusAfterSocketError(socketException);
if (NetEventSource.IsEnabled) NetEventSource.Error(this, socketException);
throw socketException;
}
}