Skip to content

Commit c1346b4

Browse files
committed
refactor: 数据处理器增加数据转化器适配
1 parent c15164d commit c1346b4

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

src/BootstrapBlazor/Extensions/ITcpSocketClientExtensions.cs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,18 @@ public static void SetDataPackageAdapter(this ITcpSocketClient client, IDataPack
7676
}
7777

7878
/// <summary>
79-
/// Configures the specified <see cref="ITcpSocketClient"/> to use a custom data package adapter and a callback
80-
/// function for processing received data.
79+
/// Configures the specified <see cref="ITcpSocketClient"/> to use a data package adapter and a callback function
80+
/// for processing received data.
8181
/// </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)
82+
/// <remarks>This method sets up the <paramref name="client"/> to process incoming data using the
83+
/// specified <paramref name="adapter"/> and <paramref name="socketDataConverter"/>. The <paramref
84+
/// name="callback"/> is called with the converted entity whenever data is received.</remarks>
85+
/// <typeparam name="TEntity">The type of the entity that the data will be converted to.</typeparam>
86+
/// <param name="client">The TCP socket client to configure.</param>
87+
/// <param name="adapter">The data package adapter responsible for handling incoming data.</param>
88+
/// <param name="socketDataConverter">The converter used to transform the received data into the specified entity type.</param>
89+
/// <param name="callback">The callback function to be invoked with the converted entity.</param>
90+
public static void SetDataPackageAdapter<TEntity>(this ITcpSocketClient client, IDataPackageAdapter adapter, ISocketDataConverter<TEntity> socketDataConverter, Func<TEntity?, Task> callback)
9391
{
9492
// 设置 ITcpSocketClient 的回调函数
9593
client.ReceivedCallBack = async buffer =>
@@ -102,12 +100,9 @@ public static void SetDataPackageAdapter<TEntity>(this ITcpSocketClient client,
102100
adapter.ReceivedCallBack = async buffer =>
103101
{
104102
TEntity? ret = default;
105-
if (adapter.TryConvertTo(buffer, out var t))
103+
if (socketDataConverter.TryConvertTo(buffer, out var t))
106104
{
107-
if (t is TEntity entity)
108-
{
109-
ret = entity;
110-
}
105+
ret = t;
111106
}
112107
await callback(ret);
113108
};

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,17 @@ public virtual async ValueTask HandlerAsync(ReadOnlyMemory<byte> data, Cancellat
4848
/// <inheritdoc/>
4949
/// </summary>
5050
/// <param name="data"></param>
51+
/// <param name="socketDataConverter"></param>
5152
/// <param name="entity"></param>
5253
/// <returns></returns>
53-
public virtual bool TryConvertTo(ReadOnlyMemory<byte> data, [NotNullWhen(true)] out object? entity)
54+
public virtual bool TryConvertTo<TEntity>(ReadOnlyMemory<byte> data, ISocketDataConverter<TEntity> socketDataConverter, out TEntity? entity)
5455
{
55-
entity = null;
56-
return false;
56+
entity = default;
57+
if (socketDataConverter.TryConvertTo(data, out var v))
58+
{
59+
entity = v;
60+
}
61+
return entity != null;
5762
}
5863

5964
/// <summary>

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ public interface IDataPackageAdapter
4141
ValueTask HandlerAsync(ReadOnlyMemory<byte> data, CancellationToken token = default);
4242

4343
/// <summary>
44-
/// Attempts to convert the specified binary data into an object representation.
44+
/// Attempts to convert the specified byte data into an entity of type <typeparamref name="TEntity"/>.
4545
/// </summary>
46-
/// <remarks>This method does not throw an exception if the conversion fails. Instead, it returns <see
47-
/// langword="false"/> and sets <paramref name="entity"/> to <see langword="null"/>.</remarks>
48-
/// <param name="data">The binary data to be converted. Must not be empty.</param>
49-
/// <param name="entity">When this method returns <see langword="true"/>, contains the converted object. When this method returns <see
50-
/// langword="false"/>, contains <see langword="null"/>.</param>
46+
/// <remarks>This method does not throw an exception if the conversion fails. Instead, it returns <see
47+
/// langword="false"/> and sets <paramref name="entity"/> to its default value.</remarks>
48+
/// <typeparam name="TEntity">The type of the entity to convert the data to.</typeparam>
49+
/// <param name="data">The byte data to be converted.</param>
50+
/// <param name="socketDataConverter">The converter used to transform the byte data into an entity.</param>
51+
/// <param name="entity">When this method returns, contains the converted entity if the conversion was successful; otherwise, the default
52+
/// value for the type of the entity.</param>
5153
/// <returns><see langword="true"/> if the conversion was successful; otherwise, <see langword="false"/>.</returns>
52-
bool TryConvertTo(ReadOnlyMemory<byte> data, [NotNullWhen(true)] out object? entity);
54+
bool TryConvertTo<TEntity>(ReadOnlyMemory<byte> data, ISocketDataConverter<TEntity> socketDataConverter, out TEntity? entity);
5355
}

0 commit comments

Comments
 (0)