Skip to content

Commit 22750ca

Browse files
committed
feat: 增加基类
1 parent a36c910 commit 22750ca

File tree

2 files changed

+132
-36
lines changed

2 files changed

+132
-36
lines changed

src/BootstrapBlazor/Services/TcpSocket/DefaultTcpSocketClient.cs

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,30 @@
1212
namespace BootstrapBlazor.Components;
1313

1414
[UnsupportedOSPlatform("browser")]
15-
sealed class DefaultTcpSocketClient(IPEndPoint localEndPoint) : ITcpSocketClient
15+
sealed class DefaultTcpSocketClient(IPEndPoint localEndPoint) : TcpSocketClientBase
1616
{
1717
private TcpClient? _client;
1818
private IDataPackageHandler? _dataPackageHandler;
1919
private CancellationTokenSource? _receiveCancellationTokenSource;
2020
private IPEndPoint? _remoteEndPoint;
2121

22-
public bool IsConnected => _client?.Connected ?? false;
23-
24-
public IPEndPoint? LocalEndPoint { get; set; }
22+
public override bool IsConnected => _client?.Connected ?? false;
2523

2624
[NotNull]
2725
public ILogger<DefaultTcpSocketClient>? Logger { get; set; }
2826

29-
public int ReceiveBufferSize { get; set; } = 1024 * 64;
30-
31-
public bool IsAutoReceive { get; set; } = true;
32-
33-
public Func<ReadOnlyMemory<byte>, ValueTask>? ReceivedCallBack { get; set; }
34-
35-
public int ConnectTimeout { get; set; }
36-
37-
public int SendTimeout { get; set; }
38-
39-
public int ReceiveTimeout { get; set; }
40-
41-
public void SetDataHandler(IDataPackageHandler handler)
27+
public override void SetDataHandler(IDataPackageHandler handler)
4228
{
4329
_dataPackageHandler = handler;
4430
}
4531

46-
public async ValueTask<bool> ConnectAsync(IPEndPoint endPoint, CancellationToken token = default)
32+
public override async ValueTask<bool> ConnectAsync(IPEndPoint endPoint, CancellationToken token = default)
4733
{
4834
var ret = false;
4935
try
5036
{
5137
// 释放资源
52-
Close();
38+
await Close();
5339

5440
// 创建新的 TcpClient 实例
5541
_client ??= new TcpClient(localEndPoint);
@@ -91,7 +77,7 @@ public async ValueTask<bool> ConnectAsync(IPEndPoint endPoint, CancellationToken
9177
return ret;
9278
}
9379

94-
public async ValueTask<bool> SendAsync(ReadOnlyMemory<byte> data, CancellationToken token = default)
80+
public override async ValueTask<bool> SendAsync(ReadOnlyMemory<byte> data, CancellationToken token = default)
9581
{
9682
if (_client is not { Connected: true })
9783
{
@@ -137,7 +123,7 @@ public async ValueTask<bool> SendAsync(ReadOnlyMemory<byte> data, CancellationTo
137123
return ret;
138124
}
139125

140-
public async ValueTask<Memory<byte>> ReceiveAsync(CancellationToken token = default)
126+
public override async ValueTask<Memory<byte>> ReceiveAsync(CancellationToken token = default)
141127
{
142128
if (_client == null || !_client.Connected)
143129
{
@@ -228,12 +214,7 @@ private async ValueTask<int> ReceiveCoreAsync(TcpClient client, Memory<byte> buf
228214
return len;
229215
}
230216

231-
public void Close()
232-
{
233-
Dispose(true);
234-
}
235-
236-
private void Dispose(bool disposing)
217+
protected override ValueTask DisposeAsync(bool disposing)
237218
{
238219
if (disposing)
239220
{
@@ -255,14 +236,6 @@ private void Dispose(bool disposing)
255236
_client = null;
256237
}
257238
}
258-
}
259-
260-
/// <summary>
261-
/// <inheritdoc/>
262-
/// </summary>
263-
public void Dispose()
264-
{
265-
Dispose(true);
266-
GC.SuppressFinalize(this);
239+
return ValueTask.CompletedTask;
267240
}
268241
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
using System.Net;
7+
8+
namespace BootstrapBlazor.Components;
9+
10+
/// <summary>
11+
/// Provides a base implementation for a TCP socket client, enabling connection, data transmission, and reception over
12+
/// TCP.
13+
/// </summary>
14+
/// <remarks>This abstract class serves as a foundation for implementing TCP socket clients. It provides methods
15+
/// for connecting to a remote endpoint, sending and receiving data, and managing connection state. Derived classes can
16+
/// extend or customize the behavior as needed.</remarks>
17+
public abstract class TcpSocketClientBase : ITcpSocketClient
18+
{
19+
/// <summary>
20+
/// <inheritdoc/>
21+
/// </summary>
22+
public abstract bool IsConnected { get; }
23+
24+
/// <summary>
25+
/// <inheritdoc/>
26+
/// </summary>
27+
public IPEndPoint? LocalEndPoint { get; set; }
28+
29+
/// <summary>
30+
/// <inheritdoc/>
31+
/// </summary>
32+
public int ReceiveBufferSize { get; set; } = 1024 * 64;
33+
34+
/// <summary>
35+
/// <inheritdoc/>
36+
/// </summary>
37+
public bool IsAutoReceive { get; set; } = true;
38+
39+
/// <summary>
40+
/// <inheritdoc/>
41+
/// </summary>
42+
public Func<ReadOnlyMemory<byte>, ValueTask>? ReceivedCallBack { get; set; }
43+
44+
/// <summary>
45+
/// <inheritdoc/>
46+
/// </summary>
47+
public int ConnectTimeout { get; set; }
48+
49+
/// <summary>
50+
/// <inheritdoc/>
51+
/// </summary>
52+
public int SendTimeout { get; set; }
53+
54+
/// <summary>
55+
/// <inheritdoc/>
56+
/// </summary>
57+
public int ReceiveTimeout { get; set; }
58+
59+
/// <summary>
60+
/// <inheritdoc/>
61+
/// </summary>
62+
public virtual void SetDataHandler(IDataPackageHandler handler)
63+
{
64+
65+
}
66+
67+
/// <summary>
68+
/// <inheritdoc/>
69+
/// </summary>
70+
/// <param name="endPoint"></param>
71+
/// <param name="token"></param>
72+
/// <returns></returns>
73+
public abstract ValueTask<bool> ConnectAsync(IPEndPoint endPoint, CancellationToken token = default);
74+
75+
/// <summary>
76+
/// <inheritdoc/>
77+
/// </summary>
78+
/// <param name="data"></param>
79+
/// <param name="token"></param>
80+
/// <returns></returns>
81+
public abstract ValueTask<bool> SendAsync(ReadOnlyMemory<byte> data, CancellationToken token = default);
82+
83+
/// <summary>
84+
/// <inheritdoc/>
85+
/// </summary>
86+
/// <param name="token"></param>
87+
/// <returns></returns>
88+
public abstract ValueTask<Memory<byte>> ReceiveAsync(CancellationToken token = default);
89+
90+
/// <summary>
91+
/// <inheritdoc/>
92+
/// </summary>
93+
public virtual ValueTask Close()
94+
{
95+
return DisposeAsync(true);
96+
}
97+
98+
/// <summary>
99+
/// Releases the resources used by the current instance of the class.
100+
/// </summary>
101+
/// <remarks>This method is called to free both managed and unmanaged resources. If the <paramref
102+
/// name="disposing"/> parameter is <see langword="true"/>, the method releases managed resources in addition to
103+
/// unmanaged resources. Override this method in a derived class to provide custom cleanup logic.</remarks>
104+
/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only
105+
/// unmanaged resources.</param>
106+
protected virtual ValueTask DisposeAsync(bool disposing)
107+
{
108+
if (disposing)
109+
{
110+
LocalEndPoint = null;
111+
}
112+
return ValueTask.CompletedTask;
113+
}
114+
115+
/// <summary>
116+
/// <inheritdoc/>
117+
/// </summary>
118+
public async ValueTask DisposeAsync()
119+
{
120+
await DisposeAsync(true);
121+
GC.SuppressFinalize(this);
122+
}
123+
}

0 commit comments

Comments
 (0)