RevenantX / LiteNetLib

Lite reliable UDP library for Mono and .NET
https://revenantx.github.io/LiteNetLib/index.html
MIT License
3k stars 489 forks source link

Surface Shutdown Event #524

Closed starburst997 closed 1 year ago

starburst997 commented 1 year ago

On iOS (Unity) at least, I noticed that the Sockets can become in a permanent "Shutdown" state when you put the App in background and go back to it. Meaning you have to do a manager.Stop() / manager.Start() to re-create them if you want to call manager.Connect() again (which I do now if I catch this event).

RevenantX commented 1 year ago

There was PausedSocketFix for that thing, but maybe something changed in latest unity. https://github.com/RevenantX/LiteNetLib/blob/master/LiteNetLib/PausedSocketFix.cs

Are you using LiteNetLib as asmdef or dll?

starburst997 commented 1 year ago

asmdef (source) on Unity 2021.1.28 using master of this lib

It sure look like PausedSocketFix should be fixing that issue but at least on my combination of device (iphone 7 / iOS 15.5) / unity, it doesn't. What do work tho is listening to the shutdown event and then restarting the manager so perhaps it can happens later and not on the exact same frame as UnityEngine.Application.focusChanged.

starburst997 commented 1 year ago

And the error I was seeing in my log while debugging why my re-connect code wasn't working:

[S] System.Net.Sockets.SocketException (0x80004005): The socket has been shut down
  at System.Net.Sockets.Socket.SendTo (System.Byte[] buffer, System.Int32 offset, System.Int32 size, System.Net.Sockets.SocketFlags socketFlags, System.Net.EndPoint remoteEP) [0x00000] in <00000000000000000000000000000000>:0 
  at LiteNetLib.NetManager.SendRaw (System.Byte[] message, System.Int32 start, System.Int32 length, System.Net.IPEndPoint remoteEndPoint) [0x00000] in <00000000000000000000000000000000>:0 
  at LiteNetLib.NetManager.UpdateLogic () [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.ThreadStart.Invoke () [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.ContextCallback.Invoke (System.Object state) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <000000