Skip to content

Commit acdfdcf

Browse files
update
Making adjustments based on peer review of this branch's PR.
1 parent 97c2c7a commit acdfdcf

File tree

3 files changed

+123
-129
lines changed

3 files changed

+123
-129
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,7 +1459,6 @@ internal void Initialize(NetworkManager networkManager)
14591459
Transport.NetworkMetrics = NetworkManager.MetricsManager.NetworkMetrics;
14601460
Transport.OnTransportEvent += HandleNetworkEvent;
14611461
Transport.Initialize(networkManager);
1462-
Transport.CreateDisconnectEventMap();
14631462
}
14641463
}
14651464

@@ -1470,9 +1469,9 @@ internal void Shutdown()
14701469
{
14711470
if (Transport && IsListening)
14721471
{
1472+
Transport.ShuttingDown();
14731473
var clientId = NetworkManager ? NetworkManager.LocalClientId : NetworkManager.ServerClientId;
14741474
var transportId = ClientIdToTransportId(clientId);
1475-
Transport.SetDisconnectEvent(NetworkTransport.DisconnectEvents.TransportShutdown);
14761475
GenerateDisconnectInformation(clientId, transportId, $"{nameof(NetworkConnectionManager)} was shutdown.");
14771476
}
14781477

@@ -1526,7 +1525,6 @@ internal void Shutdown()
15261525
try
15271526
{
15281527
Transport?.DisconnectLocalClient();
1529-
Transport?.CleanDisconnectEventMap();
15301528
}
15311529
catch (Exception ex)
15321530
{
@@ -1556,7 +1554,6 @@ internal void Shutdown()
15561554
var transport = NetworkManager.NetworkConfig?.NetworkTransport;
15571555
if (transport != null)
15581556
{
1559-
transport.CleanDisconnectEventMap();
15601557
transport.Shutdown();
15611558
if (NetworkManager.LogLevel <= LogLevel.Developer)
15621559
{

com.unity.netcode.gameobjects/Runtime/Transports/NetworkTransport.cs

Lines changed: 22 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using UnityEngine;
43

54
namespace Unity.Netcode
@@ -172,7 +171,7 @@ internal NetworkTopologyTypes CurrentTopology()
172171
/// <remarks>
173172
/// <see cref="AddDisconnectEventMap"/> provides you with the ability to register the transport's disconnect event types with the local equivalent.
174173
/// </remarks>
175-
public enum DisconnectEvents : byte
174+
public enum DisconnectEvents
176175
{
177176
/// <summary>
178177
/// If transport has mapped its disconnect events, this event signifies that the transport closed the connection due to a locally invoked shutdown.
@@ -219,98 +218,52 @@ public enum DisconnectEvents : byte
219218
/// </summary>
220219
public string DisconnectEventMessage { get; private set; }
221220

222-
private Dictionary<byte, DisconnectEvents> m_DisconnectEventMap = new Dictionary<byte, DisconnectEvents>();
223-
private Dictionary<DisconnectEvents, string> m_DisconnectEventMessageMap = new Dictionary<DisconnectEvents, string>();
224-
225221
/// <summary>
226222
/// This should be invoked by the <see cref="NetworkTransport"/> derived class when a transport level disconnect event occurs.<br />
227-
/// If there is a map for the specific transport event id, then <see cref="DisconnectEvent"/> will be set to the equivalent <see cref="DisconnectEvents"/> value.
223+
/// It is up to the <see cref="NetworkTransport"/> derived class to create a map between the transport's disconnect events and the
224+
/// pre-defined <see cref="DisconnectEvents"/> enum values.
228225
/// </summary>
229-
/// <param name="disconnectEventId">The transport's disconnect event identifer.</param>
230-
/// <param name="messageOverride">Optional message to override any existing registered one for the transport specific disconnection event identifier.</param>
231-
public void SetDisconnectEvent(byte disconnectEventId, string messageOverride = null)
232-
{
233-
if (m_DisconnectEventMap.ContainsKey(disconnectEventId))
234-
{
235-
DisconnectEvent = m_DisconnectEventMap[disconnectEventId];
236-
}
237-
else // If there are no maps, then this will always report just disconnected.
238-
{
239-
DisconnectEvent = DisconnectEvents.Disconnected;
240-
}
241-
DisconnectEventMessage = string.Empty;
242-
if (messageOverride != null)
243-
{
244-
DisconnectEventMessage = messageOverride;
245-
}
246-
else if (m_DisconnectEventMessageMap.ContainsKey(DisconnectEvent))
247-
{
248-
DisconnectEventMessage = m_DisconnectEventMessageMap[DisconnectEvent];
249-
}
250-
}
251-
252-
internal void SetDisconnectEvent(DisconnectEvents disconnectEvent, string message = null)
226+
/// <param name="disconnectEvent">The <see cref="DisconnectEvents"/> type to set.</param>
227+
/// <param name="message">An optional message override.</param>
228+
protected void SetDisconnectEvent(DisconnectEvents disconnectEvent, string message = null)
253229
{
254230
DisconnectEvent = disconnectEvent;
255231
DisconnectEventMessage = string.Empty;
232+
256233
if (message != null)
257234
{
258235
DisconnectEventMessage = message;
259236
}
260-
else if (m_DisconnectEventMessageMap.ContainsKey(disconnectEvent))
237+
else
261238
{
262-
DisconnectEventMessage = m_DisconnectEventMessageMap[disconnectEvent];
239+
DisconnectEventMessage = OnGetDisconnectEventMessage(disconnectEvent);
263240
}
264241
}
265242

266243
/// <summary>
267-
/// Adds a map between the <see cref="DisconnectEvents"/> value and the transports equivalent event identifier.
244+
/// Override this method to provide additional information about the disconnection event.
268245
/// </summary>
269-
/// <param name="disconnectEvents">The <see cref="DisconnectEvents"/> value to be mapped.</param>
270-
/// <param name="targetEventId">The transport's equivalent event identifier value.</param>
271-
/// <param name="message">Optional message to use for this disconnect event.</param>
272-
protected void AddDisconnectEventMap(DisconnectEvents disconnectEvents, byte targetEventId, string message = null)
246+
/// <param name="disconnectEvent">The disconnect event to get from the <see cref="NetworkTransport"/> derived class.</param>
247+
/// <returns><see cref="string.Empty"/> as a default or if overridden the <see cref="string"/> returned.</returns>
248+
protected virtual string OnGetDisconnectEventMessage(DisconnectEvents disconnectEvent)
273249
{
274-
if (!m_DisconnectEventMap.ContainsKey(targetEventId))
275-
{
276-
m_DisconnectEventMap.Add(targetEventId, disconnectEvents);
277-
}
278-
279-
if (message != null && !m_DisconnectEventMessageMap.ContainsKey(disconnectEvents))
280-
{
281-
m_DisconnectEventMessageMap.Add(disconnectEvents, message);
282-
}
250+
return string.Empty;
283251
}
284252

285253
/// <summary>
286-
/// Override this method to create a disconnect event mapping table that will translate the transport's equivalent for each enum in <see cref="DisconnectEvents"/>.<br />
287-
/// This method is invoked during <see cref="NetworkConnectionManager.Initialize(NetworkManager)"/> just after <see cref="Initialize(NetworkManager)"/> has been invoked.
254+
/// Invoked when the local <see cref="NetworkManager"/> forces the transport to close a remote connection.
288255
/// </summary>
289-
/// <remarks>
290-
/// You can use <see cref="AddDisconnectEventMap"/> to register a map between <see cref="DisconnectEvents"/> and the transport's disconnect event equivalent.
291-
/// </remarks>
292-
protected virtual void OnCreateDisconnectEventMap()
293-
{
294-
295-
}
296-
297-
internal void CreateDisconnectEventMap()
298-
{
299-
DisconnectEvent = DisconnectEvents.Disconnected;
300-
DisconnectEventMessage = string.Empty;
301-
OnCreateDisconnectEventMap();
302-
}
303-
304-
internal void CleanDisconnectEventMap()
256+
internal void ClosingRemoteConnection()
305257
{
306-
DisconnectEvent = DisconnectEvents.Disconnected;
307-
m_DisconnectEventMap.Clear();
308-
m_DisconnectEventMessageMap.Clear();
258+
SetDisconnectEvent(DisconnectEvents.ClosedRemoteConnection);
309259
}
310260

311-
internal void ClosingRemoteConnection()
261+
/// <summary>
262+
/// Invoked just before the transport is shutdown.
263+
/// </summary>
264+
internal void ShuttingDown()
312265
{
313-
SetDisconnectEvent(DisconnectEvents.ClosedRemoteConnection);
266+
SetDisconnectEvent(DisconnectEvents.TransportShutdown);
314267
}
315268
}
316269

com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs

Lines changed: 100 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,8 @@ private bool ProcessEvent()
10581058
}
10591059

10601060
// Set the disconnect event error code
1061-
SetDisconnectEvent(reader.ReadByte());
1061+
var errorCode = m_UnityTransportNotificationHandler.GetDisconnectEvent(reader.ReadByte());
1062+
SetDisconnectEvent(errorCode);
10621063

10631064
m_ServerClientId = default;
10641065
m_ReliableReceiveQueues.Remove(clientId);
@@ -1369,37 +1370,6 @@ public override void DisconnectRemoteClient(ulong clientId)
13691370
}
13701371
}
13711372

1372-
private const byte k_ClosedRemoteConnection = 128;
1373-
private const byte k_TransportShutdown = 129;
1374-
1375-
internal const string DisconnectedMessage = "Gracefully disconnected.";
1376-
internal const string TimeoutMessage = "Connection closed due to timed out.";
1377-
internal const string MaxConnectionAttemptsMessage = "Connection closed due to maximum connection attempts reached.";
1378-
internal const string ClosedByRemoteMessage = "Connection was closed by remote endpoint.";
1379-
internal const string AuthenticationFailureMessage = "Connection closed due to authentication failure.";
1380-
internal const string ProtocolErrorMessage = "Gracefully disconnected.";
1381-
internal const string ClosedRemoteConnectionMessage = "Local transport closed the remote endpoint connection.";
1382-
internal const string TransportShutdownMessage = "The transport was shutdown.";
1383-
1384-
/// <inheritdoc/>
1385-
protected override void OnCreateDisconnectEventMap()
1386-
{
1387-
// Implemented in UTP
1388-
1389-
AddDisconnectEventMap(DisconnectEvents.Disconnected, (byte)DisconnectReason.Default, DisconnectedMessage);
1390-
AddDisconnectEventMap(DisconnectEvents.ProtocolTimeout, (byte)DisconnectReason.Timeout, TimeoutMessage);
1391-
AddDisconnectEventMap(DisconnectEvents.MaxConnectionAttempts, (byte)DisconnectReason.MaxConnectionAttempts, MaxConnectionAttemptsMessage);
1392-
AddDisconnectEventMap(DisconnectEvents.ClosedByRemote, (byte)DisconnectReason.ClosedByRemote, ClosedByRemoteMessage);
1393-
AddDisconnectEventMap(DisconnectEvents.AuthenticationFailure, (byte)DisconnectReason.AuthenticationFailure, AuthenticationFailureMessage);
1394-
AddDisconnectEventMap(DisconnectEvents.ProtocolError, (byte)DisconnectReason.ProtocolError, ProtocolErrorMessage);
1395-
1396-
// Not implemented in UTP
1397-
AddDisconnectEventMap(DisconnectEvents.ClosedRemoteConnection, k_ClosedRemoteConnection, ClosedRemoteConnectionMessage);
1398-
AddDisconnectEventMap(DisconnectEvents.TransportShutdown, k_TransportShutdown, TransportShutdownMessage);
1399-
1400-
base.OnCreateDisconnectEventMap();
1401-
}
1402-
14031373
/// <summary>
14041374
/// Gets the current RTT for a specific client
14051375
/// </summary>
@@ -1449,30 +1419,6 @@ public NetworkEndpoint GetEndpoint(ulong clientId)
14491419
return new NetworkEndpoint();
14501420
}
14511421

