Skip to content

Commit 4dca0e6

Browse files
committed
feat: update codes for lower language version
1 parent f43a96b commit 4dca0e6

File tree

2 files changed

+14
-25
lines changed

2 files changed

+14
-25
lines changed

SimpleNetworkManager.NET/Network/BaseClientConnection.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

SimpleNetworkManager.NET/Network/TcpTransport/TcpClientConnection.cs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public class TcpClientConnection : BaseClientConnection
1717
private readonly CancellationTokenSource _cancellationTokenSource;
1818
private readonly SemaphoreSlim _sendSemaphore;
1919

20-
private UniTask? _receiveTask;
2120
private bool _isConnected;
2221

2322
public TcpClient TcpClient => _tcpClient;
@@ -312,10 +311,10 @@ internal override async UniTask SendMessageAsync(BaseMessage message)
312311
}
313312
}
314313

315-
internal override async UniTask DisconnectAsync()
314+
internal override UniTask DisconnectAsync()
316315
{
317316
if (_disposed || !_isConnected)
318-
return;
317+
return UniTask.CompletedTask;
319318

320319
_logger.LogInformation("Disconnecting client {ConnectionId}", ConnectionId);
321320

@@ -332,25 +331,9 @@ internal override async UniTask DisconnectAsync()
332331
_logger.LogWarning(ex, "Error during client {ConnectionId} disconnection", ConnectionId);
333332
}
334333

335-
// Wait for receive task to complete
336-
if (_receiveTask != null)
337-
{
338-
try
339-
{
340-
await _receiveTask.Value;
341-
}
342-
catch (OperationCanceledException)
343-
{
344-
// Expected when cancellation is requested
345-
}
346-
catch (Exception ex)
347-
{
348-
_logger.LogWarning(ex, "Error waiting for receive task completion for client {ConnectionId}", ConnectionId);
349-
}
350-
}
351-
352334
// Raise disconnected event
353335
OnDisconnected();
336+
return UniTask.CompletedTask;
354337
}
355338

356339
public override async UniTask RejectConnectionAsync(ConnectionErrorTypes errorType, string errorText, bool shouldRetry, int retryDelayMs)

0 commit comments

Comments
 (0)