Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -14,6 +14,7 @@ Additional documentation and release notes are available at [Multiplayer Documen

### Fixed

- Fixed issue where the `OnClientDisconnected` client identifier was incorrect after a pending client connection was denied. (#2569)
- Fixed warning "Runtime Network Prefabs was not empty at initialization time." being erroneously logged when no runtime network prefabs had been added (#2565)
- Fixed issue where some temporary debug console logging was left in a merged PR. (#2562)
- Fixed the "Generate Default Network Prefabs List" setting not loading correctly and always reverting to being checked. (#2545)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,16 @@ internal void OnClientDisconnectFromServer(ulong clientId)
{
var transportId = ClientIdToTransportId(clientId);
NetworkManager.NetworkConfig.NetworkTransport.DisconnectRemoteClient(transportId);

try
{
OnClientDisconnectCallback?.Invoke(clientId);
Comment thread
NoelStephensUnity marked this conversation as resolved.
}
catch (Exception exception)
{
Debug.LogException(exception);
}

// Clean up the transport to client (and vice versa) mappings
TransportIdCleanUp(transportId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,18 @@ protected virtual void OnNewClientStartedAndConnected(NetworkManager networkMana
{
}

/// <summary>
/// CreateAndStartNewClient Only
/// Override this method to bypass the waiting for a client to connect.
/// </summary>
/// <remarks>
/// Use this for testing connection and disconnection scenarios
/// </remarks>
protected virtual bool WaitForNewClientToConnect(NetworkManager networkManager)
Comment thread
NoelStephensUnity marked this conversation as resolved.
Outdated
{
return true;
}

/// <summary>
/// This will create, start, and connect a new client while in the middle of an
/// integration test.
Expand All @@ -455,19 +467,22 @@ protected IEnumerator CreateAndStartNewClient()

OnNewClientStarted(networkManager);

// Wait for the new client to connect
yield return WaitForClientsConnectedOrTimeOut();

OnNewClientStartedAndConnected(networkManager);
if (s_GlobalTimeoutHelper.TimedOut)
if (WaitForNewClientToConnect(networkManager))
{
AddRemoveNetworkManager(networkManager, false);
Object.DestroyImmediate(networkManager.gameObject);
}
// Wait for the new client to connect
yield return WaitForClientsConnectedOrTimeOut();

AssertOnTimeout($"{nameof(CreateAndStartNewClient)} timed out waiting for the new client to be connected!");
ClientNetworkManagerPostStart(networkManager);
VerboseDebug($"[{networkManager.name}] Created and connected!");
OnNewClientStartedAndConnected(networkManager);
if (s_GlobalTimeoutHelper.TimedOut)
{
AddRemoveNetworkManager(networkManager, false);
Object.DestroyImmediate(networkManager.gameObject);
}

AssertOnTimeout($"{nameof(CreateAndStartNewClient)} timed out waiting for the new client to be connected!");
ClientNetworkManagerPostStart(networkManager);
VerboseDebug($"[{networkManager.name}] Created and connected!");
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Collections;
using Unity.Netcode.TestHelpers.Runtime;
using UnityEngine.TestTools;


namespace Unity.Netcode.RuntimeTests
{
public class ClientApprovalDenied : NetcodeIntegrationTest
{
protected override int NumberOfClients => 0;
private bool m_ApproveConnection = true;
private ulong m_PendingClientId = 0;
private ulong m_DisconnectedClientId = 0;

protected override void OnServerAndClientsCreated()
{
m_ServerNetworkManager.NetworkConfig.ConnectionApproval = true;
m_ServerNetworkManager.ConnectionApprovalCallback = ConnectionApproval;
}

private void ConnectionApproval(NetworkManager.ConnectionApprovalRequest connectionApprovalRequest, NetworkManager.ConnectionApprovalResponse connectionApprovalResponse)
{
connectionApprovalResponse.Approved = m_ApproveConnection;
connectionApprovalResponse.CreatePlayerObject = true;
// When denied, store the client identifier to use for validating the client disconnected notification identifier matches
if (!m_ApproveConnection)
{
m_PendingClientId = connectionApprovalRequest.ClientNetworkId;
}
}

protected override void OnNewClientCreated(NetworkManager networkManager)
{
networkManager.NetworkConfig.ConnectionApproval = true;
base.OnNewClientCreated(networkManager);
}

protected override bool WaitForNewClientToConnect(NetworkManager networkManager)
{
return false;
}

/// <summary>
/// Validates that when a pending client is denied approval the server-host
/// OnClientDisconnected method will return the valid pending client identifier.
/// </summary>
[UnityTest]
public IEnumerator ClientApprovalDeniedNotificationTest()
{
m_ApproveConnection = false;
m_ServerNetworkManager.OnClientDisconnectCallback += OnClientDisconnectCallback;
yield return CreateAndStartNewClient();
m_ServerNetworkManager.OnClientDisconnectCallback -= OnClientDisconnectCallback;

yield return WaitForConditionOrTimeOut(() => m_PendingClientId == m_DisconnectedClientId);
AssertOnTimeout($"Timed out waiting for disconnect notification for pending Client-{m_PendingClientId}!");
}

private void OnClientDisconnectCallback(ulong clientId)
{
m_DisconnectedClientId = clientId;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.