Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/BootstrapBlazor/Services/TcpSocket/TcpSocketClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,14 @@ public async ValueTask<bool> ConnectAsync(IPEndPoint endPoint, CancellationToken
}
catch (OperationCanceledException ex)
{
var message = token.IsCancellationRequested
? $"TCP Socket connect operation was canceled from {LocalEndPoint} to {endPoint}"
: $"TCP Socket connect operation timed out from {LocalEndPoint} to {endPoint}";
Log(LogLevel.Warning, ex, message);
if (token.IsCancellationRequested)
{
Log(LogLevel.Warning, ex, $"TCP Socket connect operation was canceled from {LocalEndPoint} to {endPoint}");
}
else
{
Log(LogLevel.Warning, ex, $"TCP Socket connect operation timed out from {LocalEndPoint} to {endPoint}");
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -241,9 +245,14 @@ private async ValueTask<int> ReceiveCoreAsync(ISocketClientProvider client, Memo
}
catch (OperationCanceledException ex)
{
Log(LogLevel.Warning, ex, token.IsCancellationRequested
? $"TCP Socket receive operation canceled from {_localEndPoint} to {_remoteEndPoint}"
: $"TCP Socket receive operation timed out from {_localEndPoint} to {_remoteEndPoint}");
if (token.IsCancellationRequested)
{
Log(LogLevel.Warning, ex, $"TCP Socket receive operation canceled from {_localEndPoint} to {_remoteEndPoint}");
}
else
{
Log(LogLevel.Warning, ex, $"TCP Socket receive operation timed out from {_localEndPoint} to {_remoteEndPoint}");
}
}
catch (Exception ex)
{
Expand Down
26 changes: 16 additions & 10 deletions test/UnitTest/Services/TcpSocketFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,14 @@ public async Task ReceiveAsync_Ok()
var send = await client.SendAsync(data);
Assert.True(send);

// 未设置数据处理器未开启自动接收时,调用 ReceiveAsync 方法获取数据
// 需要自己处理粘包分包和业务问题
var payload = await client.ReceiveAsync();
Assert.Equal(payload.ToArray(), [1, 2, 3, 4, 5]);
Assert.Equal([1, 2, 3, 4, 5], payload.ToArray());

// 由于服务器端模拟了拆包发送第二段数据,所以这里可以再次调用 ReceiveAsync 方法获取第二段数据
payload = await client.ReceiveAsync();
Assert.Equal([3, 4], payload.ToArray());
}

[Fact]
Expand Down Expand Up @@ -290,7 +296,7 @@ public async Task ReceiveAsync_Error()
await client.SendAsync(data);

await tcs.Task;
Assert.Equal(buffer.ToArray(), [1, 2, 3, 4, 5]);
Assert.Equal([1, 2, 3, 4, 5], buffer.ToArray());

// 关闭连接
StopTcpServer(server);
Expand Down Expand Up @@ -330,7 +336,7 @@ public async Task FixLengthDataPackageHandler_Ok()
Assert.True(result);

await tcs.Task;
Assert.Equal(receivedBuffer.ToArray(), [1, 2, 3, 4, 5, 3, 4]);
Assert.Equal([1, 2, 3, 4, 5, 3, 4], receivedBuffer.ToArray());

// 关闭连接
await client.CloseAsync();
Expand Down Expand Up @@ -372,7 +378,7 @@ public async Task FixLengthDataPackageHandler_Sticky()
await tcs.Task;

// 验证接收到的数据
Assert.Equal(receivedBuffer.ToArray(), [1, 2, 3, 4, 5, 3, 4]);
Assert.Equal([1, 2, 3, 4, 5, 3, 4], receivedBuffer.ToArray());

// 重置接收缓冲区
receivedBuffer = new byte[1024];
Expand All @@ -382,12 +388,12 @@ public async Task FixLengthDataPackageHandler_Sticky()
await tcs.Task;

// 验证第二次收到的数据
Assert.Equal(receivedBuffer.ToArray(), [2, 2, 3, 4, 5, 6, 7]);
Assert.Equal([2, 2, 3, 4, 5, 6, 7], receivedBuffer.ToArray());
tcs = new TaskCompletionSource();
await tcs.Task;

// 验证第三次收到的数据
Assert.Equal(receivedBuffer.ToArray(), [3, 2, 3, 4, 5, 6, 7]);
Assert.Equal([3, 2, 3, 4, 5, 6, 7], receivedBuffer.ToArray());

// 关闭连接
await client.CloseAsync();
Expand Down Expand Up @@ -428,15 +434,15 @@ public async Task DelimiterDataPackageHandler_Ok()
await tcs.Task;

// 验证接收到的数据
Assert.Equal(receivedBuffer.ToArray(), [1, 2, 3, 4, 5, 13, 10]);
Assert.Equal([1, 2, 3, 4, 5, 13, 10], receivedBuffer.ToArray());

// 等待第二次数据
receivedBuffer = new byte[1024];
tcs = new TaskCompletionSource();
await tcs.Task;

// 验证接收到的数据
Assert.Equal(receivedBuffer.ToArray(), [5, 6, 13, 10]);
Assert.Equal([5, 6, 13, 10], receivedBuffer.ToArray());

// 关闭连接
await client.CloseAsync();
Expand Down Expand Up @@ -480,8 +486,8 @@ public async Task TryConvertTo_Ok()
await tcs.Task;

Assert.NotNull(entity);
Assert.Equal(entity.Header, [1, 2, 3, 4, 5]);
Assert.Equal(entity.Body, [3, 4]);
Assert.Equal([1, 2, 3, 4, 5], entity.Header);
Assert.Equal([3, 4], entity.Body);

// 测试异常流程
var adapter2 = new DataPackageAdapter();
Expand Down
Loading