Skip to content

Commit 977f854

Browse files
EmandMpatrickexe
andauthored
feat: [Backport] Expose methods for ClientId to TransportId mappings (#3515)
[MTTB-950](https://jira.unity3d.com/browse/MTTB-950) implements: #2668 fixes: #2359 ## Changelog - Added: Mappings for ClientId to TransportId ## Testing and Documentation - Includes unit tests. ## Backport Backport of #3516 --------- Co-authored-by: Patrick Coglan <[email protected]>
1 parent 4836328 commit 977f854

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1010

1111
### Added
1212

13+
- Added mappings between `ClientId` and `TransportId`. (#3515)
1314
- Added `SinglePlayerTransport` that provides the ability to start as a host for a single player network session. (#3475)
1415
- When using UnityTransport >=2.4 and Unity >= 6000.1.0a1, SetConnectionData will accept a fully qualified hostname instead of an IP as a connect address on the client side. (#3440)
1516

com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,20 @@ private void HostServerInitialize()
11501150
ConnectionManager.InvokeOnClientConnectedCallback(LocalClientId);
11511151
}
11521152

1153+
/// <summary>
1154+
/// Get the TransportId from the associated ClientId.
1155+
/// </summary>
1156+
/// <param name="clientId">The ClientId to get the TransportId from</param>
1157+
/// <returns>The TransportId associated with the given ClientId</returns>
1158+
public ulong GetTransportIdFromClientId(ulong clientId) => ConnectionManager.ClientIdToTransportId(clientId);
1159+
1160+
/// <summary>
1161+
/// Get the ClientId from the associated TransportId.
1162+
/// </summary>
1163+
/// <param name="transportId">The TransportId to get the ClientId from</param>
1164+
/// <returns>The ClientId from the associated TransportId</returns>
1165+
public ulong GetClientIdFromTransportId(ulong transportId) => ConnectionManager.TransportIdToClientId(transportId);
1166+
11531167
/// <summary>
11541168
/// Disconnects the remote client.
11551169
/// </summary>
@@ -1310,7 +1324,7 @@ private void OnDestroy()
13101324
}
13111325
#if UNITY_EDITOR
13121326
EditorApplication.playModeStateChanged -= ModeChanged;
1313-
#endif
1327+
#endif
13141328
}
13151329

13161330
// Command line options

testproject/Assets/Tests/Runtime/NetworkManagerTests.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,28 @@
99

1010
namespace TestProject.RuntimeTests
1111
{
12+
public class NetworkManagerTests : NetcodeIntegrationTest
13+
{
14+
protected override int NumberOfClients => 2;
15+
16+
[Test]
17+
public void ValidateTransportAndClientIds()
18+
{
19+
var transportId = m_ServerNetworkManager.GetTransportIdFromClientId(m_ServerNetworkManager.LocalClientId);
20+
Assert.IsTrue(m_ServerNetworkManager.GetTransportIdFromClientId(m_ServerNetworkManager.LocalClientId) == m_ServerNetworkManager.ConnectionManager.ServerTransportId);
21+
Assert.IsTrue(m_ServerNetworkManager.GetClientIdFromTransportId(transportId) == m_ServerNetworkManager.LocalClientId);
22+
23+
foreach (var client in m_ClientNetworkManagers)
24+
{
25+
transportId = m_ServerNetworkManager.GetTransportIdFromClientId(client.LocalClientId);
26+
Assert.AreEqual(client.LocalClientId, m_ServerNetworkManager.GetClientIdFromTransportId(transportId), "Server and client transport IDs don't match.");
27+
}
28+
}
29+
}
30+
1231
[TestFixture(UseSceneManagement.SceneManagementDisabled)]
1332
[TestFixture(UseSceneManagement.SceneManagementEnabled)]
14-
public class NetworkManagerTests : NetcodeIntegrationTest
33+
public class NetworkManagerSceneTests : NetcodeIntegrationTest
1534
{
1635
private const string k_SceneToLoad = "InSceneNetworkObject";
1736
protected override int NumberOfClients => 0;
@@ -33,7 +52,7 @@ public enum UseSceneManagement
3352

3453
private bool m_UseSceneManagement;
3554

36-
public NetworkManagerTests(UseSceneManagement useSceneManagement)
55+
public NetworkManagerSceneTests(UseSceneManagement useSceneManagement)
3756
{
3857
m_UseSceneManagement = useSceneManagement == UseSceneManagement.SceneManagementEnabled;
3958
}

0 commit comments

Comments
 (0)