Skip to content

Commit de60e63

Browse files
committed
feat: 增加 ReceivedCallBack 回调方法
1 parent 31a2a95 commit de60e63

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/BootstrapBlazor/Services/TcpSocket/DataPackage/DataPackageHandlerBase.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ public abstract class DataPackageHandlerBase : IDataPackageHandler
1616
private Memory<byte> _lastReceiveBuffer = Memory<byte>.Empty;
1717

1818
/// <summary>
19-
/// 当接收数据处理完成后,回调该函数执行接收
19+
/// Gets or sets the callback function to handle received data.
2020
/// </summary>
21+
/// <remarks>The callback function should be designed to handle the received data efficiently and
22+
/// asynchronously. Ensure that the implementation does not block or perform long-running operations, as this may
23+
/// impact performance.</remarks>
2124
public Func<ReadOnlyMemory<byte>, ValueTask>? ReceivedCallBack { get; set; }
2225

2326
/// <summary>

src/BootstrapBlazor/Services/TcpSocket/DefaultTcpSocketClient.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class DefaultTcpSocketClient : ITcpSocketClient
2828

2929
public int ReceiveBufferSize { get; set; } = 1024 * 10;
3030

31+
public Func<ReadOnlyMemory<byte>, ValueTask>? ReceivedCallBack { get; set; }
32+
3133
public DefaultTcpSocketClient(string host, int port = 0)
3234
{
3335
LocalEndPoint = new IPEndPoint(GetIPAddress(host), port);
@@ -136,6 +138,11 @@ private async ValueTask ReceiveAsync()
136138
{
137139
buffer = buffer[..len];
138140

141+
if (ReceivedCallBack != null)
142+
{
143+
await ReceivedCallBack(buffer);
144+
}
145+
139146
if (_dataPackageHandler != null)
140147
{
141148
await _dataPackageHandler.ReceiveAsync(buffer);

src/BootstrapBlazor/Services/TcpSocket/ITcpSocketClient.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ public interface ITcpSocketClient : IDisposable
3030
/// specific local endpoint, this property may return <see langword="null"/>.</remarks>
3131
IPEndPoint LocalEndPoint { get; }
3232

33+
/// <summary>
34+
/// Gets or sets the callback function to handle received data.
35+
/// </summary>
36+
/// <remarks>The callback function should be designed to handle the received data efficiently and
37+
/// asynchronously. Ensure that the implementation does not block or perform long-running operations, as this may
38+
/// impact performance.</remarks>
39+
Func<ReadOnlyMemory<byte>, ValueTask>? ReceivedCallBack { get; set; }
40+
3341
/// <summary>
3442
/// Configures the data handler to process incoming data packages.
3543
/// </summary>

0 commit comments

Comments
 (0)