Unity-Technologies / com.unity.netcode.gameobjects

Netcode for GameObjects is a high-level netcode SDK that provides networking capabilities to GameObject/MonoBehaviour workflows within Unity and sits on top of underlying transport layer.
MIT License
2.15k stars 435 forks source link

DisconnectReason is not working #3062

Closed TheCaveOfWonders closed 1 month ago

TheCaveOfWonders commented 2 months ago

Disconnect a player and give a disconnect reason, NetworkManager.DisconnectClient(Player.NetId, nameof(ConnectResult.Kicked)); on the disconnected player's side, read the disconnect reason NetworkManager.Singleton.DisconnectReason, it's always empty. I tried using OnClientStopped event as well as OnClientDisconnectCallback given that the latter is to be deprecated in the future.

This feature was working before, I don't know which version broke it, I'm using v1.10.0

NoelStephensUnity commented 2 months ago

@TheCaveOfWonders I am unable to reproduce this issue and it might require looking at how you are disconnecting the client that is leading up to this. I am attaching a small project that utilizes the same DisconnectClient method and get the expected results running on NGO v1.10.0 and on NGO v1.11.0. It displays in the runtime console log and in the editor console log: image

Here is the project: DisconnectReason.zip

The NetworkManagerHelper file in scripts contains the server-side disconnect client script. The OnDisconnectNotify file in scripts contains the disconnect notification script.

I left the project's manifest set to NGO v1.10.0 so you can verify on your side. Could there be something else that is causing the NetworkManager to be destroyed or possibly other script disconnecting the client prior to invoking the portion of your script that invokes NetworkManager.DisconnectClient(Player.NetId, nameof(ConnectResult.Kicked));?

TheCaveOfWonders commented 2 months ago

hmm thanks for verifying it @NoelStephensUnity I made a few tests on my side, it seems to work when I have release builds connecting to each other, but not when I have a release build and an editor (debug). I'm not sure why when I have an editor and a release build, it stops working, the networking logic in my code is identical for release vs debug (editor) build.

I did put breakpoints in my debug (editor) client in NetworkConnectionManager.cs and we don't get a message of type DisconnectReasonMessage in public static void ReceiveMessage<T>(FastBufferReader reader, ref NetworkContext context, NetworkMessageManager manager) where T : INetworkMessage, new() when the client is disconnected.

That means we will also never reach DisconnectReasonMessage.cs

public void Handle(ref NetworkContext context)
{
    ((NetworkManager)context.SystemOwner).ConnectionManager.DisconnectReason = Reason;
}

I'm not sure if Unity.Netcode behaves slightly differently if it's in Debug vs Release.

Anyhow it's not really a blocker since it works in release builds.

NoelStephensUnity commented 2 months ago

@TheCaveOfWonders Hmm... editor & release... that is odd. So, I tested this with the sample provided above:

What I did notice is on the host side when it disconnects a client with a reason it doesn't populate the host's NetworkManager.DisconnectReason so if you print out a disconnect message on the host side when the host disconnects a client there will be no disconnect reason (which I believe has been the default behavior since that feature was available).

The only other things I could think of are:

If you do discover what your issue was and any of the above suggestions worked (or at a minimum led you to the issue) I would be very interested in knowing so I can put that in my list of troubleshooting tools.

TheCaveOfWonders commented 2 months ago

@NoelStephensUnity

Thanks for your tips, I found the culprit, it turns out it was the Network Simulator that's causing the issue. If I disable it, everything works properly, but if it's enabled we don't get the DisconnectReason (if we're in the editor). image

Looking at the docs https://docs-multiplayer.unity3d.com/tools/current/tools-network-simulator/ I don't see anything to indicate that the Network Simulator only runs in debug, and I currently do not disable it in my release builds, so I can't explain why the issue is only present in the editor builds and not the release builds, except perhaps because the release builds run much faster (maybe it's a timing thing.)

NoelStephensUnity commented 2 months ago

@TheCaveOfWonders Wow... yeah that is an obscure bug indeed... 😸

What version of UTP are you using?

TheCaveOfWonders commented 2 months ago

I'm using UTP 2.3.0