Skip to content
This repository was archived by the owner on Nov 13, 2025. It is now read-only.

Commit 52e6da0

Browse files
authored
feat: make SendTimeout effect (#32)
* feat: 增加 SendTimeout 参数生效问题 * test: 增加 ReceiveAsync 单元测试代码覆盖率 * test: 增加 SendTimeout 单元测试 * doc: 项目文件格式化 * chore: bump version 9.0.11 * refactor: 统一 Options 类
1 parent 65af74a commit 52e6da0

4 files changed

Lines changed: 23 additions & 15 deletions

File tree

src/Longbow.TcpSocket/Interface/DefaultTcpSocketClient.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ sealed class DefaultTcpSocketClient(IOptions<TcpSocketClientOptions> options) :
2727
/// <summary>
2828
/// <inheritdoc/>
2929
/// </summary>
30-
public IPEndPoint LocalEndPoint => _localEndPoint ?? _options.LocalEndPoint;
30+
public IPEndPoint LocalEndPoint => _localEndPoint ?? Options.LocalEndPoint;
3131

3232
/// <summary>
3333
/// <inheritdoc/>
@@ -97,7 +97,7 @@ public async ValueTask<bool> ConnectAsync(IPEndPoint endPoint, CancellationToken
9797
ret = true;
9898
}
9999

100-
if (_options.IsAutoReceive)
100+
if (Options.IsAutoReceive)
101101
{
102102
_ = Task.Run(AutoReceiveAsync, CancellationToken.None).ConfigureAwait(false);
103103
}
@@ -117,7 +117,7 @@ private async ValueTask AutoReceiveAsync()
117117
// 自动接收方法
118118
_autoReceiveTokenSource ??= new();
119119

120-
using var block = MemoryPool<byte>.Shared.Rent(_options.ReceiveBufferSize);
120+
using var block = MemoryPool<byte>.Shared.Rent(Options.ReceiveBufferSize);
121121
var buffer = block.Memory;
122122
while (_autoReceiveTokenSource is { IsCancellationRequested: false })
123123
{
@@ -133,10 +133,10 @@ private async ValueTask<int> ReceiveCoreAsync(Memory<byte> buffer, CancellationT
133133
if (_client is { Connected: true })
134134
{
135135
var receiveToken = token;
136-
if (_options.ReceiveTimeout > 0)
136+
if (Options.ReceiveTimeout > 0)
137137
{
138138
// 设置接收超时时间
139-
using var receiveTokenSource = new CancellationTokenSource(_options.ReceiveTimeout);
139+
using var receiveTokenSource = new CancellationTokenSource(Options.ReceiveTimeout);
140140
using var link = CancellationTokenSource.CreateLinkedTokenSource(receiveToken, receiveTokenSource.Token);
141141
receiveToken = link.Token;
142142
}
@@ -178,7 +178,14 @@ public async ValueTask<bool> SendAsync(ReadOnlyMemory<byte> data, CancellationTo
178178
var ret = false;
179179
if (_sender != null)
180180
{
181-
ret = await _sender.SendAsync(data, token);
181+
var sendToken = token;
182+
if (Options.SendTimeout > 0)
183+
{
184+
using var sendTokenSource = new CancellationTokenSource(Options.SendTimeout);
185+
using var link = CancellationTokenSource.CreateLinkedTokenSource(token, sendTokenSource.Token);
186+
sendToken = link.Token;
187+
}
188+
ret = await _sender.SendAsync(data, sendToken);
182189
}
183190

184191
return ret;

src/Longbow.TcpSocket/Longbow.TcpSocket.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<Version>9.0.10</Version>
4+
<Version>9.0.11</Version>
55
</PropertyGroup>
66

77
<PropertyGroup>

test/UnitTestTcpSocket/TcpSocketFactoryTest.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public async Task SendAsync_Ok()
7777
// 测试正常电文
7878
cst.Dispose();
7979
cst = new();
80+
client.Options.SendTimeout = 1000;
8081
var result = await client.SendAsync("test", Encoding.UTF8, cst.Token);
8182
Assert.True(result);
8283

@@ -132,9 +133,8 @@ public async Task ReceiveAsync_Zero()
132133
var server = StartTcpServer(port, MockZeroPackageAsync);
133134
await client.ConnectAsync("localhost", port);
134135

135-
var buffer = new byte[10];
136-
var len = await client.ReceiveAsync(buffer);
137-
Assert.Equal(0, len);
136+
var buffer = await client.ReceiveAsync();
137+
Assert.Equal(ReadOnlyMemory<byte>.Empty, buffer);
138138
server.Stop();
139139
}
140140

@@ -190,8 +190,9 @@ public async Task ReceiveAsync_Ok()
190190
Assert.Equal([1, 2, 3, 4, 5], buffer[0..len]);
191191

192192
// 由于服务器端模拟了拆包发送第二段数据,所以这里可以再次调用 ReceiveAsync 方法获取第二段数据
193-
len = await client.ReceiveAsync(buffer);
194-
Assert.Equal([3, 4], buffer[0..len]);
193+
// 调用扩展方法直接获得到接收数据
194+
var payload = await client.ReceiveAsync();
195+
Assert.Equal([3, 4], payload.ToArray());
195196
}
196197

197198
[Fact]

test/UnitTestTcpSocket/UnitTestTcpSocket.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
<Using Include="Longbow.Sockets.DataHandlers" />
3030
<Using Include="Longbow.Sockets.DataConverters" />
3131
<Using Include="Longbow.TcpSocket" />
32-
<Using Include="System.Diagnostics.CodeAnalysis"/>
33-
<Using Include="Xunit"/>
34-
<Using Include="Xunit.Abstractions"/>
32+
<Using Include="System.Diagnostics.CodeAnalysis" />
33+
<Using Include="Xunit" />
34+
<Using Include="Xunit.Abstractions" />
3535
</ItemGroup>
3636

3737
</Project>

0 commit comments

Comments
 (0)