Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed iOS Socket Failure being caused when application is paused. Make it rebind on the same Port when resuming. #382

Merged
merged 2 commits into from
Jul 2, 2020

Conversation

leonidumanskiy
Copy link
Contributor

@leonidumanskiy leonidumanskiy commented Jul 2, 2020

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 and Socket.Close until unitySocketFix.Paused is set to true

When application loses focus and OnApplicationPause is invoked, Socket is closed and Paused is set to true.
Normally, NetSocket.Close sets IsRunning to false which causes next iteration of ReceiveLogic to exit. However in this case IsRunning is not set to true and there is a chance that ReceiveLogic runs after socket has been closed, and before unitySocketFix.Paused is set to true, causing socket exceptions.

This is addressed in this PR by setting unitySocketFix.Paused to true before NetSocket.Close is called, making sure that next ReceiveLogic cycle will exit and not attempt to poll socket that is closed.

Problem 2: Early exit from ReceiveLogic when restoring Socket after application gains focus

When application gains focus back, NetSocket.Bind is called to restore the socket, only after which unitySocketFix.Paused is set back to false.
However NetSocket.Bind runs ReceiveLogic which checks if IsActive is false, in which case it exits. Since Paused is set after Socket.Bind is called, first cycle of ReceiveLogic 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 inside NetSocket.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 default Port parameter instead of LocalEndPoint.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.

@RevenantX RevenantX merged commit 8958e67 into RevenantX:master Jul 2, 2020
@RevenantX
Copy link
Owner

@leonidumanskiy thanks for fixes! I wasn't able to massively test this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants