Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,58 @@ public sealed class NetworkConnectionManager

internal IReadOnlyDictionary<ulong, PendingClient> PendingClients => m_PendingClients;

internal Coroutine LocalClientApprovalCoroutine;

/// <summary>
/// Client-Side:
/// Starts the client-side approval timeout coroutine
/// </summary>
/// <param name="clientId"></param>
internal void StartClientApprovalCoroutine(ulong clientId)
{
LocalClientApprovalCoroutine = NetworkManager.StartCoroutine(ApprovalTimeout(clientId));
}

/// <summary>
/// Client-Side:
/// Stops the client-side approval timeout when it is approved.
/// <see cref="ConnectionApprovedMessage.Handle(ref NetworkContext)"/>
/// </summary>
internal void StopClientApprovalCoroutine()
{
if (LocalClientApprovalCoroutine != null)
{
NetworkManager.StopCoroutine(LocalClientApprovalCoroutine);
LocalClientApprovalCoroutine = null;
}
}

/// <summary>
/// Server-Side:
/// Handles the issue with populating NetworkManager.PendingClients
/// </summary>
internal void AddPendingClient(ulong clientId)
{
m_PendingClients.Add(clientId, new PendingClient()
{
ClientId = clientId,
ConnectionState = PendingClient.State.PendingConnection
ConnectionState = PendingClient.State.PendingConnection,
ApprovalCoroutine = NetworkManager.StartCoroutine(ApprovalTimeout(clientId))
});

NetworkManager.PendingClients.Add(clientId, PendingClients[clientId]);
}

/// <summary>
/// Server-Side:
/// Handles the issue with depopulating NetworkManager.PendingClients
/// </summary>
internal void RemovePendingClient(ulong clientId)
{
if (m_PendingClients.ContainsKey(clientId) && m_PendingClients[clientId].ApprovalCoroutine != null)
{
NetworkManager.StopCoroutine(m_PendingClients[clientId].ApprovalCoroutine);
}
m_PendingClients.Remove(clientId);
NetworkManager.PendingClients.Remove(clientId);
}
Expand Down Expand Up @@ -272,8 +305,6 @@ internal void ConnectEventHandler(ulong transportClientId)
}

AddPendingClient(clientId);

NetworkManager.StartCoroutine(ApprovalTimeout(clientId));
}
else
{
Expand All @@ -283,7 +314,7 @@ internal void ConnectEventHandler(ulong transportClientId)
}

SendConnectionRequest();
NetworkManager.StartCoroutine(ApprovalTimeout(clientId));
StartClientApprovalCoroutine(clientId);
}

#if DEVELOPMENT_BUILD || UNITY_EDITOR
Expand Down Expand Up @@ -545,7 +576,7 @@ internal void HandleConnectionApproval(ulong ownerClientId, NetworkManager.Conne
LocalClient.IsApproved = response.Approved;
if (response.Approved)
{
// Inform new client it got approved
// The client was approved, stop the server-side approval time out coroutine
RemovePendingClient(ownerClientId);

var client = AddClient(ownerClientId);
Expand Down Expand Up @@ -818,6 +849,8 @@ internal void OnClientDisconnectFromServer(ulong clientId)
}

// Assure the client id is no longer in the pending clients list
// and stop the server-side client approval timeout since the client
// is no longer connected.
RemovePendingClient(clientId);

// Handle cleaning up the server-side client send queue
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
using UnityEngine;

namespace Unity.Netcode
{
/// <summary>
/// Server-Side Only:
/// A class representing a client that is currently in the process of connecting
/// </summary>
public class PendingClient
{
internal Coroutine ApprovalCoroutine = null;

/// <summary>
/// The ClientId of the client
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ public void Handle(ref NetworkContext context)
networkManager.ConnectionManager.LocalClient.SetRole(false, true, networkManager);
networkManager.ConnectionManager.LocalClient.IsApproved = true;
networkManager.ConnectionManager.LocalClient.ClientId = OwnerClientId;
// Stop the client-side approval timeout coroutine since we are approved.
networkManager.ConnectionManager.StopClientApprovalCoroutine();

// Only if scene management is disabled do we handle NetworkObject synchronization at this point
if (!networkManager.NetworkConfig.EnableSceneManagement)
Expand Down