16
16
using Unity . Networking . Transport . TLS ;
17
17
using Unity . Networking . Transport . Utilities ;
18
18
using UnityEngine ;
19
- using NetcodeNetworkEvent = Unity . Netcode . NetworkEvent ;
20
- using TransportNetworkEvent = Unity . Networking . Transport . NetworkEvent ;
19
+
20
+ using NetcodeEvent = Unity . Netcode . NetworkEvent ;
21
+ using TransportError = Unity . Networking . Transport . Error . StatusCode ;
22
+ using TransportEvent = Unity . Networking . Transport . NetworkEvent . Type ;
21
23
22
24
namespace Unity . Netcode . Transports . UTP
23
25
{
@@ -42,56 +44,6 @@ void CreateDriver(
42
44
out NetworkPipeline reliableSequencedPipeline ) ;
43
45
}
44
46
45
- /// <summary>
46
- /// Helper utility class to convert <see cref="Networking.Transport"/> error codes to human readable error messages.
47
- /// </summary>
48
- public static class ErrorUtilities
49
- {
50
- private static readonly FixedString128Bytes k_NetworkSuccess = "Success" ;
51
- private static readonly FixedString128Bytes k_NetworkIdMismatch = "Invalid connection ID {0}." ;
52
- private static readonly FixedString128Bytes k_NetworkVersionMismatch = "Connection ID is invalid. Likely caused by sending on stale connection {0}." ;
53
- private static readonly FixedString128Bytes k_NetworkStateMismatch = "Connection state is invalid. Likely caused by sending on connection {0} which is stale or still connecting." ;
54
- private static readonly FixedString128Bytes k_NetworkPacketOverflow = "Packet is too large to be allocated by the transport." ;
55
- 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." ;
56
-
57
- /// <summary>
58
- /// Convert a UTP error code to human-readable error message.
59
- /// </summary>
60
- /// <param name="error">UTP error code.</param>
61
- /// <param name="connectionId">ID of the connection on which the error occurred.</param>
62
- /// <returns>Human-readable error message.</returns>
63
- public static string ErrorToString ( Networking . Transport . Error . StatusCode error , ulong connectionId )
64
- {
65
- return ErrorToString ( ( int ) error , connectionId ) ;
66
- }
67
-
68
- internal static string ErrorToString ( int error , ulong connectionId )
69
- {
70
- return ErrorToFixedString ( error , connectionId ) . ToString ( ) ;
71
- }
72
-
73
- internal static FixedString128Bytes ErrorToFixedString ( int error , ulong connectionId )
74
- {
75
- switch ( ( Networking . Transport . Error . StatusCode ) error )
76
- {
77
- case Networking . Transport . Error . StatusCode . Success :
78
- return k_NetworkSuccess ;
79
- case Networking . Transport . Error . StatusCode . NetworkIdMismatch :
80
- return FixedString . Format ( k_NetworkIdMismatch , connectionId ) ;
81
- case Networking . Transport . Error . StatusCode . NetworkVersionMismatch :
82
- return FixedString . Format ( k_NetworkVersionMismatch , connectionId ) ;
83
- case Networking . Transport . Error . StatusCode . NetworkStateMismatch :
84
- return FixedString . Format ( k_NetworkStateMismatch , connectionId ) ;
85
- case Networking . Transport . Error . StatusCode . NetworkPacketOverflow :
86
- return k_NetworkPacketOverflow ;
87
- case Networking . Transport . Error . StatusCode . NetworkSendQueueFull :
88
- return k_NetworkSendQueueFull ;
89
- default :
90
- return FixedString . Format ( "Unknown error code {0}." , error ) ;
91
- }
92
- }
93
- }
94
-
95
47
/// <summary>
96
48
/// The Netcode for GameObjects NetworkTransport for UnityTransport.
97
49
/// Note: This is highly recommended to use over UNet.
@@ -114,13 +66,6 @@ public enum ProtocolType
114
66
RelayUnityTransport ,
115
67
}
116
68
117
- private enum State
118
- {
119
- Disconnected ,
120
- Listening ,
121
- Connected ,
122
- }
123
-
124
69
/// <summary>
125
70
/// The default maximum (receive) packet queue size
126
71
/// </summary>
@@ -421,17 +366,16 @@ private struct PacketLossCache
421
366
422
367
internal static event Action < int , NetworkDriver > TransportInitialized ;
423
368
internal static event Action < int > TransportDisposed ;
424
- internal NetworkDriver NetworkDriver => m_Driver ;
425
369
426
370
/// <summary>
427
- /// Provides access to the <see cref="Networking.Transport. NetworkDriver"/> for this <see cref="UnityTransport"/> instance.
371
+ /// Provides access to the <see cref="NetworkDriver"/> for this instance.
428
372
/// </summary>
429
373
protected NetworkDriver m_Driver ;
430
374
431
375
/// <summary>
432
- /// Gets a reference to the <see cref="Networking.Transport. NetworkDriver"/>.
376
+ /// Gets a reference to the <see cref="NetworkDriver"/>.
433
377
/// </summary>
434
- /// <returns>ref <see cref="Networking.Transport. NetworkDriver"/></returns>
378
+ /// <returns>ref <see cref="NetworkDriver"/></returns>
435
379
public ref NetworkDriver GetNetworkDriver ( )
436
380
{
437
381
return ref m_Driver ;
@@ -455,7 +399,6 @@ public NetworkEndpoint GetLocalEndpoint()
455
399
456
400
private PacketLossCache m_PacketLossCache = new PacketLossCache ( ) ;
457
401
458
- private State m_State = State . Disconnected ;
459
402
private NetworkSettings m_NetworkSettings ;
460
403
private ulong m_ServerClientId ;
461
404
@@ -501,7 +444,7 @@ private void InitDriver()
501
444
out m_UnreliableSequencedFragmentedPipeline ,
502
445
out m_ReliableSequencedPipeline ) ;
503
446
504
- TransportInitialized ? . Invoke ( GetInstanceID ( ) , NetworkDriver ) ;
447
+ TransportInitialized ? . Invoke ( GetInstanceID ( ) , m_Driver ) ;
505
448
}
506
449
507
450
private void DisposeInternals ( )
@@ -583,8 +526,7 @@ private bool ClientBindAndConnect()
583
526
return false ;
584
527
}
585
528
586
- var serverConnection = Connect ( serverEndpoint ) ;
587
- m_ServerClientId = ParseClientId ( serverConnection ) ;
529
+ Connect ( serverEndpoint ) ;
588
530
589
531
return true ;
590
532
}
@@ -624,7 +566,6 @@ private bool ServerBindAndListen(NetworkEndpoint endPoint)
624
566
return false ;
625
567
}
626
568
627
- m_State = State . Listening ;
628
569
return true ;
629
570
}
630
571
@@ -776,9 +717,9 @@ public void Execute()
776
717
while ( ! Queue . IsEmpty )
777
718
{
778
719
var result = Driver . BeginSend ( pipeline , connection , out var writer ) ;
779
- if ( result != ( int ) Networking . Transport . Error . StatusCode . Success )
720
+ if ( result != ( int ) TransportError . Success )
780
721
{
781
- Debug . LogError ( $ "Error sending message : { ErrorUtilities . ErrorToFixedString ( result , clientId ) } ") ;
722
+ Debug . LogError ( $ "Send error on connection { clientId } : { ErrorUtilities . ErrorToFixedString ( result ) } ") ;
782
723
return ;
783
724
}
784
725
@@ -803,9 +744,9 @@ public void Execute()
803
744
// and we'll retry sending them later). Otherwise log the error and remove the
804
745
// message from the queue (we don't want to resend it again since we'll likely
805
746
// just get the same error again).
806
- if ( result != ( int ) Networking . Transport . Error . StatusCode . NetworkSendQueueFull )
747
+ if ( result != ( int ) TransportError . NetworkSendQueueFull )
807
748
{
808
- Debug . LogError ( $ "Error sending the message : { ErrorUtilities . ErrorToFixedString ( result , clientId ) } ") ;
749
+ Debug . LogError ( $ "Send error on connection { clientId } : { ErrorUtilities . ErrorToFixedString ( result ) } ") ;
809
750
Queue . Consume ( written ) ;
810
751
}
811
752
@@ -849,7 +790,7 @@ private bool AcceptConnection()
849
790
return false ;
850
791
}
851
792
852
- InvokeOnTransportEvent ( NetcodeNetworkEvent . Connect ,
793
+ InvokeOnTransportEvent ( NetcodeEvent . Connect ,
853
794
ParseClientId ( connection ) ,
854
795
default ,
855
796
m_RealTimeProvider . RealTimeSinceStartup ) ;
@@ -887,7 +828,7 @@ private void ReceiveMessages(ulong clientId, NetworkPipeline pipeline, DataStrea
887
828
break ;
888
829
}
889
830
890
- InvokeOnTransportEvent ( NetcodeNetworkEvent . Data , clientId , message , m_RealTimeProvider . RealTimeSinceStartup ) ;
831
+ InvokeOnTransportEvent ( NetcodeEvent . Data , clientId , message , m_RealTimeProvider . RealTimeSinceStartup ) ;
891
832
}
892
833
}
893
834
@@ -898,44 +839,38 @@ private bool ProcessEvent()
898
839
899
840
switch ( eventType )
900
841
{
901
- case TransportNetworkEvent . Type . Connect :
842
+ case TransportEvent . Connect :
902
843
{
903
- InvokeOnTransportEvent ( NetcodeNetworkEvent . Connect ,
844
+ InvokeOnTransportEvent ( NetcodeEvent . Connect ,
904
845
clientId ,
905
846
default ,
906
847
m_RealTimeProvider . RealTimeSinceStartup ) ;
907
848
908
- m_State = State . Connected ;
849
+ m_ServerClientId = clientId ;
909
850
return true ;
910
851
}
911
- case TransportNetworkEvent . Type . Disconnect :
852
+ case TransportEvent . Disconnect :
912
853
{
913
- // Handle cases where we're a client receiving a Disconnect event. The
914
- // meaning of the event depends on our current state. If we were connected
915
- // then it means we got disconnected. If we were disconnected means that our
916
- // connection attempt has failed.
917
- if ( m_State == State . Connected )
918
- {
919
- m_State = State . Disconnected ;
920
- m_ServerClientId = default ;
921
- }
922
- else if ( m_State == State . Disconnected )
854
+ // If we're a client and had not yet set the server client ID, it means
855
+ // our connection to the server failed to be established. Any other case
856
+ // means a clean disconnect that doesn't require logging.
857
+ if ( ! m_Driver . Listening && m_ServerClientId == default )
923
858
{
924
859
Debug . LogError ( "Failed to connect to server." ) ;
925
- m_ServerClientId = default ;
926
860
}
927
861
862
+ m_ServerClientId = default ;
928
863
m_ReliableReceiveQueues . Remove ( clientId ) ;
929
864
ClearSendQueuesForClientId ( clientId ) ;
930
865
931
- InvokeOnTransportEvent ( NetcodeNetworkEvent . Disconnect ,
866
+ InvokeOnTransportEvent ( NetcodeEvent . Disconnect ,
932
867
clientId ,
933
868
default ,
934
869
m_RealTimeProvider . RealTimeSinceStartup ) ;
935
870
936
871
return true ;
937
872
}
938
- case TransportNetworkEvent . Type . Data :
873
+ case TransportEvent . Data :
939
874
{
940
875
ReceiveMessages ( clientId , pipeline , reader ) ;
941
876
return true ;
@@ -957,7 +892,7 @@ protected override void OnEarlyUpdate()
957
892
Debug . LogError ( "Transport failure! Relay allocation needs to be recreated, and NetworkManager restarted. " +
958
893
"Use NetworkManager.OnTransportFailure to be notified of such events programmatically." ) ;
959
894
960
- InvokeOnTransportEvent ( NetcodeNetworkEvent . TransportFailure , 0 , default , m_RealTimeProvider . RealTimeSinceStartup ) ;
895
+ InvokeOnTransportEvent ( NetcodeEvent . TransportFailure , 0 , default , m_RealTimeProvider . RealTimeSinceStartup ) ;
961
896
return ;
962
897
}
963
898
@@ -1180,21 +1115,21 @@ private void FlushSendQueuesForClientId(ulong clientId)
1180
1115
/// </summary>
1181
1116
public override void DisconnectLocalClient ( )
1182
1117
{
1183
- if ( m_State == State . Connected )
1118
+ if ( m_ServerClientId != default )
1184
1119
{
1185
1120
FlushSendQueuesForClientId ( m_ServerClientId ) ;
1186
1121
1187
1122
if ( m_Driver . Disconnect ( ParseClientId ( m_ServerClientId ) ) == 0 )
1188
1123
{
1189
- m_State = State . Disconnected ;
1124
+ m_ServerClientId = default ;
1190
1125
1191
1126
m_ReliableReceiveQueues . Remove ( m_ServerClientId ) ;
1192
1127
ClearSendQueuesForClientId ( m_ServerClientId ) ;
1193
1128
1194
1129
// If we successfully disconnect we dispatch a local disconnect message
1195
1130
// this how uNET and other transports worked and so this is just keeping with the old behavior
1196
1131
// should be also noted on the client this will call shutdown on the NetworkManager and the Transport
1197
- InvokeOnTransportEvent ( NetcodeNetworkEvent . Disconnect ,
1132
+ InvokeOnTransportEvent ( NetcodeEvent . Disconnect ,
1198
1133
m_ServerClientId ,
1199
1134
default ,
1200
1135
m_RealTimeProvider . RealTimeSinceStartup ) ;
@@ -1209,14 +1144,14 @@ public override void DisconnectLocalClient()
1209
1144
public override void DisconnectRemoteClient ( ulong clientId )
1210
1145
{
1211
1146
#if DEBUG
1212
- if ( m_State != State . Listening )
1147
+ if ( ! m_Driver . IsCreated )
1213
1148
{
1214
1149
Debug . LogWarning ( $ "{ nameof ( DisconnectRemoteClient ) } should only be called on a listening server!") ;
1215
1150
return ;
1216
1151
}
1217
1152
#endif
1218
1153
1219
- if ( m_State == State . Listening )
1154
+ if ( m_Driver . IsCreated )
1220
1155
{
1221
1156
FlushSendQueuesForClientId ( clientId ) ;
1222
1157
@@ -1331,12 +1266,12 @@ public override void Initialize(NetworkManager networkManager = null)
1331
1266
/// <param name="payload">The incoming data payload</param>
1332
1267
/// <param name="receiveTime">The time the event was received, as reported by m_RealTimeProvider.RealTimeSinceStartup.</param>
1333
1268
/// <returns>Returns the event type</returns>
1334
- public override NetcodeNetworkEvent PollEvent ( out ulong clientId , out ArraySegment < byte > payload , out float receiveTime )
1269
+ public override NetcodeEvent PollEvent ( out ulong clientId , out ArraySegment < byte > payload , out float receiveTime )
1335
1270
{
1336
1271
clientId = default ;
1337
1272
payload = default ;
1338
1273
receiveTime = default ;
1339
- return NetcodeNetworkEvent . Nothing ;
1274
+ return NetcodeEvent . Nothing ;
1340
1275
}
1341
1276
1342
1277
/// <summary>
@@ -1404,7 +1339,7 @@ public override void Send(ulong clientId, ArraySegment<byte> payload, NetworkDel
1404
1339
DisconnectRemoteClient ( clientId ) ;
1405
1340
1406
1341
// DisconnectRemoteClient doesn't notify SDK of disconnection.
1407
- InvokeOnTransportEvent ( NetcodeNetworkEvent . Disconnect ,
1342
+ InvokeOnTransportEvent ( NetcodeEvent . Disconnect ,
1408
1343
clientId ,
1409
1344
default ( ArraySegment < byte > ) ,
1410
1345
m_RealTimeProvider . RealTimeSinceStartup ) ;
@@ -1520,10 +1455,9 @@ public override void Shutdown()
1520
1455
DisposeInternals ( ) ;
1521
1456
1522
1457
m_ReliableReceiveQueues . Clear ( ) ;
1523
- m_State = State . Disconnected ;
1524
1458
1525
1459
// We must reset this to zero because UTP actually re-uses clientIds if there is a clean disconnect
1526
- m_ServerClientId = 0 ;
1460
+ m_ServerClientId = default ;
1527
1461
}
1528
1462
1529
1463
private void ConfigureSimulator ( )
@@ -1786,4 +1720,37 @@ public override int GetHashCode()
1786
1720
}
1787
1721
}
1788
1722
}
1723
+
1724
+ /// <summary>
1725
+ /// Utility class to convert Unity Transport error codes to human-readable error messages.
1726
+ /// </summary>
1727
+ public static class ErrorUtilities
1728
+ {
1729
+ /// <summary>
1730
+ /// Convert a Unity Transport error code to human-readable error message.
1731
+ /// </summary>
1732
+ /// <param name="error">Unity Transport error code.</param>
1733
+ /// <param name="connectionId">ID of connection on which error occurred (unused).</param>
1734
+ /// <returns>Human-readable error message.</returns>
1735
+ public static string ErrorToString ( TransportError error , ulong connectionId )
1736
+ {
1737
+ return ErrorToFixedString ( ( int ) error ) . ToString ( ) ;
1738
+ }
1739
+
1740
+ internal static FixedString128Bytes ErrorToFixedString ( int error )
1741
+ {
1742
+ switch ( ( TransportError ) error )
1743
+ {
1744
+ case TransportError . NetworkVersionMismatch :
1745
+ case TransportError . NetworkStateMismatch :
1746
+ return "invalid connection state (likely stale/closed connection)" ;
1747
+ case TransportError . NetworkPacketOverflow :
1748
+ return "packet is too large for the transport (likely need to increase MTU)" ;
1749
+ case TransportError . NetworkSendQueueFull :
1750
+ return "send queue full (need to increase 'Max Send Queue Size' parameter)" ;
1751
+ default :
1752
+ return FixedString . Format ( "unexpected error code {0}" , error ) ;
1753
+ }
1754
+ }
1755
+ }
1789
1756
}
0 commit comments