1452-
/// <summary>
1453-
/// Initializes the transport
1454-
/// </summary>
1455-
/// <param name="networkManager">The NetworkManager that initialized and owns the transport</param>
1456-
public override void Initialize(NetworkManager networkManager = null)
1457-
{
1458-
#if DEBUG
1459-
if (sizeof(ulong) != UnsafeUtility.SizeOf<NetworkConnection>())
1460-
{
1461-
Debug.LogWarning($"Netcode connection id size {sizeof(ulong)} does not match UTP connection id size {UnsafeUtility.SizeOf<NetworkConnection>()}!");
1462-
return;
1463-
}
1464-
#endif
1465-
1466-
m_NetworkManager = networkManager;
1467-
1468-
if (m_NetworkManager && m_NetworkManager.PortOverride.Overidden)
1469-
{
1470-
ConnectionData.Port = m_NetworkManager.PortOverride.Value;
1471-
}
1472-
1473-
m_RealTimeProvider = m_NetworkManager ? m_NetworkManager.RealTimeProvider : new RealTimeProvider();
1474-
}
1475-
14761422
/// <summary>
14771423
/// Polls for incoming events, with an extra output parameter to report the precise time the event was received.
14781424
/// </summary>
@@ -1626,6 +1572,39 @@ public override bool StartServer()
16261572
return succeeded;
16271573
}
16281574

