44using Unity . Burst ;
55using Unity . Burst . Intrinsics ;
66using Unity . Collections ;
7+ using Unity . Collections . LowLevel . Unsafe ;
78using Unity . Entities ;
89using Unity . NetCode ;
910using Unity . Netcode . Transports . UTP ;
@@ -42,12 +43,12 @@ public static NativeArray<byte> ToNativeArray(in FixedBytes1280 data)
4243 }
4344 }
4445
45- [ BurstCompile ]
46+ [ BurstCompile ]
4647 internal struct TransportRpc : IOutOfBandRpcCommand , IRpcCommandSerializer < TransportRpc >
4748 {
4849 public FixedBytes1280 Buffer ;
4950 public ulong Order ;
50-
51+
5152 public unsafe void Serialize ( ref DataStreamWriter writer , in RpcSerializerState state , in TransportRpc data )
5253 {
5354 writer . WriteULong ( data . Order ) ;
@@ -64,7 +65,7 @@ public unsafe void Deserialize(ref DataStreamReader reader, in RpcDeserializerSt
6465 {
6566 Length = length
6667 } ;
67-
68+
6869 var span = new Span < byte > ( FixedBytes1280 . GetUnsafePtr ( data . Buffer ) , length ) ;
6970 reader . ReadBytes ( span ) ;
7071 }
@@ -74,7 +75,7 @@ private static void InvokeExecute(ref RpcExecutor.Parameters parameters)
7475 {
7576 RpcExecutor . ExecuteCreateRequestComponent < TransportRpc , TransportRpc > ( ref parameters ) ;
7677 }
77-
78+
7879 private static readonly PortableFunctionPointer < RpcExecutor . ExecuteDelegate > k_InvokeExecuteFunctionPointer = new PortableFunctionPointer < RpcExecutor . ExecuteDelegate > ( InvokeExecute ) ;
7980
8081 public PortableFunctionPointer < RpcExecutor . ExecuteDelegate > CompileExecute ( )
@@ -113,13 +114,13 @@ public void OnUpdate(ref SystemState state)
113114 state . Dependency = sendJob . Schedule ( m_Request . Query , state . Dependency ) ;
114115 }
115116 }
116-
117+
117118 internal partial class UnifiedNetcodeUpdateSystem : SystemBase
118119 {
119120 public UnifiedNetcodeTransport Transport ;
120121
121122 public List < Connection > DisconnectQueue = new List < Connection > ( ) ;
122-
123+
123124 public void Disconnect ( Connection connection )
124125 {
125126 DisconnectQueue . Add ( connection ) ;
@@ -128,7 +129,7 @@ public void Disconnect(Connection connection)
128129 protected override void OnUpdate ( )
129130 {
130131 using var commandBuffer = new EntityCommandBuffer ( Allocator . Temp ) ;
131- foreach ( var ( request , rpc , entity ) in SystemAPI . Query < RefRO < ReceiveRpcCommandRequest > , RefRO < TransportRpc > > ( ) . WithEntityAccess ( ) )
132+ foreach ( var ( request , rpc , entity ) in SystemAPI . Query < RefRO < ReceiveRpcCommandRequest > , RefRO < TransportRpc > > ( ) . WithEntityAccess ( ) )
132133 {
133134 var connectionId = SystemAPI . GetComponent < NetworkId > ( request . ValueRO . SourceConnection ) . Value ;
134135
@@ -150,7 +151,7 @@ protected override void OnUpdate()
150151 DisconnectQueue . Clear ( ) ;
151152
152153 commandBuffer . Playback ( EntityManager ) ;
153-
154+
154155 }
155156 }
156157
@@ -162,7 +163,7 @@ internal class UnifiedNetcodeTransport : NetworkTransport
162163 public override ulong ServerClientId => ( ulong ) m_ServerClientId ;
163164
164165 private NetworkManager m_NetworkManager ;
165-
166+
166167 private IRealTimeProvider m_RealTimeProvider ;
167168
168169 private class ConnectionInfo
@@ -174,9 +175,9 @@ private class ConnectionInfo
174175 public ulong LastReceived ;
175176 public Dictionary < ulong , FixedBytes1280 > DeferredMessages ;
176177 }
177-
178+
178179 private Dictionary < int , ConnectionInfo > m_Connections ;
179-
180+
180181 internal void DispatchMessage ( int connectionId , in FixedBytes1280 buffer , ulong order )
181182 {
182183 var connectionInfo = m_Connections [ connectionId ] ;
@@ -197,7 +198,7 @@ internal void DispatchMessage(int connectionId, in FixedBytes1280 buffer, ulong
197198 connectionInfo . DeferredMessages [ order ] = buffer ;
198199 return ;
199200 }
200-
201+
201202 using var arr = FixedBytes1280 . ToNativeArray ( buffer ) ;
202203 var reader = new DataStreamReader ( arr ) ;
203204 if ( connectionInfo . ReceiveQueue == null )
@@ -246,15 +247,15 @@ public override unsafe void Send(ulong clientId, ArraySegment<byte> payload, Net
246247 {
247248 Buffer = new FixedBytes1280 ( ) ,
248249 } ;
249-
250+
250251 var writer = new DataStreamWriter ( FixedBytes1280 . GetUnsafePtr ( rpc . Buffer ) , k_MaxPacketSize ) ;
251252
252253 var amount = connectionInfo . SendQueue . FillWriterWithBytes ( ref writer , k_MaxPacketSize ) ;
253254 rpc . Buffer . Length = amount ;
254255 rpc . Order = ++ connectionInfo . LastSent ;
255256
256257 connectionInfo . Connection . SendOutOfBandMessage ( rpc ) ;
257-
258+
258259 connectionInfo . SendQueue . Consume ( amount ) ;
259260 }
260261 }
@@ -266,7 +267,7 @@ public override NetworkEvent PollEvent(out ulong clientId, out ArraySegment<byte
266267 receiveTime = 0 ;
267268 return NetworkEvent . Nothing ;
268269 }
269-
270+
270271 private void OnClientConnectedToServer ( Connection connection , NetCodeConnectionEvent connectionEvent )
271272 {
272273 m_Connections [ connection . NetworkId . Value ] = new ConnectionInfo
@@ -276,62 +277,62 @@ private void OnClientConnectedToServer(Connection connection, NetCodeConnectionE
276277 Connection = connection
277278 } ;
278279 m_ServerClientId = connection . NetworkId . Value ;
279- InvokeOnTransportEvent ( NetworkEvent . Connect , ( ulong ) connection . NetworkId . Value , default , m_RealTimeProvider . RealTimeSinceStartup ) ;
280+ InvokeOnTransportEvent ( NetworkEvent . Connect , ( ulong ) connection . NetworkId . Value , default , m_RealTimeProvider . RealTimeSinceStartup ) ;
280281 }
281-
282+
282283 private void OnServerNewClientConnection ( Connection connection , NetCodeConnectionEvent connectionEvent )
283284 {
284285 m_Connections [ connection . NetworkId . Value ] = new ConnectionInfo
285286 {
286287 ReceiveQueue = null ,
287288 SendQueue = new BatchedSendQueue ( BatchedSendQueue . MaximumMaximumCapacity ) ,
288289 Connection = connection
289- } ; ;
290- InvokeOnTransportEvent ( NetworkEvent . Connect , ( ulong ) connection . NetworkId . Value , default , m_RealTimeProvider . RealTimeSinceStartup ) ;
290+ } ; ;
291+ InvokeOnTransportEvent ( NetworkEvent . Connect , ( ulong ) connection . NetworkId . Value , default , m_RealTimeProvider . RealTimeSinceStartup ) ;
291292 }
292293
293- private const string InvalidRpcMessage = "An invalid RPC was received" ;
294- private const string HandshakeTimeoutMessage = "The connection was closed because the handshake timed out." ;
295- private const string ApprovalFailureMessage = "The connection was closed because the connection was not approved by the server." ;
296- private const string ApprovalTimeoutMessage = "The connection was closed because the connection approval process timed out." ;
297-
294+ private const string k_InvalidRpcMessage = "An invalid RPC was received" ;
295+ private const string k_HandshakeTimeoutMessage = "The connection was closed because the handshake timed out." ;
296+ private const string k_ApprovalFailureMessage = "The connection was closed because the connection was not approved by the server." ;
297+ private const string k_ApprovalTimeoutMessage = "The connection was closed because the connection approval process timed out." ;
298+
298299 private string GetDisconnectMessageFromNetworkStreamDisconnectReason ( NetworkStreamDisconnectReason reason )
299300 {
300301 switch ( reason )
301302 {
302303 case NetworkStreamDisconnectReason . ConnectionClose :
303304 return UnityTransportNotificationHandler . DisconnectedMessage ;
304- case NetworkStreamDisconnectReason . Timeout :
305+ case NetworkStreamDisconnectReason . Timeout :
305306 return UnityTransportNotificationHandler . TimeoutMessage ;
306- case NetworkStreamDisconnectReason . MaxConnectionAttempts :
307+ case NetworkStreamDisconnectReason . MaxConnectionAttempts :
307308 return UnityTransportNotificationHandler . MaxConnectionAttemptsMessage ;
308- case NetworkStreamDisconnectReason . ClosedByRemote :
309- return UnityTransportNotificationHandler . ClosedRemoteConnectionMessage ;
309+ case NetworkStreamDisconnectReason . ClosedByRemote :
310+ return UnityTransportNotificationHandler . ClosedRemoteConnectionMessage ;
310311 case NetworkStreamDisconnectReason . BadProtocolVersion :
311- return UnityTransportNotificationHandler . ProtocolErrorMessage ;
312+ return UnityTransportNotificationHandler . ProtocolErrorMessage ;
312313 case NetworkStreamDisconnectReason . InvalidRpc :
313- return InvalidRpcMessage ;
314+ return k_InvalidRpcMessage ;
314315 case NetworkStreamDisconnectReason . AuthenticationFailure :
315316 return UnityTransportNotificationHandler . AuthenticationFailureMessage ;
316317 case NetworkStreamDisconnectReason . ProtocolError :
317- return UnityTransportNotificationHandler . ProtocolErrorMessage ;
318+ return UnityTransportNotificationHandler . ProtocolErrorMessage ;
318319 case NetworkStreamDisconnectReason . HandshakeTimeout :
319- return HandshakeTimeoutMessage ;
320+ return k_HandshakeTimeoutMessage ;
320321 case NetworkStreamDisconnectReason . ApprovalFailure :
321- return ApprovalFailureMessage ;
322+ return k_ApprovalFailureMessage ;
322323 case NetworkStreamDisconnectReason . ApprovalTimeout :
323- return ApprovalTimeoutMessage ;
324+ return k_ApprovalTimeoutMessage ;
324325 }
325326 return "Unknown reason" ;
326327 }
327-
328+
328329 private DisconnectEvents GetDisconnectEventFromNetworkStreamDisconnectReason ( NetworkStreamDisconnectReason reason )
329330 {
330331 switch ( reason )
331332 {
332333 case NetworkStreamDisconnectReason . ConnectionClose :
333334 return DisconnectEvents . Disconnected ;
334- case NetworkStreamDisconnectReason . Timeout :
335+ case NetworkStreamDisconnectReason . Timeout :
335336 return DisconnectEvents . ProtocolTimeout ;
336337 case NetworkStreamDisconnectReason . MaxConnectionAttempts :
337338 return DisconnectEvents . MaxConnectionAttempts ;
@@ -361,12 +362,12 @@ private void OnClientDisconnectFromServer(Connection connection, NetCodeConnecti
361362 GetDisconnectEventFromNetworkStreamDisconnectReason ( connectionEvent . DisconnectReason ) ,
362363 GetDisconnectMessageFromNetworkStreamDisconnectReason ( connectionEvent . DisconnectReason )
363364 ) ;
364- InvokeOnTransportEvent ( NetworkEvent . Disconnect , ( ulong ) connection . NetworkId . Value , default , m_RealTimeProvider . RealTimeSinceStartup ) ;
365+ InvokeOnTransportEvent ( NetworkEvent . Disconnect , ( ulong ) connection . NetworkId . Value , default , m_RealTimeProvider . RealTimeSinceStartup ) ;
365366 }
366367
367368 private void OnServerClientDisconnected ( Connection connection , NetCodeConnectionEvent connectionEvent )
368369 {
369- InvokeOnTransportEvent ( NetworkEvent . Disconnect , ( ulong ) connection . NetworkId . Value , default , m_RealTimeProvider . RealTimeSinceStartup ) ;
370+ InvokeOnTransportEvent ( NetworkEvent . Disconnect , ( ulong ) connection . NetworkId . Value , default , m_RealTimeProvider . RealTimeSinceStartup ) ;
370371 }
371372
372373 public override bool StartClient ( )
0 commit comments