Skip to content

Commit 9493824

Browse files
authored
fix: Incorrect clientId in OnClientConnectedCallback (#3312)
* fix: Incorrect clientId in OnClientConnectedCallback * CHANGELOG.md
1 parent 7b239f2 commit 9493824

File tree

5 files changed

+90
-1
lines changed

5 files changed

+90
-1
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1212

1313
### Fixed
1414

15+
- Fixed `OnClientConnectedCallback` passing incorrect `clientId` when scene management is disabled. (#3312)
1516
- Fixed DestroyObject flow on non-authority game clients. (#3291)
1617
- Fixed exception being thrown when a `GameObject` with an associated `NetworkTransform` is disabled. (#3243)
1718
- Fixed issue where the scene migration synchronization table was not cleaned up if the `GameObject` of a `NetworkObject` is destroyed before it should have been. (#3230)

com.unity.netcode.gameobjects/Runtime/Messaging/Messages/ConnectionApprovedMessage.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public void Handle(ref NetworkContext context)
305305
NetworkLog.LogInfo($"[Client-{OwnerClientId}][Scene Management Disabled] Synchronization complete!");
306306
}
307307
// When scene management is disabled we notify after everything is synchronized
308-
networkManager.ConnectionManager.InvokeOnClientConnectedCallback(context.SenderId);
308+
networkManager.ConnectionManager.InvokeOnClientConnectedCallback(OwnerClientId);
309309

310310
// For convenience, notify all NetworkBehaviours that synchronization is complete.
311311
networkManager.SpawnManager.NotifyNetworkObjectsSynchronized();

com.unity.netcode.gameobjects/Tests/Runtime/Connection.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using NUnit.Framework;
4+
using Unity.Netcode.TestHelpers.Runtime;
5+
using UnityEngine.TestTools;
6+
7+
namespace Unity.Netcode.RuntimeTests
8+
{
9+
[TestFixture(SceneManagementState.SceneManagementEnabled, NetworkTopologyTypes.DistributedAuthority)]
10+
[TestFixture(SceneManagementState.SceneManagementDisabled, NetworkTopologyTypes.DistributedAuthority)]
11+
[TestFixture(SceneManagementState.SceneManagementEnabled, NetworkTopologyTypes.ClientServer)]
12+
[TestFixture(SceneManagementState.SceneManagementDisabled, NetworkTopologyTypes.ClientServer)]
13+
internal class ClientConnectionTests : IntegrationTestWithApproximation
14+
{
15+
protected override int NumberOfClients => 3;
16+
private readonly bool m_SceneManagementEnabled;
17+
private HashSet<ulong> m_ServerCallbackCalled = new HashSet<ulong>();
18+
private HashSet<ulong> m_ClientCallbackCalled = new HashSet<ulong>();
19+
20+
public ClientConnectionTests(SceneManagementState sceneManagementState, NetworkTopologyTypes networkTopologyType) : base(networkTopologyType)
21+
{
22+
m_SceneManagementEnabled = sceneManagementState == SceneManagementState.SceneManagementEnabled;
23+
}
24+
25+
protected override void OnServerAndClientsCreated()
26+
{
27+
m_ServerNetworkManager.NetworkConfig.EnableSceneManagement = m_SceneManagementEnabled;
28+
m_ServerNetworkManager.OnClientConnectedCallback += Server_OnClientConnectedCallback;
29+
30+
foreach (var client in m_ClientNetworkManagers)
31+
{
32+
client.NetworkConfig.EnableSceneManagement = m_SceneManagementEnabled;
33+
client.OnClientConnectedCallback += Client_OnClientConnectedCallback;
34+
}
35+
36+
base.OnServerAndClientsCreated();
37+
}
38+
39+
[UnityTest]
40+
public IEnumerator VerifyOnClientConnectedCallback()
41+
{
42+
yield return WaitForConditionOrTimeOut(AllCallbacksCalled);
43+
AssertOnTimeout("Timed out waiting for all clients to be connected!");
44+
45+
// The client callbacks should have been called once per client (called once on self)
46+
Assert.True(m_ClientCallbackCalled.Count == NumberOfClients);
47+
48+
// The server callback should be called for self, and then once per client
49+
Assert.True(m_ServerCallbackCalled.Count == 1 + NumberOfClients);
50+
}
51+
52+
private void Server_OnClientConnectedCallback(ulong clientId)
53+
{
54+
if (!m_ServerCallbackCalled.Add(clientId))
55+
{
56+
Assert.Fail($"Client already connected: {clientId}");
57+
}
58+
}
59+
60+
private void Client_OnClientConnectedCallback(ulong clientId)
61+
{
62+
if (!m_ClientCallbackCalled.Add(clientId))
63+
{
64+
Assert.Fail($"Client already connected: {clientId}");
65+
}
66+
}
67+
68+
private bool AllCallbacksCalled()
69+
{
70+
foreach (var client in m_ClientNetworkManagers)
71+
{
72+
if (!m_ClientCallbackCalled.Contains(client.LocalClientId) || !m_ServerCallbackCalled.Contains(client.LocalClientId))
73+
{
74+
return false;
75+
}
76+
}
77+
78+
return m_ServerCallbackCalled.Contains(m_ServerNetworkManager.LocalClientId);
79+
}
80+
}
81+
}
82+

com.unity.netcode.gameobjects/Tests/Runtime/Connection/ClientConnectionTests.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)