Skip to content

Commit ef9e1c1

Browse files
fix: client side disconnect incorrect client count on host-server side [MTTB-135] (Up-Port) (#3075)
* fix Fixed issue with the client count not being correct on the host or server side when a client disconnects itself from a session. * test validation tests for the changes/updates to this pr. * update updating the changelog.
1 parent 3afc6f9 commit ef9e1c1

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Additional documentation and release notes are available at [Multiplayer Documen
1212

1313
### Fixed
1414

15+
- Fixed issue with the client count not being correct on the host or server side when a client disconnects itself from a session. (#3075)
16+
1517
### Changed
1618

1719
## [2.0.0] - 2024-09-12

com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,12 @@ internal void InvokeOnClientConnectedCallback(ulong clientId)
105105
continue;
106106
}
107107

108-
peerClientIds[idx] = peerId;
109-
++idx;
108+
// This assures if the server has not timed out prior to the client synchronizing that it doesn't exceed the allocated peer count.
109+
if (peerClientIds.Length > idx)
110+
{
111+
peerClientIds[idx] = peerId;
112+
++idx;
113+
}
110114
}
111115

112116
try
@@ -496,24 +500,32 @@ internal void DisconnectEventHandler(ulong transportClientId)
496500
// Process the incoming message queue so that we get everything from the server disconnecting us or, if we are the server, so we got everything from that client.
497501
MessageManager.ProcessIncomingMessageQueue();
498502

499-
InvokeOnClientDisconnectCallback(clientId);
500-
501-
if (LocalClient.IsHost)
502-
{
503-
InvokeOnPeerDisconnectedCallback(clientId);
504-
}
505-
506503
if (LocalClient.IsServer)
507504
{
505+
// We need to process the disconnection before notifying
508506
OnClientDisconnectFromServer(clientId);
507+
508+
// Now notify the client has disconnected
509+
InvokeOnClientDisconnectCallback(clientId);
510+
511+
if (LocalClient.IsHost)
512+
{
513+
InvokeOnPeerDisconnectedCallback(clientId);
514+
}
509515
}
510-
else // As long as we are not in the middle of a shutdown
511-
if (!NetworkManager.ShutdownInProgress)
516+
else
512517
{
513-
// We must pass true here and not process any sends messages as we are no longer connected.
514-
// Otherwise, attempting to process messages here can cause an exception within UnityTransport
515-
// as the client ID is no longer valid.
516-
NetworkManager.Shutdown(true);
518+
// Notify local client of disconnection
519+
InvokeOnClientDisconnectCallback(clientId);
520+
521+
// As long as we are not in the middle of a shutdown
522+
if (!NetworkManager.ShutdownInProgress)
523+
{
524+
// We must pass true here and not process any sends messages as we are no longer connected.
525+
// Otherwise, attempting to process messages here can cause an exception within UnityTransport
526+
// as the client ID is no longer valid.
527+
NetworkManager.Shutdown(true);
528+
}
517529
}
518530
#if DEVELOPMENT_BUILD || UNITY_EDITOR
519531
s_TransportDisconnect.End();

com.unity.netcode.gameobjects/Tests/Runtime/ConnectionApprovalTimeoutTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected override IEnumerator OnStartedServerAndClients()
8282
public IEnumerator ValidateApprovalTimeout()
8383
{
8484
// Just delay for a second
85-
yield return new WaitForSeconds(1);
85+
yield return new WaitForSeconds(k_TestTimeoutPeriod * 0.25f);
8686

8787
// Verify we haven't received the time out message yet
8888
NetcodeLogAssert.LogWasNotReceived(LogType.Log, m_ExpectedLogMessage);

com.unity.netcode.gameobjects/Tests/Runtime/DisconnectTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ public IEnumerator ClientPlayerDisconnected([Values] ClientDisconnectType client
180180
Assert.IsTrue(m_DisconnectedEvent[m_ServerNetworkManager].ClientId == m_ClientId, $"Expected ClientID {m_ClientId} but found ClientID {m_DisconnectedEvent[m_ServerNetworkManager].ClientId} for the server {nameof(NetworkManager)} disconnect event entry!");
181181
Assert.IsTrue(m_DisconnectedEvent.ContainsKey(m_ClientNetworkManagers[0]), $"Could not find the client {nameof(NetworkManager)} disconnect event entry!");
182182
Assert.IsTrue(m_DisconnectedEvent[m_ClientNetworkManagers[0]].ClientId == m_ClientId, $"Expected ClientID {m_ClientId} but found ClientID {m_DisconnectedEvent[m_ServerNetworkManager].ClientId} for the client {nameof(NetworkManager)} disconnect event entry!");
183+
Assert.IsTrue(m_ServerNetworkManager.ConnectedClientsIds.Count == 1, $"Expected connected client identifiers count to be 1 but it was {m_ServerNetworkManager.ConnectedClientsIds.Count}!");
184+
Assert.IsTrue(m_ServerNetworkManager.ConnectedClients.Count == 1, $"Expected connected client identifiers count to be 1 but it was {m_ServerNetworkManager.ConnectedClients.Count}!");
185+
Assert.IsTrue(m_ServerNetworkManager.ConnectedClientsList.Count == 1, $"Expected connected client identifiers count to be 1 but it was {m_ServerNetworkManager.ConnectedClientsList.Count}!");
183186
}
184187

185188
if (m_OwnerPersistence == OwnerPersistence.DestroyWithOwner)

0 commit comments

Comments
 (0)