Skip to content

Commit 689268a

Browse files
fix: g-2501 Connection disapproval results in spam logging 10 seconds later (#2514)
* fix: g-2501 Connection disapproval results in spam logging 10 seconds later * fix Minor adjustments to get this PR passing the connection approval timeout tests. * update Reverting accidental change in ProcessPendingApprovals that shouldn't have been changed. --------- Co-authored-by: NoelStephensUnity <[email protected]>
1 parent 2717686 commit 689268a

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,25 +83,58 @@ public sealed class NetworkConnectionManager
8383

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

86+
internal Coroutine LocalClientApprovalCoroutine;
87+
88+
/// <summary>
89+
/// Client-Side:
90+
/// Starts the client-side approval timeout coroutine
91+
/// </summary>
92+
/// <param name="clientId"></param>
93+
internal void StartClientApprovalCoroutine(ulong clientId)
94+
{
95+
LocalClientApprovalCoroutine = NetworkManager.StartCoroutine(ApprovalTimeout(clientId));
96+
}
97+
98+
/// <summary>
99+
/// Client-Side:
100+
/// Stops the client-side approval timeout when it is approved.
101+
/// <see cref="ConnectionApprovedMessage.Handle(ref NetworkContext)"/>
102+
/// </summary>
103+
internal void StopClientApprovalCoroutine()
104+
{
105+
if (LocalClientApprovalCoroutine != null)
106+
{
107+
NetworkManager.StopCoroutine(LocalClientApprovalCoroutine);
108+
LocalClientApprovalCoroutine = null;
109+
}
110+
}
111+
86112
/// <summary>
113+
/// Server-Side:
87114
/// Handles the issue with populating NetworkManager.PendingClients
88115
/// </summary>
89116
internal void AddPendingClient(ulong clientId)
90117
{
91118
m_PendingClients.Add(clientId, new PendingClient()
92119
{
93120
ClientId = clientId,
94-
ConnectionState = PendingClient.State.PendingConnection
121+
ConnectionState = PendingClient.State.PendingConnection,
122+
ApprovalCoroutine = NetworkManager.StartCoroutine(ApprovalTimeout(clientId))
95123
});
96124

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

100128
/// <summary>
129+
/// Server-Side:
101130
/// Handles the issue with depopulating NetworkManager.PendingClients
102131
/// </summary>
103132
internal void RemovePendingClient(ulong clientId)
104133
{
134+
if (m_PendingClients.ContainsKey(clientId) && m_PendingClients[clientId].ApprovalCoroutine != null)
135+
{
136+
NetworkManager.StopCoroutine(m_PendingClients[clientId].ApprovalCoroutine);
137+
}
105138
m_PendingClients.Remove(clientId);
106139
NetworkManager.PendingClients.Remove(clientId);
107140
}
@@ -272,8 +305,6 @@ internal void ConnectEventHandler(ulong transportClientId)
272305
}
273306

274307
AddPendingClient(clientId);
275-
276-
NetworkManager.StartCoroutine(ApprovalTimeout(clientId));
277308
}
278309
else
279310
{
@@ -283,7 +314,7 @@ internal void ConnectEventHandler(ulong transportClientId)
283314
}
284315

285316
SendConnectionRequest();
286-
NetworkManager.StartCoroutine(ApprovalTimeout(clientId));
317+
StartClientApprovalCoroutine(clientId);
287318
}
288319

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

551582
var client = AddClient(ownerClientId);
@@ -818,6 +849,8 @@ internal void OnClientDisconnectFromServer(ulong clientId)
818849
}
819850

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

823856
// Handle cleaning up the server-side client send queue

com.unity.netcode.gameobjects/Runtime/Connection/PendingClient.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
using UnityEngine;
2+
13
namespace Unity.Netcode
24
{
35
/// <summary>
6+
/// Server-Side Only:
47
/// A class representing a client that is currently in the process of connecting
58
/// </summary>
69
public class PendingClient
710
{
11+
internal Coroutine ApprovalCoroutine = null;
12+
813
/// <summary>
914
/// The ClientId of the client
1015
/// </summary>

com.unity.netcode.gameobjects/Runtime/Messaging/Messages/ConnectionApprovedMessage.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ public void Handle(ref NetworkContext context)
120120
networkManager.ConnectionManager.LocalClient.SetRole(false, true, networkManager);
121121
networkManager.ConnectionManager.LocalClient.IsApproved = true;
122122
networkManager.ConnectionManager.LocalClient.ClientId = OwnerClientId;
123+
// Stop the client-side approval timeout coroutine since we are approved.
124+
networkManager.ConnectionManager.StopClientApprovalCoroutine();
123125

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

0 commit comments

Comments
 (0)