Skip to content

Commit 90c16bc

Browse files
Simplify ErrorUtilities
1 parent 82d09dc commit 90c16bc

File tree

1 file changed

+35
-52
lines changed

1 file changed

+35
-52
lines changed

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

Lines changed: 35 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -44,56 +44,6 @@ void CreateDriver(
4444
out NetworkPipeline reliableSequencedPipeline);
4545
}
4646

47-
/// <summary>
48-
/// Helper utility class to convert <see cref="Networking.Transport"/> error codes to human readable error messages.
49-
/// </summary>
50-
public static class ErrorUtilities
51-
{
52-
private static readonly FixedString128Bytes k_NetworkSuccess = "Success";
53-
private static readonly FixedString128Bytes k_NetworkIdMismatch = "Invalid connection ID {0}.";
54-
private static readonly FixedString128Bytes k_NetworkVersionMismatch = "Connection ID is invalid. Likely caused by sending on stale connection {0}.";
55-
private static readonly FixedString128Bytes k_NetworkStateMismatch = "Connection state is invalid. Likely caused by sending on connection {0} which is stale or still connecting.";
56-
private static readonly FixedString128Bytes k_NetworkPacketOverflow = "Packet is too large to be allocated by the transport.";
57-
private static readonly FixedString128Bytes k_NetworkSendQueueFull = "Unable to queue packet in the transport. Likely caused by send queue size ('Max Send Queue Size') being too small.";
58-
59-
/// <summary>
60-
/// Convert a UTP error code to human-readable error message.
61-
/// </summary>
62-
/// <param name="error">UTP error code.</param>
63-
/// <param name="connectionId">ID of the connection on which the error occurred.</param>
64-
/// <returns>Human-readable error message.</returns>
65-
public static string ErrorToString(TransportError error, ulong connectionId)
66-
{
67-
return ErrorToString((int)error, connectionId);
68-
}
69-
70-
internal static string ErrorToString(int error, ulong connectionId)
71-
{
72-
return ErrorToFixedString(error, connectionId).ToString();
73-
}
74-
75-
internal static FixedString128Bytes ErrorToFixedString(int error, ulong connectionId)
76-
{
77-
switch ((TransportError)error)
78-
{
79-
case TransportError.Success:
80-
return k_NetworkSuccess;
81-
case TransportError.NetworkIdMismatch:
82-
return FixedString.Format(k_NetworkIdMismatch, connectionId);
83-
case TransportError.NetworkVersionMismatch:
84-
return FixedString.Format(k_NetworkVersionMismatch, connectionId);
85-
case TransportError.NetworkStateMismatch:
86-
return FixedString.Format(k_NetworkStateMismatch, connectionId);
87-
case TransportError.NetworkPacketOverflow:
88-
return k_NetworkPacketOverflow;
89-
case TransportError.NetworkSendQueueFull:
90-
return k_NetworkSendQueueFull;
91-
default:
92-
return FixedString.Format("Unknown error code {0}.", error);
93-
}
94-
}
95-
}
96-
9747
/// <summary>
9848
/// The Netcode for GameObjects NetworkTransport for UnityTransport.
9949
/// Note: This is highly recommended to use over UNet.
@@ -769,7 +719,7 @@ public void Execute()
769719
var result = Driver.BeginSend(pipeline, connection, out var writer);
770720
if (result != (int)TransportError.Success)
771721
{
772-
Debug.LogError($"Error sending message: {ErrorUtilities.ErrorToFixedString(result, clientId)}");
722+
Debug.LogError($"Send error on connection {clientId}: {ErrorUtilities.ErrorToFixedString(result)}");
773723
return;
774724
}
775725

@@ -796,7 +746,7 @@ public void Execute()
796746
// just get the same error again).
797747
if (result != (int)TransportError.NetworkSendQueueFull)
798748
{
799-
Debug.LogError($"Error sending the message: {ErrorUtilities.ErrorToFixedString(result, clientId)}");
749+
Debug.LogError($"Send error on connection {clientId}: {ErrorUtilities.ErrorToFixedString(result)}");
800750
Queue.Consume(written);
801751
}
802752

@@ -1765,4 +1715,37 @@ public override int GetHashCode()
17651715
}
17661716
}
17671717
}
1718+
1719+
/// <summary>
1720+
/// Utility class to convert Unity Transport error codes to human-readable error messages.
1721+
/// </summary>
1722+
public static class ErrorUtilities
1723+
{
1724+
/// <summary>
1725+
/// Convert a Unity Transport error code to human-readable error message.
1726+
/// </summary>
1727+
/// <param name="error">Unity Transport error code.</param>
1728+
/// <param name="connectionId">ID of connection on which error occurred (unused).</param>
1729+
/// <returns>Human-readable error message.</returns>
1730+
public static string ErrorToString(TransportError error, ulong connectionId)
1731+
{
1732+
return ErrorToFixedString((int)error).ToString();
1733+
}
1734+
1735+
internal static FixedString128Bytes ErrorToFixedString(int error)
1736+
{
1737+
switch ((TransportError)error)
1738+
{
1739+
case TransportError.NetworkVersionMismatch:
1740+
case TransportError.NetworkStateMismatch:
1741+
return "invalid connection state (likely stale/closed connection)";
1742+
case TransportError.NetworkPacketOverflow:
1743+
return "packet is too large for the transport (likely need to increase MTU)";
1744+
case TransportError.NetworkSendQueueFull:
1745+
return "send queue full (need to increase 'Max Send Queue Size' parameter)";
1746+
default:
1747+
return FixedString.Format("unexpected error code {0}", error);
1748+
}
1749+
}
1750+
}
17681751
}

0 commit comments

Comments
 (0)