1575+
private UnityTransportNotificationHandler m_UnityTransportNotificationHandler;
1576+
1577+
/// <inheritdoc/>
1578+
protected override string OnGetDisconnectEventMessage(DisconnectEvents disconnectEvent)
1579+
{
1580+
return m_UnityTransportNotificationHandler.GetDisconnectEventMessage(disconnectEvent);
1581+
}
1582+
1583+
/// <summary>
1584+
/// Initializes the transport
1585+
/// </summary>
1586+
/// <param name="networkManager">The NetworkManager that initialized and owns the transport</param>
1587+
public override void Initialize(NetworkManager networkManager = null)
1588+
{
1589+
#if DEBUG
1590+
if (sizeof(ulong) != UnsafeUtility.SizeOf<NetworkConnection>())
1591+
{
1592+
Debug.LogWarning($"Netcode connection id size {sizeof(ulong)} does not match UTP connection id size {UnsafeUtility.SizeOf<NetworkConnection>()}!");
1593+
return;
1594+
}
1595+
#endif
1596+
1597+
m_NetworkManager = networkManager;
1598+
1599+
if (m_NetworkManager && m_NetworkManager.PortOverride.Overidden)
1600+
{
1601+
ConnectionData.Port = m_NetworkManager.PortOverride.Value;
1602+
}
1603+
1604+
m_RealTimeProvider = m_NetworkManager ? m_NetworkManager.RealTimeProvider : new RealTimeProvider();
1605+
m_UnityTransportNotificationHandler = new UnityTransportNotificationHandler();
1606+
}
1607+
16291608
/// <summary>
16301609
/// Shuts down the transport
16311610
/// </summary>
@@ -1663,6 +1642,8 @@ public override void Shutdown()
16631642

