public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, object optionValue)
{
if (CleanedUp)
{
throw new ObjectDisposedException(this.GetType().FullName);
}
// Validate input parameters.
if (optionValue == null)
{
throw new ArgumentNullException(nameof(optionValue));
}
CheckSetOptionPermissions(optionLevel, optionName);
if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"optionLevel:{optionLevel} optionName:{optionName} optionValue:{optionValue}");
if (optionLevel == SocketOptionLevel.Socket && optionName == SocketOptionName.Linger)
{
LingerOption lingerOption = optionValue as LingerOption;
if (lingerOption == null)
{
throw new ArgumentException(SR.Format(SR.net_sockets_invalid_optionValue, "LingerOption"), nameof(optionValue));
}
if (lingerOption.LingerTime < 0 || lingerOption.LingerTime > (int)UInt16.MaxValue)
{
throw new ArgumentException(SR.Format(SR.ArgumentOutOfRange_Bounds_Lower_Upper, 0, (int)UInt16.MaxValue), "optionValue.LingerTime");
}
SetLingerOption(lingerOption);
}
else if (optionLevel == SocketOptionLevel.IP && (optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership))
{
MulticastOption multicastOption = optionValue as MulticastOption;
if (multicastOption == null)
{
throw new ArgumentException(SR.Format(SR.net_sockets_invalid_optionValue, "MulticastOption"), nameof(optionValue));
}
SetMulticastOption(optionName, multicastOption);
}
else if (optionLevel == SocketOptionLevel.IPv6 && (optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership))
{
// IPv6 Changes: Handle IPv6 Multicast Add / Drop
IPv6MulticastOption multicastOption = optionValue as IPv6MulticastOption;
if (multicastOption == null)
{
throw new ArgumentException(SR.Format(SR.net_sockets_invalid_optionValue, "IPv6MulticastOption"), nameof(optionValue));
}
SetIPv6MulticastOption(optionName, multicastOption);
}
else
{
throw new ArgumentException(SR.net_sockets_invalid_optionValue_all, nameof(optionValue));
}
}