Skip to content

fix: client side disconnect incorrect client count on host-server side [MTTB-135] (Up-Port) #3075

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Additional documentation and release notes are available at [Multiplayer Documen

### Fixed

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

### Changed

## [2.0.0] - 2024-09-12
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,12 @@ internal void InvokeOnClientConnectedCallback(ulong clientId)
continue;
}

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

try
Expand Down Expand Up @@ -496,24 +500,32 @@ internal void DisconnectEventHandler(ulong transportClientId)
// 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.
MessageManager.ProcessIncomingMessageQueue();

InvokeOnClientDisconnectCallback(clientId);

if (LocalClient.IsHost)
{
InvokeOnPeerDisconnectedCallback(clientId);
}

if (LocalClient.IsServer)
{
// We need to process the disconnection before notifying
OnClientDisconnectFromServer(clientId);

// Now notify the client has disconnected
InvokeOnClientDisconnectCallback(clientId);

if (LocalClient.IsHost)
{
InvokeOnPeerDisconnectedCallback(clientId);
}
}
else // As long as we are not in the middle of a shutdown
if (!NetworkManager.ShutdownInProgress)
else
{
// We must pass true here and not process any sends messages as we are no longer connected.
// Otherwise, attempting to process messages here can cause an exception within UnityTransport
// as the client ID is no longer valid.
NetworkManager.Shutdown(true);
// Notify local client of disconnection
InvokeOnClientDisconnectCallback(clientId);

// As long as we are not in the middle of a shutdown
if (!NetworkManager.ShutdownInProgress)
{
// We must pass true here and not process any sends messages as we are no longer connected.
// Otherwise, attempting to process messages here can cause an exception within UnityTransport
// as the client ID is no longer valid.
NetworkManager.Shutdown(true);
}
}
#if DEVELOPMENT_BUILD || UNITY_EDITOR
s_TransportDisconnect.End();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected override IEnumerator OnStartedServerAndClients()
public IEnumerator ValidateApprovalTimeout()
{
// Just delay for a second
yield return new WaitForSeconds(1);
yield return new WaitForSeconds(k_TestTimeoutPeriod * 0.25f);

// Verify we haven't received the time out message yet
NetcodeLogAssert.LogWasNotReceived(LogType.Log, m_ExpectedLogMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ public IEnumerator ClientPlayerDisconnected([Values] ClientDisconnectType client
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!");
Assert.IsTrue(m_DisconnectedEvent.ContainsKey(m_ClientNetworkManagers[0]), $"Could not find the client {nameof(NetworkManager)} disconnect event entry!");
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!");
Assert.IsTrue(m_ServerNetworkManager.ConnectedClientsIds.Count == 1, $"Expected connected client identifiers count to be 1 but it was {m_ServerNetworkManager.ConnectedClientsIds.Count}!");
Assert.IsTrue(m_ServerNetworkManager.ConnectedClients.Count == 1, $"Expected connected client identifiers count to be 1 but it was {m_ServerNetworkManager.ConnectedClients.Count}!");
Assert.IsTrue(m_ServerNetworkManager.ConnectedClientsList.Count == 1, $"Expected connected client identifiers count to be 1 but it was {m_ServerNetworkManager.ConnectedClientsList.Count}!");
}

if (m_OwnerPersistence == OwnerPersistence.DestroyWithOwner)
Expand Down
Loading