Skip to content

Commit fa1d548

Browse files
author
leonid.umanskiy
committed
Fix iOS Socket Failure being caused when application is paused. Make it rebind on the same Port when resuming.
1 parent 17acdd9 commit fa1d548

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

LiteNetLib/NetSocket.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ private void OnApplicationPause(bool pause)
3838
return;
3939
if (pause)
4040
{
41-
Socket.Close(true);
4241
Paused = true;
42+
Socket.Close(true);
4343
}
4444
else if (Paused)
4545
{
@@ -48,7 +48,6 @@ private void OnApplicationPause(bool pause)
4848
NetDebug.WriteError("[S] Cannot restore connection \"{0}\",\"{1}\" port {2}", BindAddrIPv4, BindAddrIPv6, Port);
4949
Socket.OnErrorRestore();
5050
}
51-
Paused = false;
5251
}
5352
}
5453
}
@@ -182,6 +181,9 @@ public bool Bind(IPAddress addressIPv4, IPAddress addressIPv6, int port, bool re
182181
_udpSocketv4 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
183182
if (!BindSocket(_udpSocketv4, new IPEndPoint(addressIPv4, port), reuseAddress))
184183
return false;
184+
185+
LocalPort = ((IPEndPoint)_udpSocketv4.LocalEndPoint).Port;
186+
185187
#if UNITY_IOS && !UNITY_EDITOR
186188
if (_unitySocketFix == null)
187189
{
@@ -192,12 +194,15 @@ public bool Bind(IPAddress addressIPv4, IPAddress addressIPv6, int port, bool re
192194
_unitySocketFix.BindAddrIPv4 = addressIPv4;
193195
_unitySocketFix.BindAddrIPv6 = addressIPv6;
194196
_unitySocketFix.Reuse = reuseAddress;
195-
_unitySocketFix.Port = port;
197+
_unitySocketFix.Port = LocalPort;
196198
_unitySocketFix.IPv6 = ipv6;
197199
}
200+
else
201+
{
202+
_unitySocketFix.Paused = false;
203+
}
198204
#endif
199205

200-
LocalPort = ((IPEndPoint)_udpSocketv4.LocalEndPoint).Port;
201206
IsRunning = true;
202207
_threadv4 = new Thread(ReceiveLogic)
203208
{

0 commit comments

Comments
 (0)