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

feat: disconnect reason #2280

Merged
merged 37 commits into from Nov 7, 2022
Merged

feat: disconnect reason #2280

merged 37 commits into from Nov 7, 2022

Conversation

jeffreyrainy
Copy link
Contributor

Tentative API addition for disconnection reason

@jeffreyrainy jeffreyrainy changed the title Feat/disconnect reason feat: disconnect reason Oct 27, 2022
@jeffreyrainy jeffreyrainy marked this pull request as ready for review October 28, 2022 21:16
@jeffreyrainy jeffreyrainy requested a review from a team as a code owner October 28, 2022 21:16
Copy link
Collaborator

@NoelStephensUnity NoelStephensUnity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!
Really like your solution to notify the user that they exceeded the maximum allowed message size. Much better than my suggested exception. 💯

@JesseOlmer
Copy link
Contributor

@SamuelBellomo fyi

@jeffreyrainy jeffreyrainy enabled auto-merge (squash) November 2, 2022 18:40
/// <param name="clientId">The ClientId to disconnect</param>
/// <param name="reason">Disconnection reason. If set, client will receive a DisconnectReasonMessage and have the
/// reason available in the NetworkManager.DisconnectReason property</param>
public void DisconnectClient(ulong clientId, string reason)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a flow in boss room where when the host shuts down gracefully, we'll send a message to all clients telling them "hey! this is a nice shutdown, please give a popup saying the host just left". We need this flow, since else clients will think there was some wifi issues and will do their reconnect coroutine.
I wonder how we'd do this now that we have this new flow. Since shutdown also disconnects clients, could it be possible to set a reason there as well?
Or would users be expected to disconnect all clients with reason before shutting down?
Cause without that message, users would have no way of knowing if this is an elegant shutdown (and that clients just go back to main menu) vs a weird disconnect where the client would make some effort to try to reconnect, not knowing what happened to the host.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the solution could still be a custom message (or RPC thinking of it) and still have the "delay one frame to send the RPC so we flush before closing the connection). We would still have this weird "wait one frame" though :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add an API to shutdown with a disconnect reason. Internally, it would disconnect all clients with this reason, and then shutdown. This would be exactly equivalent to you disconnecting all clients before shutting down. Please let me know if you are requesting this API.

That said, from there, two things can happen. Either the UTP flushing of its queue survives the shutdown. Or maybe it doesn't. Maybe shutdown overrides the queuing and you lose recent sends. Maybe it guarantees flushing before shutting down. Someone would need to check. In any case, if it doesn't do what is needed you'd have to file a bug with Transport.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure I don't have the power to request anything of you :P
Since we are adding this new way of doing things, with disconnect reason, my thinking was it'd be great if this was used in most places a disconnect could happen.
We couldn't do it for ALL cases for sure, timeouts and the such wouldn't get the reason message anyway. But it'd make sure disconnect reason is a first class citizen instead of just a patch/something added later?
Besides shutdown, are there other places users can be disconnected?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So just tested this real quick in boss room when pointing to your branch and it seems to work (and yep, UTP already allowed to flush, that was fixed a few months ago):

        if (m_DisconnectReason != ConnectStatus.Undefined)
        {
            string reasonString = JsonUtility.ToJson(m_DisconnectReason);
            var connectedClients = NetworkManager.Singleton.ConnectedClients.ToList();
            for (int i = connectedClients.Count - 1; i >= 0; i--)
            {
                var connectedClient = connectedClients[i];
                if (connectedClient.Key != NetworkManager.Singleton.LocalClientId)
                {
                    NetworkManager.Singleton.DisconnectClient(connectedClient.Key, reason: reasonString);
                }
            }
        }

        m_ConnectionManager.NetworkManager.Shutdown()

And then reading that value in OnClientDisconnectCallback. Testing it in game, when I shutdown the host I'll get the usual "host has left" popup on clients. It's a bit cumbersome to write that for loop but not the end of the world. Not the mountain I'll die on :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, if UTP flushes even on shutdown, you may convince me to add a Shutdown API that also takes a reason. Let me think about it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's particular to your DisconnectClient which does its own sort of flushing. I'd need to check, but if I were to send an RPC right before shutdown, I think it wouldn't work and I'd need to wait a frame before doing the Shutdown. If I remember the conversation from a few months ago correctly, UTP did its flush, but NGO was clearing things before the UTP flush. Your disconnect flushes things before the shutdown clearing (I think, to confirm)

@jeffreyrainy jeffreyrainy merged commit 23be561 into develop Nov 7, 2022
@jeffreyrainy jeffreyrainy deleted the feat/disconnect-reason branch November 7, 2022 17:09
jakobbbb pushed a commit to GooseGirlGames/com.unity.netcode.gameobjects that referenced this pull request Feb 22, 2023
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

5 participants