16641643
// We must reset this to zero because UTP actually re-uses clientIds if there is a clean disconnect
16651644
m_ServerClientId = default;
1645+
1646+
m_UnityTransportNotificationHandler = null;
16661647
}
16671648

16681649
/// <inheritdoc cref="NetworkTransport.OnCurrentTopology"/>
@@ -1849,4 +1830,67 @@ internal static FixedString128Bytes ErrorToFixedString(int error)
18491830
}
18501831
}
18511832
}
1833+
1834+
/// <summary>
1835+
/// Handles mapping <see cref="DisconnectReason"/> to <see cref="NetworkTransport.DisconnectEvents"/> as well
1836+
/// as mapping additional disconnect event message information to each <see cref="NetworkTransport.DisconnectEvents"/>.
1837+
/// </summary>
1838+
internal class UnityTransportNotificationHandler
1839+
{
1840+
private const int k_ClosedRemoteConnection = 128;
1841+
private const int k_TransportShutdown = 129;
1842+
1843+
internal const string DisconnectedMessage = "Gracefully disconnected.";
1844+
internal const string TimeoutMessage = "Connection closed due to timed out.";
1845+
internal const string MaxConnectionAttemptsMessage = "Connection closed due to maximum connection attempts reached.";
1846+
internal const string ClosedByRemoteMessage = "Connection was closed by remote endpoint.";
1847+
internal const string AuthenticationFailureMessage = "Connection closed due to authentication failure.";
1848+
internal const string ProtocolErrorMessage = "Gracefully disconnected.";
1849+
internal const string ClosedRemoteConnectionMessage = "Local transport closed the remote endpoint connection.";
1850+
internal const string TransportShutdownMessage = "The transport was shutdown.";
1851+
1852+
private Dictionary<int, NetworkTransport.DisconnectEvents> m_DisconnectEventMap = new Dictionary<int, NetworkTransport.DisconnectEvents>();
1853+
private Dictionary<NetworkTransport.DisconnectEvents, string> m_DisconnectEventMessageMap = new Dictionary<NetworkTransport.DisconnectEvents, string>();
1854+
1855+
internal NetworkTransport.DisconnectEvents GetDisconnectEvent(int disconnectEventId)
1856+
{
1857+
return m_DisconnectEventMap.ContainsKey(disconnectEventId) ? m_DisconnectEventMap[disconnectEventId] : NetworkTransport.DisconnectEvents.Disconnected;
1858+
}
1859+
1860+
public string GetDisconnectEventMessage(NetworkTransport.DisconnectEvents disconnectEvent)
1861+
{
1862+
return string.Empty;
1863+
}
1864+
1865+
public void ClosingRemoteConnection()
1866+
{
1867+
1868+
}
1869+
1870+
private void AddDisconnectEventMap(NetworkTransport.DisconnectEvents disconnectEvent, int disconnectReason, string message)
1871+
{
1872+
m_DisconnectEventMap.Add(disconnectReason, disconnectEvent);
1873+
m_DisconnectEventMessageMap.Add(disconnectEvent, message);
1874+
}
1875+
1876+
private void AddDisconnectEventMap(NetworkTransport.DisconnectEvents disconnectEvent, DisconnectReason disconnectReason, string message)
1877+
{
1878+
AddDisconnectEventMap(disconnectEvent, (int)disconnectReason, message);
1879+
}
1880+
1881+
public UnityTransportNotificationHandler()
1882+
{
1883+
// Implemented in UTP
1884+
AddDisconnectEventMap(NetworkTransport.DisconnectEvents.Disconnected, DisconnectReason.Default, DisconnectedMessage);
1885+
AddDisconnectEventMap(NetworkTransport.DisconnectEvents.ProtocolTimeout, DisconnectReason.Timeout, TimeoutMessage);
1886+
AddDisconnectEventMap(NetworkTransport.DisconnectEvents.MaxConnectionAttempts, DisconnectReason.MaxConnectionAttempts, MaxConnectionAttemptsMessage);
1887+
AddDisconnectEventMap(NetworkTransport.DisconnectEvents.ClosedByRemote, DisconnectReason.ClosedByRemote, ClosedByRemoteMessage);
1888+
AddDisconnectEventMap(NetworkTransport.DisconnectEvents.AuthenticationFailure, DisconnectReason.AuthenticationFailure, AuthenticationFailureMessage);
1889+
AddDisconnectEventMap(NetworkTransport.DisconnectEvents.ProtocolError, DisconnectReason.ProtocolError, ProtocolErrorMessage);
1890+
1891+
// Not implemented in UTP
1892+
AddDisconnectEventMap(NetworkTransport.DisconnectEvents.ClosedRemoteConnection, k_ClosedRemoteConnection, ClosedRemoteConnectionMessage);
1893+
AddDisconnectEventMap(NetworkTransport.DisconnectEvents.TransportShutdown, k_TransportShutdown, TransportShutdownMessage);
1894+
}
1895+
}
18521896
}

0 commit comments

Comments
 (0)