Skip to content

feat: [Backport] Expose methods for ClientId to TransportId mappings #3515

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

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Additional documentation and release notes are available at [Multiplayer Documen

### Added

- Added mappings between `ClientId` and `TransportId`. (#3515)
- Added `SinglePlayerTransport` that provides the ability to start as a host for a single player network session. (#3475)
- 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)

Expand Down
16 changes: 15 additions & 1 deletion com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,20 @@ private void HostServerInitialize()
ConnectionManager.InvokeOnClientConnectedCallback(LocalClientId);
}

/// <summary>
/// Get the TransportId from the associated ClientId.
/// </summary>
/// <param name="clientId">The ClientId to get the TransportId from</param>
/// <returns>The TransportId associated with the given ClientId</returns>
public ulong GetTransportIdFromClientId(ulong clientId) => ConnectionManager.ClientIdToTransportId(clientId);

/// <summary>
/// Get the ClientId from the associated TransportId.
/// </summary>
/// <param name="transportId">The TransportId to get the ClientId from</param>
/// <returns>The ClientId from the associated TransportId</returns>
public ulong GetClientIdFromTransportId(ulong transportId) => ConnectionManager.TransportIdToClientId(transportId);

/// <summary>
/// Disconnects the remote client.
/// </summary>
Expand Down Expand Up @@ -1305,7 +1319,7 @@ private void OnDestroy()
}
#if UNITY_EDITOR
EditorApplication.playModeStateChanged -= ModeChanged;
#endif
#endif
}

// Command line options
Expand Down
23 changes: 21 additions & 2 deletions testproject/Assets/Tests/Runtime/NetworkManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,28 @@

namespace TestProject.RuntimeTests
{
public class NetworkManagerTests : NetcodeIntegrationTest
{
protected override int NumberOfClients => 2;

[Test]
public void ValidateTransportAndClientIds()
{
var transportId = m_ServerNetworkManager.GetTransportIdFromClientId(m_ServerNetworkManager.LocalClientId);
Assert.IsTrue(m_ServerNetworkManager.GetTransportIdFromClientId(m_ServerNetworkManager.LocalClientId) == m_ServerNetworkManager.ConnectionManager.ServerTransportId);
Assert.IsTrue(m_ServerNetworkManager.GetClientIdFromTransportId(transportId) == m_ServerNetworkManager.LocalClientId);

foreach (var client in m_ClientNetworkManagers)
{
transportId = m_ServerNetworkManager.GetTransportIdFromClientId(client.LocalClientId);
Assert.AreEqual(client.LocalClientId, m_ServerNetworkManager.GetClientIdFromTransportId(transportId), "Server and client transport IDs don't match.");
}
}
}

[TestFixture(UseSceneManagement.SceneManagementDisabled)]
[TestFixture(UseSceneManagement.SceneManagementEnabled)]
public class NetworkManagerTests : NetcodeIntegrationTest
public class NetworkManagerSceneTests : NetcodeIntegrationTest
{
private const string k_SceneToLoad = "InSceneNetworkObject";
protected override int NumberOfClients => 0;
Expand All @@ -33,7 +52,7 @@ public enum UseSceneManagement

private bool m_UseSceneManagement;

public NetworkManagerTests(UseSceneManagement useSceneManagement)
public NetworkManagerSceneTests(UseSceneManagement useSceneManagement)
{
m_UseSceneManagement = useSceneManagement == UseSceneManagement.SceneManagementEnabled;
}
Expand Down