Skip to content

Commit ea1c142

Browse files
authored
feat(ITcpSocketClient): add SendAsync extensions method (#6269)
* doc: 更新粘包分包文档 * feat: 增加 SendAsync 扩展方法 * test: 增加单元测试
1 parent 4ce223e commit ea1c142

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

src/BootstrapBlazor.Server/Components/Samples/SocketFactories.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ private ITcpSocketFactory? TcpSocketFactory { get; set; }</Pre>
3232
<p>数据处理器设计初衷就是为了契合通讯协议大大简化我们开发逻辑,我们已通讯协议每次通讯电文均为 <b>4</b> 位定长举例说明,在实际的通讯过程中,我们接收到的通讯数据存在粘包或者分包的现象</p>
3333

3434
<ul class="ul-demo">
35-
<li><b>粘包</b>:比如我们期望收到 <b>1234</b> 四个字符,实际上我们接收到的是 <b>123412</b> 多出来的 <b>12</b> 其实是下一个通讯电文的内容,相邻两个通讯数据包的粘连称为<b>粘包</b></li>
36-
<li><b>分包</b>:比如我们期望收到 <b>1234</b> 四个字符,实际上我们接收到的是 <b>12</b> 和 <b>34</b> 两个数据包,这种情况称为<b>分包</b></li>
35+
<li><b>粘包</b>:比如我们期望收到 <b>1234</b> 四个字符,实际上我们接收到的是 <b>123412</b> 多出来的 <b>12</b> 其实是下一个数据包的内容,我们需要截取前 4 位数据作为一个数据包才能正确处理数据,这种相邻两个通讯数据包的粘连称为<b>粘包</b></li>
36+
<li><b>分包</b>:比如我们期望收到 <b>1234</b> 四个字符,实际上我们可能分两次接收到,分别是 <b>12</b> 和 <b>34</b>,我们需要将两个数据包拼接成一个才能正确的处理数据。这种情况称为<b>分包</b></li>
3737
</ul>
3838

3939
<p>我们内置了一些常用的数据处理类 <code>IDataPackageHandler</code> 接口为数据包处理接口,虚类 <code>DataPackageHandlerBase</code> 作为数据处理器基类已经内置了 <b>粘包</b> <b>分包</b> 的逻辑,继承此类后专注自己处理的业务即可</p>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
using System.Text;
7+
8+
namespace BootstrapBlazor.Components;
9+
10+
/// <summary>
11+
/// <see cref="ITcpSocketClient"/> 扩展方法类
12+
/// </summary>
13+
public static class ITcpSocketClientExtensions
14+
{
15+
/// <summary>
16+
/// Sends the specified string content to the connected TCP socket client asynchronously.
17+
/// </summary>
18+
/// <remarks>This method converts the provided string content into a byte array using the specified
19+
/// encoding (or UTF-8 by default) and sends it to the connected TCP socket client. Ensure the client is connected
20+
/// before calling this method.</remarks>
21+
/// <param name="client">The TCP socket client to which the content will be sent. Cannot be <see langword="null"/>.</param>
22+
/// <param name="content">The string content to send. Cannot be <see langword="null"/> or empty.</param>
23+
/// <param name="encoding">The character encoding to use for converting the string content to bytes. If <see langword="null"/>, UTF-8
24+
/// encoding is used by default.</param>
25+
/// <param name="token">A <see cref="CancellationToken"/> to observe while waiting for the operation to complete.</param>
26+
/// <returns>A <see cref="ValueTask{TResult}"/> that represents the asynchronous operation. The result is <see
27+
/// langword="true"/> if the content was sent successfully; otherwise, <see langword="false"/>.</returns>
28+
public static ValueTask<bool> SendAsync(this ITcpSocketClient client, string content, Encoding? encoding = null, CancellationToken token = default)
29+
{
30+
var buffer = encoding?.GetBytes(content) ?? Encoding.UTF8.GetBytes(content);
31+
return client.SendAsync(buffer, token);
32+
}
33+
}

test/UnitTest/Services/TcpSocketFactoryTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.Extensions.Logging;
77
using System.Net;
88
using System.Net.Sockets;
9+
using System.Text;
910

1011
namespace UnitTest.Services;
1112

@@ -94,8 +95,7 @@ public async Task SendAsync_Cancel()
9495
var cst = new CancellationTokenSource();
9596
cst.Cancel();
9697

97-
var data = new ReadOnlyMemory<byte>([1, 2, 3, 4, 5]);
98-
var result = await client.SendAsync(data, cst.Token);
98+
var result = await client.SendAsync("test", null, cst.Token);
9999
Assert.False(result);
100100

101101
// 设置延时发送适配器
@@ -110,7 +110,7 @@ public async Task SendAsync_Cancel()
110110
// 测试发送失败逻辑
111111
_ = Task.Run(async () =>
112112
{
113-
sendResult = await client.SendAsync(data);
113+
sendResult = await client.SendAsync("test", Encoding.UTF8);
114114
tcs.SetResult();
115115
});
116116

0 commit comments

Comments
 (0)