Skip to content

Commit d3408ac

Browse files
fix: Fixing an issue where OnClientDisconnectCallback was not being called when using UTP (#1243)
* fix: retain old behavior of getting a disconnect message * clear serverClientId in server
1 parent 6a6a1bf commit d3408ac

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

com.unity.netcode.adapter.utp/Runtime/UnityTransport.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,11 +448,22 @@ private static unsafe NetworkConnection ParseClientId(ulong netcodeConnectionId)
448448

449449
public override void DisconnectLocalClient()
450450
{
451-
Debug.Assert(m_State == State.Connected, "DisconnectLocalClient should be called on a connected client");
452-
453451
if (m_State == State.Connected)
454452
{
455-
m_Driver.Disconnect(ParseClientId(m_ServerClientId));
453+
if (m_Driver.Disconnect(ParseClientId(m_ServerClientId)) == 0)
454+
{
455+
456+
m_State = State.Disconnected;
457+
458+
459+
// If we successfully disconnect we dispatch a local disconnect message
460+
// this how uNET and other transports worked and so this is just keeping with the old behavior
461+
// should be also noted on the client this will call shutdown on the NetworkManager and the Transport
462+
InvokeOnTransportEvent(NetcodeNetworkEvent.Disconnect,
463+
m_ServerClientId,
464+
default(ArraySegment<byte>),
465+
Time.realtimeSinceStartup);
466+
}
456467
}
457468
}
458469

@@ -699,6 +710,9 @@ public override void Shutdown()
699710

700711
// make sure we don't leak queues when we shutdown
701712
m_SendQueue.Clear();
713+
714+
// We must reset this to zero because UTP actually re-uses clientIds if there is a clean disconnect
715+
m_ServerClientId = 0;
702716
}
703717

704718
public void CreateDriver(UnityTransport transport, out NetworkDriver driver, out NetworkPipeline unreliableSequencedPipeline, out NetworkPipeline reliableSequencedPipeline, out NetworkPipeline reliableSequencedFragmentedPipeline)

0 commit comments

Comments
 (0)