Made NATPunch socket keep trying if bind failed (Thanks FakeByte!)

This commit is contained in:
Derek S 2021-04-05 23:54:37 -05:00
parent 6595d4ead2
commit 689800c377

View file

@ -433,10 +433,21 @@ namespace LightReflectiveMirror
initalData.WriteString(ref sendPos, data.ReadString(ref pos));
NATPunchtroughPort = data.ReadInt(ref pos);
if (_NATPuncher == null)
{
_NATPuncher = new UdpClient { ExclusiveAddressUse = false };
while (true)
{
try
{
_NATIP = new IPEndPoint(IPAddress.Parse(GetLocalIp()), UnityEngine.Random.Range(16000, 17000));
_NATPuncher.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
_NATPuncher.Client.Bind(_NATIP);
break;
}
catch {} // Binding port is in use, keep trying :P
}
}
IPAddress serverAddr;