Skip to content

Commit

Permalink
fix: provide users ability to get client endpoint backport (#3131)
Browse files Browse the repository at this point in the history
* fix

Adding method so users can obtain endpoint information about a client's connection.

* test

Validation test for the GetEndpoint method to assure it returns valid  endpoint information.

* fix

Fixing missed issue with NetworkManagerTests

* update

adding changelog entry.

* style

removing white spaces
  • Loading branch information
NoelStephensUnity authored Nov 19, 2024
1 parent 0c32e93 commit dcaeef9
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
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 `UnityTransport.GetEndpoint` method to provide a way to obtain `NetworkEndpoint` information of a connection via client identifier. (#3131)
- Added a static `NetworkManager.OnInstantiated` event notification to be able to track when a new `NetworkManager` instance has been instantiated. (#3089)
- Added a static `NetworkManager.OnDestroying` event notification to be able to track when an existing `NetworkManager` instance is being destroyed. (#3089)
- Added message size validation to named and unnamed message sending functions for better error messages. (#3043)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,30 @@ public override ulong GetCurrentRtt(ulong clientId)
return (ulong)ExtractRtt(ParseClientId(clientId));
}

/// <summary>
/// Provides the <see cref="NetworkEndpoint"/> for the NGO client identifier specified.
/// </summary>
/// <remarks>
/// - This is only really useful for direct connections.
/// - Relay connections and clients connected using a distributed authority network topology will not provide the client's actual endpoint information.
/// - For LAN topologies this should work as long as it is a direct connection and not a relay connection.
/// </remarks>
/// <param name="clientId">NGO client identifier to get endpoint information about.</param>
/// <returns><see cref="NetworkEndpoint"/></returns>
public NetworkEndpoint GetEndpoint(ulong clientId)
{
if (m_Driver.IsCreated && NetworkManager != null && NetworkManager.IsListening)
{
var transportId = NetworkManager.ConnectionManager.ClientIdToTransportId(clientId);
var networkConnection = ParseClientId(transportId);
if (m_Driver.GetConnectionState(networkConnection) == NetworkConnection.State.Connected)
{
return m_Driver.RemoteEndPoint(networkConnection);
}
}
return new NetworkEndpoint();
}

/// <summary>
/// Initializes the transport
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections;
using NUnit.Framework;
using Unity.Netcode.TestHelpers.Runtime;
using Unity.Netcode.Transports.UTP;
using Unity.Networking.Transport;
using UnityEngine;
using UnityEngine.TestTools;

Expand Down Expand Up @@ -157,4 +160,51 @@ public override void DisconnectLocalClient()
}
}
}

/// <summary>
/// Verifies the UnityTransport.GetEndpoint method returns
/// valid NetworkEndPoint information.
/// </summary>
internal class TransportEndpointTests : NetcodeIntegrationTest
{
protected override int NumberOfClients => 2;

[UnityTest]
public IEnumerator GetEndpointReportedCorrectly()
{
var serverUnityTransport = m_ServerNetworkManager.NetworkConfig.NetworkTransport as UnityTransport;
var serverEndpoint = new NetworkEndPoint();
var clientEndpoint = new NetworkEndPoint();
foreach (var client in m_ClientNetworkManagers)
{
var unityTransport = client.NetworkConfig.NetworkTransport as UnityTransport;
serverEndpoint = unityTransport.GetEndpoint(m_ServerNetworkManager.LocalClientId);
clientEndpoint = serverUnityTransport.GetEndpoint(client.LocalClientId);
Assert.IsTrue(serverEndpoint.IsValid);
Assert.IsTrue(clientEndpoint.IsValid);
Assert.IsTrue(clientEndpoint.Address.Split(":")[0] == unityTransport.ConnectionData.Address);
Assert.IsTrue(serverEndpoint.Address.Split(":")[0] == serverUnityTransport.ConnectionData.Address);
Assert.IsTrue(serverEndpoint.Port == unityTransport.ConnectionData.Port);
Assert.IsTrue(clientEndpoint.Port >= serverUnityTransport.ConnectionData.Port);
}

// Now validate that when disconnected it returns a non-valid NetworkEndPoint
var clientId = m_ClientNetworkManagers[0].LocalClientId;
m_ClientNetworkManagers[0].Shutdown();
yield return s_DefaultWaitForTick;

serverEndpoint = (m_ClientNetworkManagers[0].NetworkConfig.NetworkTransport as UnityTransport).GetEndpoint(m_ServerNetworkManager.LocalClientId);
clientEndpoint = serverUnityTransport.GetEndpoint(clientId);
Assert.IsFalse(serverEndpoint.IsValid);
Assert.IsFalse(clientEndpoint.IsValid);

// Validate that invalid client identifiers return an invalid NetworkEndPoint
serverEndpoint = (m_ClientNetworkManagers[0].NetworkConfig.NetworkTransport as UnityTransport).GetEndpoint((ulong)UnityEngine.Random.Range(NumberOfClients + 1, 30));
clientEndpoint = serverUnityTransport.GetEndpoint((ulong)UnityEngine.Random.Range(NumberOfClients + 1, 30));
Assert.IsFalse(serverEndpoint.IsValid);
Assert.IsFalse(clientEndpoint.IsValid);
}
}


}
5 changes: 2 additions & 3 deletions testproject/Assets/Tests/Runtime/NetworkManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public enum UseSceneManagement
SceneManagementDisabled
}

private bool m_EnableSceneManagement;
private NetworkObject m_NetworkObject;
private bool m_NetworkObjectWasSpawned;
private bool m_NetworkBehaviourIsHostWasSet;
Expand Down Expand Up @@ -85,7 +84,7 @@ protected override IEnumerator OnTearDown()

protected override void OnServerAndClientsCreated()
{
m_ServerNetworkManager.NetworkConfig.EnableSceneManagement = m_EnableSceneManagement;
m_ServerNetworkManager.NetworkConfig.EnableSceneManagement = m_UseSceneManagement;
m_NetworkObjectTestComponent.ConfigureClientConnected(m_ServerNetworkManager, OnClientConnectedCallback);
}

Expand All @@ -108,7 +107,7 @@ public enum ShutdownChecks

protected override void OnNewClientCreated(NetworkManager networkManager)
{
networkManager.NetworkConfig.EnableSceneManagement = m_EnableSceneManagement;
networkManager.NetworkConfig.EnableSceneManagement = m_UseSceneManagement;
foreach (var prefab in m_ServerNetworkManager.NetworkConfig.Prefabs.Prefabs)
{
networkManager.NetworkConfig.Prefabs.Add(prefab);
Expand Down

0 comments on commit dcaeef9

Please sign in to comment.