自旋等待

This commit is contained in:
xljiulang 2021-07-24 20:58:45 +08:00
parent 30382becd7
commit 87587d1e76

View File

@ -50,9 +50,24 @@ namespace FastGithub.Dns
const int SIO_UDP_CONNRESET = unchecked((int)0x9800000C);
this.socket.IOControl(SIO_UDP_CONNRESET, new byte[4], new byte[4]);
}
this.socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
this.socket.Bind(new IPEndPoint(address, port));
var maxSpinCount = 100;
var spinWait = new SpinWait();
var localEndPoint = new IPEndPoint(address, port);
for (var i = 0; i < maxSpinCount; i++)
{
try
{
this.socket.Bind(localEndPoint);
break;
}
catch (SocketException ex) when (ex.SocketErrorCode == SocketError.AccessDenied)
{
spinWait.SpinOnce();
}
}
}
/// <summary>