Skip to content

Commit bedca79

Browse files
committed
feat: 增加 SetDataPackageAdapter 方法
1 parent f84bf0e commit bedca79

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

src/BootstrapBlazor/Extensions/ITcpSocketClientExtensions.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,42 @@ public static void SetDataPackageAdapter(this ITcpSocketClient client, IDataPack
7474
// 设置 DataPackageAdapter 的回调函数
7575
adapter.ReceivedCallBack = buffer => callback(buffer);
7676
}
77+
78+
/// <summary>
79+
/// Configures the specified <see cref="ITcpSocketClient"/> to use a custom data package adapter and a callback
80+
/// function for processing received data.
81+
/// </summary>
82+
/// <remarks>This method sets up the <paramref name="client"/> to use the provided <paramref
83+
/// name="adapter"/> for handling incoming data. The adapter processes the raw data received by the client and
84+
/// attempts to convert it into an instance of <typeparamref name="TEntity"/>. If the conversion is successful, the
85+
/// <paramref name="callback"/> is invoked with the converted entity; otherwise, it is invoked with <see
86+
/// langword="null"/>.</remarks>
87+
/// <typeparam name="TEntity">The type of the entity that the data package adapter will attempt to convert the received data into.</typeparam>
88+
/// <param name="client">The <see cref="ITcpSocketClient"/> instance to configure.</param>
89+
/// <param name="adapter">The <see cref="IDataPackageAdapter"/> instance responsible for handling and processing incoming data.</param>
90+
/// <param name="callback">A callback function to be invoked with the processed data of type <typeparamref name="TEntity"/>. The callback
91+
/// receives <see langword="null"/> if the data cannot be converted to <typeparamref name="TEntity"/>.</param>
92+
public static void SetDataPackageAdapter<TEntity>(this ITcpSocketClient client, IDataPackageAdapter adapter, Func<TEntity?, Task> callback)
93+
{
94+
// 设置 ITcpSocketClient 的回调函数
95+
client.ReceivedCallBack = async buffer =>
96+
{
97+
// 将接收到的数据传递给 DataPackageAdapter 进行数据处理合规数据触发 ReceivedCallBack 回调
98+
await adapter.HandlerAsync(buffer);
99+
};
100+
101+
// 设置 DataPackageAdapter 的回调函数
102+
adapter.ReceivedCallBack = async buffer =>
103+
{
104+
TEntity? ret = default;
105+
if (adapter.TryConvertTo(buffer, out var t))
106+
{
107+
if (t is TEntity entity)
108+
{
109+
ret = entity;
110+
}
111+
}
112+
await callback(ret);
113+
};
114+
}
77115
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ public virtual async ValueTask HandlerAsync(ReadOnlyMemory<byte> data, Cancellat
4444
}
4545
}
4646

47+
/// <summary>
48+
/// <inheritdoc/>
49+
/// </summary>
50+
/// <param name="data"></param>
51+
/// <param name="entity"></param>
52+
/// <returns></returns>
53+
public virtual bool TryConvertTo(ReadOnlyMemory<byte> data, [NotNullWhen(true)] out object? entity)
54+
{
55+
entity = null;
56+
return false;
57+
}
58+
4759
/// <summary>
4860
/// Handles incoming data by invoking a callback method, if one is defined.
4961
/// </summary>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
namespace BootstrapBlazor.Components;
7+
8+
/// <summary>
9+
/// Provides a base implementation for adapting data packages between different systems or formats.
10+
/// </summary>
11+
/// <remarks>This abstract class serves as a foundation for implementing custom data package adapters. It defines
12+
/// common methods for sending, receiving, and handling data packages, as well as a property for accessing the
13+
/// associated data package handler. Derived classes should override the virtual methods to provide specific behavior
14+
/// for handling data packages.</remarks>
15+
public abstract class DataPackageAdapterBase<TEntity> : DataPackageAdapter
16+
{
17+
/// <summary>
18+
/// <inheritdoc/>
19+
/// </summary>
20+
/// <param name="data"></param>
21+
/// <param name="entity"></param>
22+
/// <returns></returns>
23+
public abstract bool TryConvertTo(ReadOnlyMemory<byte> data, [NotNullWhen(true)] out TEntity? entity);
24+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,16 @@ public interface IDataPackageAdapter
3939
/// <returns>A <see cref="ValueTask"/> representing the asynchronous operation. The task completes when the data has been
4040
/// successfully received and processed.</returns>
4141
ValueTask HandlerAsync(ReadOnlyMemory<byte> data, CancellationToken token = default);
42+
43+
/// <summary>
44+
/// Attempts to convert the specified binary data into an object representation.
45+
/// </summary>
46+
/// <remarks>This method does not throw exceptions for invalid input. Instead, it returns <see
47+
/// langword="false"/> if the conversion fails, and <paramref name="entity"/> will be <see
48+
/// langword="null"/>.</remarks>
49+
/// <param name="data">The binary data to be converted. Must not be empty.</param>
50+
/// <param name="entity">When this method returns <see langword="true"/>, contains the converted object. When this method returns <see
51+
/// langword="false"/>, the value is <see langword="null"/>.</param>
52+
/// <returns><see langword="true"/> if the conversion was successful; otherwise, <see langword="false"/>.</returns>
53+
bool TryConvertTo(ReadOnlyMemory<byte> data, [NotNullWhen(true)] out object? entity);
4254
}

0 commit comments

Comments
 (0)