Skip to content

Commit b310fed

Browse files
committed
feat(tcp): 替换 DefaultTcpSocketClient 为 TouchSocketTcpClient
添加 TouchSocket 包引用,删除 DefaultTcpSocketClient 类,使用 TouchSocketTcpClient 类实现 ITcpSocketClient 接口,提供连接、发送和接收数据的功能。修改 DefaultTcpSocketFactory 类构造函数,移除对 ILogger<DefaultTcpSocketClient> 的依赖,改为使用 TouchSocketTcpClient。新增 TouchSocketTcpClient 类实现 TCP 连接逻辑。
1 parent 9f47065 commit b310fed

4 files changed

Lines changed: 151 additions & 197 deletions

File tree

src/extensions/BootstrapBlazor.TouchSocket/BootstrapBlazor.TouchSocket.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414
</ItemGroup>
1515

1616
<ItemGroup>
17-
<!--<PackageReference Include="BootstrapBlazor" Version="$(BBVersion)" />-->
17+
<PackageReference Include="TouchSocket" Version="3.1.8" />
1818
</ItemGroup>
1919

2020
<ItemGroup>
2121
<ProjectReference Include="..\..\..\..\BootstrapBlazor\src\BootstrapBlazor\BootstrapBlazor.csproj" />
2222
</ItemGroup>
2323

24+
<ItemGroup>
25+
<PackageReference Include="BootstrapBlazor" Version="$(BBVersion)" />
26+
</ItemGroup>
27+
2428
<ItemGroup>
2529
<Using Include="Microsoft.JSInterop" />
2630
</ItemGroup>

src/extensions/BootstrapBlazor.TouchSocket/Services/DefaultTcpSocketClient.cs

Lines changed: 0 additions & 191 deletions
This file was deleted.

src/extensions/BootstrapBlazor.TouchSocket/Services/DefaultTcpSocketFactory.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,15 @@
88

99
namespace BootstrapBlazor.Components;
1010

11-
class DefaultTcpSocketFactory(IServiceProvider provider) : ITcpSocketFactory
11+
class DefaultTcpSocketFactory : ITcpSocketFactory
1212
{
1313
private readonly ConcurrentDictionary<string, ITcpSocketClient> _pool = new();
1414

1515
public ITcpSocketClient GetOrCreate(string host, int port = 0)
1616
{
1717
return _pool.GetOrAdd($"{host}:{port}", key =>
1818
{
19-
var client = new DefaultTcpSocketClient(host, port)
20-
{
21-
Logger = provider.GetService<ILogger<DefaultTcpSocketClient>>()
22-
};
19+
var client = new TouchSocketTcpClient();
2320
return client;
2421
});
2522
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
using Microsoft.Extensions.Hosting;
6+
using Microsoft.Extensions.Logging;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using System.Net;
11+
using System.Text;
12+
using System.Threading.Tasks;
13+
using TouchSocket.Core;
14+
using TouchSocket.Sockets;
15+
16+
namespace BootstrapBlazor.Components;
17+
18+
internal sealed class TouchSocketTcpClient : TcpClientBase, ITcpSocketClient
19+
{
20+
private IDataPackageHandler? dataPackageHandler;
21+
public bool IsConnected => base.Online;
22+
public IPEndPoint LocalEndPoint => base.MainSocket.LocalEndPoint as IPEndPoint ?? throw new ArgumentNullException(nameof(LocalEndPoint));
23+
public int ReceiveBufferSize { get; set; }
24+
public Func<ReadOnlyMemory<byte>, ValueTask>? ReceivedCallBack { get; set; }
25+
26+
#region ConnectAsync
27+
28+
public async ValueTask<bool> ConnectAsync(string host, int port, CancellationToken token = default)
29+
{
30+
try
31+
{
32+
if (this.IsConnected)
33+
{
34+
await this.CloseAsync("Already connected", token);
35+
}
36+
37+
await this.SetupAsync(new TouchSocketConfig()
38+
.SetRemoteIPHost($"{host}:{port}"));
39+
40+
await base.TcpConnectAsync(5000, token);
41+
42+
return true;
43+
}
44+
catch
45+
{
46+
return false;
47+
}
48+
}
49+
50+
public async ValueTask<bool> ConnectAsync(IPEndPoint endPoint, CancellationToken token = default)
51+
{
52+
try
53+
{
54+
if (this.IsConnected)
55+
{
56+
await this.CloseAsync("Already connected", token);
57+
}
58+
59+
await this.SetupAsync(new TouchSocketConfig()
60+
.SetRemoteIPHost(endPoint.ToString()));
61+
62+
await base.TcpConnectAsync(5000, token);
63+
64+
return true;
65+
}
66+
catch
67+
{
68+
return false;
69+
}
70+
}
71+
72+
#endregion ConnectAsync
73+
74+
public async ValueTask<bool> SendAsync(ReadOnlyMemory<byte> data, CancellationToken token = default)
75+
{
76+
try
77+
{
78+
ReadOnlyMemory<byte> memory;
79+
var dataPackageHandler = this.dataPackageHandler;
80+
if (dataPackageHandler == null)
81+
{
82+
memory = data;
83+
}
84+
else
85+
{
86+
memory = await dataPackageHandler.SendAsync(data);
87+
}
88+
89+
await base.ProtectedDefaultSendAsync(memory).WaitAsync(token);
90+
return true;
91+
}
92+
catch
93+
{
94+
return false;
95+
}
96+
}
97+
98+
public void SetDataHandler(IDataPackageHandler handler)
99+
{
100+
this.dataPackageHandler = handler ?? throw new ArgumentNullException(nameof(handler));
101+
handler.ReceivedCallBack = this.OnReceivedCallBack;
102+
}
103+
104+
protected override async ValueTask<bool> OnTcpReceiving(ByteBlock byteBlock)
105+
{
106+
var dataPackageHandler = this.dataPackageHandler;
107+
if (dataPackageHandler != null)
108+
{
109+
await dataPackageHandler.ReceiveAsync(byteBlock.Memory);
110+
}
111+
112+
var func = this.ReceivedCallBack;
113+
if (func != null)
114+
{
115+
await func(byteBlock.Memory);
116+
}
117+
return true;
118+
}
119+
120+
private async ValueTask OnReceivedCallBack(ReadOnlyMemory<byte> memory)
121+
{
122+
var func = this.ReceivedCallBack;
123+
if (func != null)
124+
{
125+
await func(memory);
126+
}
127+
}
128+
129+
#region Close
130+
131+
async ValueTask<bool> ITcpSocketClient.CloseAsync(string msg, CancellationToken token)
132+
{
133+
await this.CloseAsync(msg, token);
134+
return true;
135+
}
136+
137+
public override async Task<Result> CloseAsync(string msg, CancellationToken token = default)
138+
{
139+
await base.ShutdownAsync(System.Net.Sockets.SocketShutdown.Both);
140+
return await base.CloseAsync(msg, token);
141+
}
142+
143+
#endregion Close
144+
}

0 commit comments

Comments
 (0)