-
-
Notifications
You must be signed in to change notification settings - Fork 362
feat(SocketDataConverterCollections): add global converter configuration function #6449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1ea7877
feat: 增加 ISocketDataConverter 接口
ArgoZhang 180ee2f
feat: 增加 SocketDataConverterCollections 配置类
ArgoZhang 985fb9f
test: 增加单元测试
ArgoZhang eee2b6d
test: 更新单元测试
ArgoZhang 9f3726c
test: 增加单元测试
ArgoZhang 1eb0405
doc: 更新文档
ArgoZhang 3e69b8b
feat: 增加 SocketDataConverterCollections 配置项自动获取属性标签逻辑
ArgoZhang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| using Microsoft.Extensions.DependencyInjection; | ||
| using System.Reflection; | ||
| using System.Runtime.Versioning; | ||
| using System.Text; | ||
|
|
@@ -115,7 +116,7 @@ public static void SetDataPackageAdapter<TEntity>(this ITcpSocketClient client, | |
| /// </summary> | ||
| /// <remarks>This method sets up the <paramref name="client"/> to use the specified <paramref | ||
| /// name="adapter"/> for handling incoming data. If the <typeparamref name="TEntity"/> type is decorated with a <see | ||
| /// cref="SocketDataConverterAttribute"/>, the associated converter is used to transform the data before invoking | ||
| /// cref="SocketDataTypeConverterAttribute"/>, the associated converter is used to transform the data before invoking | ||
| /// the <paramref name="callback"/>. The callback is called with the converted entity or <see langword="null"/> if | ||
| /// conversion fails.</remarks> | ||
| /// <typeparam name="TEntity">The type of entity that the data package adapter will handle.</typeparam> | ||
|
|
@@ -131,27 +132,60 @@ public static void SetDataPackageAdapter<TEntity>(this ITcpSocketClient client, | |
| await adapter.HandlerAsync(buffer); | ||
| }; | ||
|
|
||
| ISocketDataConverter<TEntity>? converter = null; | ||
|
|
||
| var type = typeof(TEntity); | ||
| var converterType = type.GetCustomAttribute<SocketDataConverterAttribute>(); | ||
| var converterType = type.GetCustomAttribute<SocketDataTypeConverterAttribute>(); | ||
| if (converterType is { Type: not null }) | ||
| { | ||
| // 如果类型上有 SocketDataTypeConverterAttribute 特性则使用特性中指定的转换器 | ||
| if (Activator.CreateInstance(converterType.Type) is ISocketDataConverter<TEntity> socketDataConverter) | ||
| { | ||
| // 设置 DataPackageAdapter 的回调函数 | ||
| adapter.ReceivedCallBack = async buffer => | ||
| { | ||
| TEntity? ret = default; | ||
| if (socketDataConverter.TryConvertTo(buffer, out var t)) | ||
| { | ||
| ret = t; | ||
| } | ||
| await callback(ret); | ||
| }; | ||
| converter = socketDataConverter; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| // 如果没有特性则从 ITcpSocketClient 中的服务容器获取转换器 | ||
| converter = client.GetSocketDataConverter<TEntity>(); | ||
| } | ||
|
|
||
| if (converter == null) | ||
| { | ||
| // 设置正常回调 | ||
| adapter.ReceivedCallBack = async buffer => await callback(default); | ||
| } | ||
| else | ||
| { | ||
| // 设置转化器 | ||
| adapter.SetDataAdapterCallback(converter, callback); | ||
| } | ||
| } | ||
|
|
||
| private static void SetDataAdapterCallback<TEntity>(this IDataPackageAdapter adapter, ISocketDataConverter<TEntity> converter, Func<TEntity?, Task> callback) | ||
| { | ||
| adapter.ReceivedCallBack = async buffer => | ||
| { | ||
| TEntity? ret = default; | ||
| if (converter.TryConvertTo(buffer, out var t)) | ||
| { | ||
| ret = t; | ||
| } | ||
| await callback(ret); | ||
| }; | ||
| } | ||
|
|
||
| private static ISocketDataConverter<TEntity>? GetSocketDataConverter<TEntity>(this ITcpSocketClient client) | ||
| { | ||
| ISocketDataConverter<TEntity>? converter = null; | ||
| if (client is IServiceProvider provider) | ||
| { | ||
| var converters = provider.GetRequiredService<IOptions<SocketDataConverterCollections>>().Value; | ||
| if (converters.TryGetTypeConverter<TEntity>(out var v)) | ||
| { | ||
| converter = v; | ||
| } | ||
| } | ||
| return converter; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.