-
-
Notifications
You must be signed in to change notification settings - Fork 363
test(DataPackageAdapter): update ReceiveAsync comment #6361
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
18 commits
Select commit
Hold shift + click to select a range
6cba73e
refactor: 移除 ReceiveAsync 方法
ArgoZhang b72ddfc
Revert "refactor: 移除 ReceiveAsync 方法"
ArgoZhang fa8abd7
refactor: 调整目录结构
ArgoZhang a2f2f69
test: 增加单元测试注释
ArgoZhang 39ac0a3
revert: 撤销接收方法
ArgoZhang b8f2c6e
refactor: 代码重构
ArgoZhang cf99565
test: 更新单元测试
ArgoZhang f77ddb7
Merge branch 'main' into refactor-socket
ArgoZhang c466d8e
doc: 增加手动接收数据示例
ArgoZhang 886a276
doc: 更改关键字
ArgoZhang d86c04c
doc: 增加一发一收的模拟服务
ArgoZhang aceb9ae
doc: 更新手动接收示例
ArgoZhang 32475af
doc: 更新说明文档
ArgoZhang 5a14528
doc: 更新示例
ArgoZhang 6e66e54
doc: 更新图标
ArgoZhang 991471e
test: 更新单元测试
ArgoZhang d922c51
test: 增加连接超时单元测试
ArgoZhang 4c74601
doc: 更新日志格式
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
4 changes: 2 additions & 2 deletions
4
...Components/Samples/Sockets/Receives.razor → ...onents/Samples/Sockets/AutoReceives.razor
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
34 changes: 34 additions & 0 deletions
34
src/BootstrapBlazor.Server/Components/Samples/Sockets/ManualReceives.razor
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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| @page "/socket/manual-receive" | ||
| @inject IStringLocalizer<ManualReceives> Localizer | ||
|
|
||
| <h3>@Localizer["ReceivesTitle"]</h3> | ||
| <h4>@Localizer["ReceivesDescription"]</h4> | ||
|
|
||
| <Notice></Notice> | ||
|
|
||
| <DemoBlock Title="@Localizer["NormalTitle"]" | ||
| Introduction="@Localizer["NormalIntro"]" | ||
| Name="Normal" ShowCode="false"> | ||
| <p>本例中连接一个模拟时间同步服务,采用一发一收的方式进行通讯,连接后发送查询电文,接收到服务器端响应时间戳电文数据</p> | ||
| <ul class="ul-demo"> | ||
| <li>点击 <b>连接</b> 按钮后通过 <code>ITcpSocketFactory</code> 服务实例创建的 <code>ITcpSocketClient</code> 对象连接到网站模拟 <code>TcpServer</code></li> | ||
| <li>点击 <b>断开</b> 按钮调用 <code>CloseAsync</code> 方法断开 Socket 连接</li> | ||
| <li>点击 <b>发送</b> 按钮调用 <code>SendAsync</code> 方法发送请求数据</li> | ||
| </ul> | ||
| <p>使用 <code>ReceiveAsync</code> 方法主动接收数据</p> | ||
| <div class="row form-inline g-3"> | ||
| <div class="col-12 col-sm-6"> | ||
| <Button Text="连接" Icon="fa-solid fa-play" | ||
| OnClick="OnConnectAsync" IsDisabled="@_client.IsConnected"></Button> | ||
| <Button Text="断开" Icon="fa-solid fa-stop" class="ms-2" | ||
| OnClick="OnCloseAsync" IsDisabled="@(!_client.IsConnected)"></Button> | ||
| <Button Text="发送" Icon="fa-solid fa-paper-plane" class="ms-2" IsAsync="true" | ||
| OnClick="OnSendAsync" IsDisabled="@(!_client.IsConnected)"></Button> | ||
| </div> | ||
| <div class="col-12"> | ||
| <Console Items="@_items" Height="496" HeaderText="接收数据(间隔 10 秒)" | ||
| ShowAutoScroll="true" OnClear="@OnClear"></Console> | ||
| </div> | ||
| </div> | ||
| </DemoBlock> | ||
|
|
89 changes: 89 additions & 0 deletions
89
src/BootstrapBlazor.Server/Components/Samples/Sockets/ManualReceives.razor.cs
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 |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| using System.Net; | ||
| using System.Text; | ||
|
|
||
| namespace BootstrapBlazor.Server.Components.Samples.Sockets; | ||
|
|
||
| /// <summary> | ||
| /// 接收电文示例 | ||
| /// </summary> | ||
| public partial class ManualReceives | ||
| { | ||
| [Inject, NotNull] | ||
| private ITcpSocketFactory? TcpSocketFactory { get; set; } | ||
|
|
||
| private ITcpSocketClient _client = null!; | ||
|
|
||
| private List<ConsoleMessageItem> _items = []; | ||
|
|
||
| private readonly IPEndPoint _serverEndPoint = new(IPAddress.Loopback, 8810); | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| protected override void OnInitialized() | ||
| { | ||
| base.OnInitialized(); | ||
|
|
||
| // 从服务中获取 Socket 实例 | ||
| _client = TcpSocketFactory.GetOrCreate("demo-manual-receive", options => | ||
| { | ||
| options.LocalEndPoint = new IPEndPoint(IPAddress.Loopback, 0); | ||
| options.IsAutoReceive = false; | ||
| }); | ||
| } | ||
|
|
||
| private async Task OnConnectAsync() | ||
| { | ||
| if (_client is { IsConnected: false }) | ||
| { | ||
| await _client.ConnectAsync(_serverEndPoint, CancellationToken.None); | ||
| } | ||
| } | ||
|
|
||
| private async Task OnCloseAsync() | ||
| { | ||
| if (_client is { IsConnected: true }) | ||
| { | ||
| await _client.CloseAsync(); | ||
| } | ||
| } | ||
|
|
||
| private Task OnClear() | ||
| { | ||
| _items = []; | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| private async Task OnSendAsync() | ||
| { | ||
| if (_client is { IsConnected: true }) | ||
| { | ||
| // 准备通讯数据 | ||
| var data = new byte[2] { 0x01, 0x02 }; | ||
| var result = await _client.SendAsync(data, CancellationToken.None); | ||
| var state = result ? "成功" : "失败"; | ||
|
|
||
| // 记录日志 | ||
| _items.Add(new ConsoleMessageItem() | ||
| { | ||
| Message = $"{DateTime.Now}: 发送数据 {_client.LocalEndPoint} - {_serverEndPoint} Data: {BitConverter.ToString(data)} {state}" | ||
| }); | ||
|
|
||
| if (result) | ||
| { | ||
| var buffer = await _client.ReceiveAsync(CancellationToken.None); | ||
| var payload = buffer.ToArray(); | ||
| _items.Add(new ConsoleMessageItem() | ||
| { | ||
| Message = $"{DateTime.Now}: 接收数据 {_client.LocalEndPoint} - {_serverEndPoint} Data {Encoding.UTF8.GetString(payload)} HEX: {BitConverter.ToString(payload)} 成功", | ||
| Color = Color.Success | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } |
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
67 changes: 67 additions & 0 deletions
67
src/BootstrapBlazor.Server/Services/MockSendReceiveSocketServerService.cs
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 |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| using System.Net; | ||
| using System.Net.Sockets; | ||
|
|
||
| namespace Longbow.Tasks.Services; | ||
|
|
||
| /// <summary> | ||
| /// 模拟 Socket 服务端服务类 | ||
| /// </summary> | ||
| class MockSendReceiveSocketServerService(ILogger<MockReceiveSocketServerService> logger) : BackgroundService | ||
| { | ||
| /// <summary> | ||
| /// 运行任务 | ||
| /// </summary> | ||
| /// <param name="stoppingToken"></param> | ||
| /// <returns></returns> | ||
| protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
| { | ||
| var server = new TcpListener(IPAddress.Loopback, 8810); | ||
| server.Start(); | ||
| while (stoppingToken is { IsCancellationRequested: false }) | ||
| { | ||
| try | ||
| { | ||
| var client = await server.AcceptTcpClientAsync(stoppingToken); | ||
| _ = Task.Run(() => MockSendAsync(client, stoppingToken), stoppingToken); | ||
| } | ||
| catch { } | ||
| } | ||
| } | ||
|
|
||
| private async Task MockSendAsync(TcpClient client, CancellationToken stoppingToken) | ||
| { | ||
| // 方法目的: | ||
| // 接收到数据后会送当前时间戳数据包到客户端 | ||
| await using var stream = client.GetStream(); | ||
| while (stoppingToken is { IsCancellationRequested: false }) | ||
| { | ||
| try | ||
| { | ||
| // 接收数据 | ||
| var len = await stream.ReadAsync(new byte[1024], stoppingToken); | ||
| if (len == 0) | ||
| { | ||
| // 断开连接 | ||
| break; | ||
| } | ||
| // 模拟服务,对接收到的消息未做处理 | ||
| // 模拟一发一收的通讯方法 | ||
| var data = System.Text.Encoding.UTF8.GetBytes(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); | ||
| await stream.WriteAsync(data, stoppingToken); | ||
| } | ||
| catch (OperationCanceledException) { break; } | ||
| catch (IOException) { break; } | ||
| catch (SocketException) { break; } | ||
| catch (Exception ex) | ||
| { | ||
| logger.LogError(ex, "MockSendReceiveSocketServerService encountered an error while sending data."); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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.