@@ -362,11 +362,6 @@ internal void RemovePendingClient(ulong clientId)
362362 return ( clientId , true ) ;
363363 }
364364
365- if ( NetworkLog . CurrentLogLevel == LogLevel . Developer )
366- {
367- NetworkLog . LogWarning ( $ "Trying to get the NGO client ID map for the transport ID ({ transportId } ) but did not find the map entry! Returning default transport ID value.") ;
368- }
369-
370365 return ( default , false ) ;
371366 }
372367
@@ -488,6 +483,15 @@ internal void HandleNetworkEvent(NetworkEvent networkEvent, ulong transportClien
488483 }
489484 }
490485
486+ /// <summary>
487+ /// Client's save their assigned transport id.
488+ /// </summary>
489+ /// <remarks>
490+ /// Added to be able to appropriately log the client's transport
491+ /// id when it is shutdown or disconnected.
492+ /// </remarks>
493+ private ulong m_LocalClientTransportId ;
494+
491495 /// <summary>
492496 /// Handles a <see cref="NetworkEvent.Connect"/> event.
493497 /// </summary>
@@ -508,6 +512,8 @@ internal void ConnectEventHandler(ulong transportClientId)
508512 }
509513 else
510514 {
515+ // Cache the local client's transport id.
516+ m_LocalClientTransportId = transportClientId ;
511517 clientId = NetworkManager . ServerClientId ;
512518 }
513519
@@ -585,9 +591,14 @@ private void GenerateDisconnectInformation(ulong clientId, ulong transportClient
585591 /// </summary>
586592 internal void DisconnectEventHandler ( ulong transportClientId )
587593 {
588- var ( clientId , wasConnectedClient ) = TransportIdCleanUp ( transportClientId ) ;
589- if ( ! wasConnectedClient )
594+ // Check to see if the client has already been removed from the table but
595+ // do not remove it just yet.
596+ var ( clientId , isConnectedClient ) = TransportIdToClientId ( transportClientId ) ;
597+
598+ // If the client is not registered and we are the server
599+ if ( ! isConnectedClient && NetworkManager . IsServer )
590600 {
601+ // Then exit early
591602 return ;
592603 }
593604
@@ -622,17 +633,12 @@ internal void DisconnectEventHandler(ulong transportClientId)
622633 {
623634 // We need to process the disconnection before notifying
624635 OnClientDisconnectFromServer ( clientId ) ;
625-
626- // Now notify the client has disconnected
627- InvokeOnClientDisconnectCallback ( clientId ) ;
628-
629- if ( LocalClient . IsHost )
630- {
631- InvokeOnPeerDisconnectedCallback ( clientId ) ;
632- }
633636 }
634637 else
635638 {
639+ // Client's clean up their transport id separately from the server.
640+ TransportIdCleanUp ( transportClientId ) ;
641+
636642 // Notify local client of disconnection
637643 InvokeOnClientDisconnectCallback ( clientId ) ;
638644
@@ -793,12 +799,15 @@ private IEnumerator ApprovalTimeout(ulong clientId)
793799 /// </summary>
794800 internal void ApproveConnection ( ref ConnectionRequestMessage connectionRequestMessage , ref NetworkContext context )
795801 {
802+ if ( ConnectionApprovalCallback == null )
803+ {
804+ return ;
805+ }
796806 // Note: Delegate creation allocates.
797807 // Note: ToArray() also allocates. :(
798808 var response = new NetworkManager . ConnectionApprovalResponse ( ) ;
799809 ClientsToApprove [ context . SenderId ] = response ;
800-
801- ConnectionApprovalCallback (
810+ ConnectionApprovalCallback ? . Invoke (
802811 new NetworkManager . ConnectionApprovalRequest
803812 {
804813 Payload = connectionRequestMessage . ConnectionData ,
@@ -852,13 +861,6 @@ internal void ProcessPendingApprovals()
852861 }
853862 }
854863
855- /// <summary>
856- /// Adding this because message hooks cannot happen fast enough under certain scenarios
857- /// where the message is sent and responded to before the hook is in place.
858- /// </summary>
859- internal bool MockSkippingApproval ;
860-
861-
862864 /// <summary>
863865 /// Server Side: Handles the denial of a client who sent a connection request
864866 /// </summary>
@@ -873,12 +875,36 @@ private void HandleConnectionDisconnect(ulong ownerClientId, string reason = "")
873875 {
874876 Reason = reason
875877 } ;
876- SendMessage ( ref disconnectReason , NetworkDelivery . Reliable , ownerClientId ) ;
878+ SendMessage ( ref disconnectReason , MessageDeliveryType < DisconnectReasonMessage > . DefaultDelivery , ownerClientId ) ;
879+ m_ClientsToDisconnect . Add ( ownerClientId ) ;
880+ return ;
877881 }
878882
879883 DisconnectRemoteClient ( ownerClientId ) ;
880884 }
881885
886+ private List < ulong > m_ClientsToDisconnect = new List < ulong > ( ) ;
887+
888+ internal void ProcessClientsToDisconnect ( )
889+ {
890+ if ( m_ClientsToDisconnect . Count == 0 )
891+ {
892+ return ;
893+ }
894+ foreach ( var clientId in m_ClientsToDisconnect )
895+ {
896+ try
897+ {
898+ DisconnectRemoteClient ( clientId ) ;
899+ }
900+ catch ( Exception ex )
901+ {
902+ Debug . LogException ( ex ) ;
903+ }
904+ }
905+ m_ClientsToDisconnect . Clear ( ) ;
906+ }
907+
882908 /// <summary>
883909 /// Server Side: Handles the approval of a client
884910 /// </summary>
@@ -939,12 +965,6 @@ internal void HandleConnectionApproval(ulong ownerClientId, bool createPlayerObj
939965 // Server doesn't send itself the connection approved message
940966 if ( ownerClientId != NetworkManager . ServerClientId )
941967 {
942- if ( MockSkippingApproval )
943- {
944- NetworkLog . LogInfo ( "Mocking server not responding with connection approved..." ) ;
945- return ;
946- }
947-
948968 SendConnectionApprovedMessage ( ownerClientId ) ;
949969
950970 // If scene management is disabled, then we are done and notify the local host-server the client is connected
@@ -1040,7 +1060,7 @@ private void SendConnectionApprovedMessage(ulong approvedClientId)
10401060 }
10411061 }
10421062
1043- SendMessage ( ref message , NetworkDelivery . ReliableFragmentedSequenced , approvedClientId ) ;
1063+ SendMessage ( ref message , MessageDeliveryType < ConnectionApprovedMessage > . DefaultDelivery , approvedClientId ) ;
10441064
10451065 message . MessageVersions . Dispose ( ) ;
10461066 message . ConnectedClientIds . Dispose ( ) ;
@@ -1111,13 +1131,9 @@ internal NetworkClient AddClient(ulong clientId)
11111131 return ConnectedClients [ clientId ] ;
11121132 }
11131133
1114- var networkClient = LocalClient ;
1115-
11161134 // If this is not the local client then create a new one
1117- if ( clientId != NetworkManager . LocalClientId )
1118- {
1119- networkClient = new NetworkClient ( ) ;
1120- }
1135+ var networkClient = clientId == NetworkManager . LocalClientId ? LocalClient : new NetworkClient ( ) ;
1136+
11211137 networkClient . SetRole ( clientId == NetworkManager . ServerClientId , isClient : true , NetworkManager ) ;
11221138 networkClient . ClientId = clientId ;
11231139 if ( ! ConnectedClients . ContainsKey ( clientId ) )
@@ -1224,6 +1240,14 @@ internal void OnClientDisconnectFromServer(ulong clientId)
12241240 // clean up as everything that needs to be destroyed will be during shutdown.
12251241 if ( NetworkManager . ShutdownInProgress && clientId == NetworkManager . ServerClientId )
12261242 {
1243+ // Now notify the client has disconnected.
1244+ // (transport id cleanup is handled within)
1245+ InvokeOnClientDisconnectCallback ( clientId ) ;
1246+
1247+ if ( LocalClient . IsHost )
1248+ {
1249+ InvokeOnPeerDisconnectedCallback ( clientId ) ;
1250+ }
12271251 return ;
12281252 }
12291253
@@ -1392,8 +1416,20 @@ internal void OnClientDisconnectFromServer(ulong clientId)
13921416 }
13931417
13941418 ConnectedClientIds . Remove ( clientId ) ;
1395- var message = new ClientDisconnectedMessage { ClientId = clientId } ;
1396- MessageManager ? . SendMessage ( ref message , MessageDeliveryType < ClientDisconnectedMessage > . DefaultDelivery , ConnectedClientIds ) ;
1419+
1420+ if ( MessageManager != null )
1421+ {
1422+ var message = new ClientDisconnectedMessage { ClientId = clientId } ;
1423+ foreach ( var sendToId in ConnectedClientIds )
1424+ {
1425+ // Do not send a disconnect message to ourself
1426+ if ( sendToId == NetworkManager . LocalClientId )
1427+ {
1428+ continue ;
1429+ }
1430+ MessageManager . SendMessage ( ref message , MessageDeliveryType < ClientDisconnectedMessage > . DefaultDelivery , sendToId ) ;
1431+ }
1432+ }
13971433
13981434 // Used for testing/validation purposes only
13991435 // Promote a new session owner when the ENABLE_DAHOST_AUTOPROMOTE_SESSION_OWNER scripting define is set
@@ -1404,17 +1440,18 @@ internal void OnClientDisconnectFromServer(ulong clientId)
14041440 var ( transportId , idExists ) = ClientIdToTransportId ( clientId ) ;
14051441 if ( idExists )
14061442 {
1407- NetworkManager . NetworkConfig . NetworkTransport . DisconnectRemoteClient ( transportId ) ;
1408-
1409- InvokeOnClientDisconnectCallback ( clientId ) ;
1410-
1411- if ( LocalClient . IsHost )
1443+ // Clean up the transport to client (and vice versa) mappings
1444+ var ( transportIdDisconnected , wasRemoved ) = TransportIdCleanUp ( transportId ) ;
1445+ if ( wasRemoved )
14121446 {
1413- InvokeOnPeerDisconnectedCallback ( clientId ) ;
1414- }
1447+ NetworkManager . NetworkConfig . NetworkTransport . DisconnectRemoteClient ( transportId ) ;
1448+ InvokeOnClientDisconnectCallback ( clientId ) ;
14151449
1416- // Clean up the transport to client (and vice versa) mappings
1417- TransportIdCleanUp ( transportId ) ;
1450+ if ( LocalClient . IsHost )
1451+ {
1452+ InvokeOnPeerDisconnectedCallback ( clientId ) ;
1453+ }
1454+ }
14181455 }
14191456
14201457 // Assure the client id is no longer in the pending clients list
@@ -1462,16 +1499,6 @@ internal void DisconnectClient(ulong clientId, string reason = null)
14621499 return ;
14631500 }
14641501
1465- if ( ! string . IsNullOrEmpty ( reason ) )
1466- {
1467- var disconnectReason = new DisconnectReasonMessage
1468- {
1469- Reason = reason
1470- } ;
1471- SendMessage ( ref disconnectReason , NetworkDelivery . Reliable , clientId ) ;
1472- }
1473-
1474- Transport . ClosingRemoteConnection ( ) ;
14751502 var transportId = ClientIdToTransportId ( clientId ) ;
14761503 if ( transportId . Item2 )
14771504 {
@@ -1491,6 +1518,7 @@ internal void DisconnectClient(ulong clientId, string reason = null)
14911518 internal void Initialize ( NetworkManager networkManager )
14921519 {
14931520 // Prepare for a new session
1521+ m_LocalClientTransportId = 0 ;
14941522 LocalClient . IsApproved = false ;
14951523 m_PendingClients . Clear ( ) ;
14961524 ConnectedClients . Clear ( ) ;
@@ -1524,8 +1552,9 @@ internal void Shutdown()
15241552 {
15251553 Transport . ShuttingDown ( ) ;
15261554 var clientId = NetworkManager ? NetworkManager . LocalClientId : NetworkManager . ServerClientId ;
1527- var transportId = ClientIdToTransportId ( clientId ) ;
1528- GenerateDisconnectInformation ( clientId , transportId . Item1 , $ "{ nameof ( NetworkConnectionManager ) } was shutdown.") ;
1555+ // Server and host just log 0 for their transport id while clients will log their cached m_LocalClientTransportId
1556+ var transportId = clientId == NetworkManager . ServerClientId ? 0 : m_LocalClientTransportId ;
1557+ GenerateDisconnectInformation ( clientId , transportId , $ "{ nameof ( NetworkConnectionManager ) } was shutdown.") ;
15291558 }
15301559
15311560 if ( LocalClient . IsServer )
0 commit comments