@@ -12,10 +12,10 @@ namespace Insthync.SimpleNetworkManager.NET.Network
1212{
1313 public abstract class BaseClientConnection : IDisposable
1414 {
15- private static uint s_connectionIdCounter = 0 ;
15+ private static int s_connectionIdCounter = 0 ;
1616 private static ConcurrentQueue < uint > s_unassignedConnectionIds = new ConcurrentQueue < uint > ( ) ;
1717
18- private static uint s_requestIdCounter = 0 ;
18+ private static int s_requestIdCounter = 0 ;
1919 private static ConcurrentQueue < uint > s_unassignedRequestIds = new ConcurrentQueue < uint > ( ) ;
2020
2121 protected readonly ILogger < BaseClientConnection > _logger ;
@@ -37,17 +37,23 @@ public BaseClientConnection(ILogger<BaseClientConnection> logger)
3737 _pendingResponses = new ConcurrentDictionary < uint , BaseResponseMessage > ( ) ;
3838 }
3939
40+ private static uint InterlockedIncrementUInt ( ref int location )
41+ {
42+ Interlocked . Increment ( ref location ) ;
43+ return location < 0 ? ( uint ) ( location + ( long ) uint . MaxValue + 1 ) : ( uint ) location ;
44+ }
45+
4046 private static uint GetNewConnectionId ( )
4147 {
4248 if ( ! s_unassignedConnectionIds . TryDequeue ( out uint connectionId ) )
43- connectionId = Interlocked . Increment ( ref s_connectionIdCounter ) ;
49+ connectionId = InterlockedIncrementUInt ( ref s_connectionIdCounter ) ;
4450 return connectionId ;
4551 }
4652
4753 private static uint GetNewRequestId ( )
4854 {
4955 if ( ! s_unassignedRequestIds . TryDequeue ( out uint requestId ) )
50- requestId = Interlocked . Increment ( ref s_requestIdCounter ) ;
56+ requestId = InterlockedIncrementUInt ( ref s_requestIdCounter ) ;
5157 return requestId ;
5258 }
5359
@@ -117,7 +123,7 @@ internal async UniTask<TResponse> SendRequestAsync<TResponse>(BaseRequestMessage
117123 throw new TimeoutException ( $ "Request timed out after { timeoutMs } milliseconds (RequestId: { requestId } ).") ;
118124 }
119125
120- if ( response is not TResponse castedResponse )
126+ if ( ! ( response is TResponse castedResponse ) )
121127 {
122128 throw new InvalidOperationException ( $ "Response type mismatch. Expected { typeof ( TResponse ) . Name } , got { response . GetType ( ) . Name } ") ;
123129 }
0 commit comments