@@ -103,11 +103,52 @@ public sealed class NetworkConnectionManager
103103 private static ProfilerMarker s_TransportDisconnect = new ProfilerMarker ( $ "{ nameof ( NetworkManager ) } .TransportDisconnect") ;
104104#endif
105105
106+ private string m_DisconnectReason ;
106107 /// <summary>
107108 /// When disconnected from the server, the server may send a reason. If a reason was sent, this property will
108- /// tell client code what the reason was. It should be queried after the OnClientDisconnectCallback is called
109+ /// provide disconnect information that will be followed by the server's disconnect reason.
109110 /// </summary>
110- public string DisconnectReason { get ; internal set ; }
111+ /// <remarks>
112+ /// On a server or host, this value could no longer exist after all subscribed callbacks are invoked for the
113+ /// client that disconnected. It is recommended to copy the message to some other property or field when
114+ /// <see cref="OnClientDisconnectCallback"/> is invoked.
115+ /// </remarks>
116+ public string DisconnectReason
117+ {
118+ get
119+ {
120+ // For in-frequent event driven invocations, a method within a getter
121+ // is "generally ok".
122+ return GetDisconnectReason ( ) ;
123+ }
124+ internal set
125+ {
126+ m_DisconnectReason = value ;
127+ }
128+ }
129+
130+ /// <summary>
131+ /// Returns the conbined result of the locally applied <see cref="DisconnectReason"/> and the
132+ /// server applied <see cref="ServerDisconnectReason"/>.
133+ /// - If both values are empty or null, then it returns <see cref="string.Empty"/>.
134+ /// - If either value is valid, then it returns that <see cref="string"/> value.
135+ /// - If both values are valid, then it returns <see cref="DisconnectReason"/> followed by a
136+ /// new line and then <see cref="ServerDisconnectReason"/>.
137+ /// </summary>
138+ /// <returns>A disconnect reason, if any.</returns>
139+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
140+ internal string GetDisconnectReason ( string header = null )
141+ {
142+ var disconnectReason = string . IsNullOrEmpty ( m_DisconnectReason ) ? string . Empty : m_DisconnectReason ;
143+ var serverDisconnectReason = string . IsNullOrEmpty ( ServerDisconnectReason ) ? string . Empty : $ "\n { ServerDisconnectReason } ";
144+ var headerInfo = string . IsNullOrEmpty ( header ) ? string . Empty : header ;
145+ return $ "{ headerInfo } { disconnectReason } { serverDisconnectReason } ";
146+ }
147+
148+ /// <summary>
149+ /// Updated by <see cref="DisconnectReasonMessage"/>.
150+ /// </summary>
151+ internal string ServerDisconnectReason ;
111152
112153 /// <summary>
113154 /// The callback to invoke once a client connects. This callback is only ran on the server and on the local client that connects.
@@ -537,17 +578,15 @@ internal void DataEventHandler(ulong transportClientId, ref ArraySegment<byte> p
537578 private void GenerateDisconnectInformation ( ulong clientId , ulong transportClientId , string reason = null )
538579 {
539580 var header = $ "[Disconnect Event][Client-{ clientId } ][TransportClientId-{ transportClientId } ]";
540- var existingDisconnectReason = DisconnectReason ;
541-
542581 var defaultMessage = Transport . DisconnectEventMessage ;
543582 if ( reason != null )
544583 {
545584 defaultMessage = $ "{ reason } { defaultMessage } ";
546585 }
586+
547587 // Just go ahead and set this whether client or server so any subscriptions to a disconnect event can check the DisconnectReason
548588 // to determine why the client disconnected
549- DisconnectReason = $ "{ header } [{ Transport . DisconnectEvent } ] { defaultMessage } ";
550- DisconnectReason = $ "{ DisconnectReason } \n { existingDisconnectReason } ";
589+ m_DisconnectReason = $ "{ header } [{ Transport . DisconnectEvent } ] { defaultMessage } ";
551590
552591 if ( NetworkLog . CurrentLogLevel <= LogLevel . Developer )
553592 {
@@ -1450,7 +1489,6 @@ internal void DisconnectClient(ulong clientId, string reason = null)
14501489 var transportId = ClientIdToTransportId ( clientId ) ;
14511490 if ( transportId . Item2 )
14521491 {
1453- DisconnectReason = string . Empty ;
14541492 GenerateDisconnectInformation ( clientId , transportId . Item1 , reason ) ;
14551493 }
14561494
@@ -1476,7 +1514,8 @@ internal void Initialize(NetworkManager networkManager)
14761514 TransportIdToClientIdMap . Clear ( ) ;
14771515 ClientsToApprove . Clear ( ) ;
14781516 NetworkObject . OrphanChildren . Clear ( ) ;
1479- DisconnectReason = string . Empty ;
1517+ m_DisconnectReason = string . Empty ;
1518+ ServerDisconnectReason = string . Empty ;
14801519
14811520 NetworkManager = networkManager ;
14821521 MessageManager = networkManager . MessageManager ;
0 commit comments