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
feat: disconnect reason #2280
Conversation
…s/com.unity.multiplayer.mlapi into feat/disconnect-reason
…s/com.unity.multiplayer.mlapi into feat/disconnect-reason
There was a problem hiding this 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. 💯
com.unity.netcode.gameobjects/Tests/Runtime/DisconnectReasonTests.cs
Outdated
Show resolved
Hide resolved
com.unity.netcode.gameobjects/Runtime/Messaging/DisconnectReasonMessage.cs
Outdated
Show resolved
Hide resolved
com.unity.netcode.gameobjects/Runtime/Messaging/DisconnectReasonMessage.cs
Outdated
Show resolved
Hide resolved
@SamuelBellomo fyi |
com.unity.netcode.gameobjects/Tests/Runtime/Messaging/DisconnectReasonTests.cs
Outdated
Show resolved
Hide resolved
/// <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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :(
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
…s/com.unity.multiplayer.mlapi into feat/disconnect-reason
* disconnect reason
Tentative API addition for disconnection reason