Skip to content

Commit 0188d95

Browse files
EmandMNoelStephensUnity
authored andcommitted
fix: Ensure DisconnectReason remains parsable (#3766)
1 parent 3997244 commit 0188d95

File tree

3 files changed

+13
-27
lines changed

3 files changed

+13
-27
lines changed

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

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -113,36 +113,21 @@ public sealed class NetworkConnectionManager
113113
/// client that disconnected. It is recommended to copy the message to some other property or field when
114114
/// <see cref="OnClientDisconnectCallback"/> is invoked.
115115
/// </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-
}
116+
public string DisconnectReason => GetDisconnectReason(); // fine as function because this call is infrequent
129117

130118
/// <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"/>.
119+
/// Gets the reason for why this client was disconnected if exists.
137120
/// </summary>
138-
/// <returns>A disconnect reason, if any.</returns>
121+
/// <returns><see cref="ServerDisconnectReason"/> disconnect reason if it exists, otherwise <see cref="m_DisconnectReason"/>.</returns>
139122
[MethodImpl(MethodImplOptions.AggressiveInlining)]
140-
internal string GetDisconnectReason(string header = null)
123+
internal string GetDisconnectReason()
141124
{
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}";
125+
// TODO: fix this properly
126+
if (!string.IsNullOrEmpty(ServerDisconnectReason))
127+
{
128+
return ServerDisconnectReason;
129+
}
130+
return m_DisconnectReason;
146131
}
147132

148133
/// <summary>
@@ -590,7 +575,8 @@ private void GenerateDisconnectInformation(ulong clientId, ulong transportClient
590575

591576
if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
592577
{
593-
NetworkLog.LogInfo($"{DisconnectReason}");
578+
var serverDisconnectReason = string.IsNullOrEmpty(ServerDisconnectReason) ? string.Empty : $"\n{ServerDisconnectReason}";
579+
NetworkLog.LogInfo($"{m_DisconnectReason}{serverDisconnectReason}");
594580
}
595581
}
596582

com.unity.netcode.gameobjects/Runtime/Messaging/DisconnectReasonMessage.cs renamed to com.unity.netcode.gameobjects/Runtime/Messaging/Messages/DisconnectReasonMessage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public void Serialize(FastBufferWriter writer, int targetVersion)
1010
{
1111
string reasonSent = Reason ?? string.Empty;
1212

13-
// Since we don't send a ConnectionApprovedMessage, the version for this message is encded with the message itself.
13+
// Since we don't send a ConnectionApprovedMessage, the version for this message is encoded with the message itself.
1414
// However, note that we HAVE received a ConnectionRequestMessage, so we DO have a valid targetVersion on this side of things.
1515
// We just have to make sure the receiving side knows what version we sent it, since whoever has the higher version number is responsible for versioning and they may be the one with the higher version number.
1616
BytePacker.WriteValueBitPacked(writer, Version);

0 commit comments

Comments
 (0)