Fixed iOS Socket Failure being caused when application is paused. Make it rebind on the same Port when resuming. #382
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
UnitySocketFix does not work as intended in the latest release and currently has several issues, addressed in this PR.
Problem 1: Race condition between
ReceiveLogic
andSocket.Close
untilunitySocketFix.Paused
is set totrue
When application loses focus and
OnApplicationPause
is invoked, Socket is closed andPaused
is set totrue
.Normally,
NetSocket.Close
setsIsRunning
tofalse
which causes next iteration ofReceiveLogic
to exit. However in this caseIsRunning
is not set to true and there is a chance that ReceiveLogic runs after socket has been closed, and beforeunitySocketFix.Paused
is set to true, causing socket exceptions.This is addressed in this PR by setting
unitySocketFix.Paused
to true beforeNetSocket.Close
is called, making sure that nextReceiveLogic
cycle will exit and not attempt to poll socket that is closed.Problem 2: Early exit from
ReceiveLogic
when restoring Socket after application gains focusWhen application gains focus back,
NetSocket.Bind
is called to restore the socket, only after whichunitySocketFix.Paused
is set back tofalse
.However NetSocket.Bind runs
ReceiveLogic
which checks ifIsActive
is false, in which case it exits. SincePaused
is set afterSocket.Bind
is called, first cycle ofReceiveLogic
will exit and socket will never receive any data after restoring the connection, causing a timeout disconnect.This problem is addressed in this PR by moving
unitySocketFix.Paused = false
insideNetSocket.Bind
.Problem 3:
_unitySocketFix.Port
is always set to 0, causing socket to bind on a new port after application gains focus_unitySocketFix.Port
is always set to 0 because this value is assigned to the defaultPort
parameter instead ofLocalEndPoint.Port
. When application gained focus again, it tried to bind with 0 receiving a random port. This will cause application to never receive messages from the server again, resulting in a timeout disconnect.While problem 1 is not critical, problem 2 and 3 basically result in connection loss after application on iOS gains focus.