Skip to content
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

fix: provide users ability to get client endpoint #3130

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
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. (#3130)
- Added `NetworkTransport.OnEarlyUpdate` and `NetworkTransport.OnPostLateUpdate` methods to provide more control over handling transport related events at the start and end of each frame. (#3113)

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,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 && m_NetworkManager != null && m_NetworkManager.IsListening)
{
var transportId = m_NetworkManager.ConnectionManager.ClientIdToTransportId(clientId);
var networkConnection = ParseClientId(transportId);
if (m_Driver.GetConnectionState(networkConnection) == NetworkConnection.State.Connected)
{
return m_Driver.GetRemoteEndpoint(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,49 @@ 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
Loading