fix: provide users ability to get client endpoint backport (#3131)

* 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
This commit is contained in:
Noel Stephens 2024-11-19 15:43:02 -06:00 коммит произвёл GitHub
Родитель 0c32e93b1c
Коммит dcaeef9ab7
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
4 изменённых файлов: 77 добавлений и 3 удалений

Просмотреть файл

@ -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)

Просмотреть файл

@ -1209,6 +1209,30 @@ namespace Unity.Netcode.Transports.UTP
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>

Просмотреть файл

@ -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;
@ -157,4 +160,51 @@ namespace Unity.Netcode.RuntimeTests
}
}
}
/// <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);
}
}
}

Просмотреть файл

@ -22,7 +22,6 @@ namespace TestProject.RuntimeTests
SceneManagementDisabled
}
private bool m_EnableSceneManagement;
private NetworkObject m_NetworkObject;
private bool m_NetworkObjectWasSpawned;
private bool m_NetworkBehaviourIsHostWasSet;
@ -85,7 +84,7 @@ namespace TestProject.RuntimeTests
protected override void OnServerAndClientsCreated()
{
m_ServerNetworkManager.NetworkConfig.EnableSceneManagement = m_EnableSceneManagement;
m_ServerNetworkManager.NetworkConfig.EnableSceneManagement = m_UseSceneManagement;
m_NetworkObjectTestComponent.ConfigureClientConnected(m_ServerNetworkManager, OnClientConnectedCallback);
}
@ -108,7 +107,7 @@ namespace TestProject.RuntimeTests
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);