Skip to content

Commit 7e51fd9

Browse files
committed
refactor: 增加 SocketClient 基类
1 parent 26f6ab7 commit 7e51fd9

File tree

6 files changed

+29
-12
lines changed

6 files changed

+29
-12
lines changed

src/BootstrapBlazor/Services/TcpSocket/DefaultTcpSocketClient.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace BootstrapBlazor.Components;
1010

1111
[UnsupportedOSPlatform("browser")]
12-
sealed class DefaultTcpSocketClient : TcpSocketClientBase<DefaultSocketClient>
12+
sealed class DefaultTcpSocketClient : TcpSocketClientBase<SocketClientBase>
1313
{
1414
public DefaultTcpSocketClient(SocketClientOptions options)
1515
{
@@ -27,9 +27,8 @@ public DefaultTcpSocketClient(SocketClientOptions options)
2727
/// <param name="localEndPoint"></param>
2828
/// <returns></returns>
2929
/// <exception cref="NotImplementedException"></exception>
30-
protected override DefaultSocketClient CreateSocketClient(IPEndPoint? localEndPoint)
30+
protected override SocketClientBase CreateSocketClient(IPEndPoint localEndPoint)
3131
{
32-
localEndPoint ??= new IPEndPoint(IPAddress.Loopback, 0);
33-
return new DefaultSocketClient(localEndPoint);
32+
return new SocketClientBase(localEndPoint);
3433
}
3534
}

src/BootstrapBlazor/Services/TcpSocket/ISocketClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public interface ISocketClient
2828
/// <remarks>This property provides information about the local endpoint of the socket, which is typically
2929
/// used to identify the local address and port being used for communication. If the socket is not bound to a
3030
/// specific local endpoint, this property may return <see langword="null"/>.</remarks>
31-
IPEndPoint? LocalEndPoint { get; }
31+
IPEndPoint LocalEndPoint { get; }
3232

3333
/// <summary>
3434
/// Establishes an asynchronous connection to the specified endpoint.

src/BootstrapBlazor/Services/TcpSocket/ITcpSocketClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public interface ITcpSocketClient : IAsyncDisposable
5353
/// <remarks>This property provides information about the local endpoint of the socket, which is typically
5454
/// used to identify the local address and port being used for communication. If the socket is not bound to a
5555
/// specific local endpoint, this property may return <see langword="null"/>.</remarks>
56-
IPEndPoint? LocalEndPoint { get; }
56+
IPEndPoint LocalEndPoint { get; }
5757

5858
/// <summary>
5959
/// Gets or sets the callback function to handle received data.

src/BootstrapBlazor/Services/TcpSocket/DefaultSocketClient.cs renamed to src/BootstrapBlazor/Services/TcpSocket/SocketClientBase.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,33 @@ namespace BootstrapBlazor.Components;
1313
/// TcpSocket 客户端默认实现
1414
/// </summary>
1515
[UnsupportedOSPlatform("browser")]
16-
class DefaultSocketClient(IPEndPoint localEndPoint) : ISocketClient
16+
public class SocketClientBase(IPEndPoint localEndPoint) : ISocketClient
1717
{
1818
private TcpClient? _client;
1919

20+
/// <summary>
21+
/// <inheritdoc/>
22+
/// </summary>
2023
public bool IsConnected => _client?.Connected ?? false;
2124

22-
public IPEndPoint? LocalEndPoint { get; set; }
25+
/// <summary>
26+
/// <inheritdoc/>
27+
/// </summary>
28+
public IPEndPoint LocalEndPoint { get; set; } = new IPEndPoint(IPAddress.Loopback, 0);
2329

30+
/// <summary>
31+
/// <inheritdoc/>
32+
/// </summary>
2433
public async ValueTask<bool> ConnectAsync(IPEndPoint endPoint, CancellationToken token = default)
2534
{
2635
_client = new TcpClient(localEndPoint);
2736
await _client.ConnectAsync(endPoint, token);
2837
return _client.Connected;
2938
}
3039

40+
/// <summary>
41+
/// <inheritdoc/>
42+
/// </summary>
3143
public async ValueTask<bool> SendAsync(ReadOnlyMemory<byte> data, CancellationToken token = default)
3244
{
3345
var ret = false;
@@ -40,6 +52,9 @@ public async ValueTask<bool> SendAsync(ReadOnlyMemory<byte> data, CancellationTo
4052
return ret;
4153
}
4254

55+
/// <summary>
56+
/// <inheritdoc/>
57+
/// </summary>
4358
public async ValueTask<int> ReceiveAsync(Memory<byte> buffer, CancellationToken token = default)
4459
{
4560
var len = 0;
@@ -51,6 +66,9 @@ public async ValueTask<int> ReceiveAsync(Memory<byte> buffer, CancellationToken
5166
return len;
5267
}
5368

69+
/// <summary>
70+
/// <inheritdoc/>
71+
/// </summary>
5472
public ValueTask CloseAsync()
5573
{
5674
if (_client != null)

src/BootstrapBlazor/Services/TcpSocket/SocketClientOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public class SocketClientOptions
4646
public int ReceiveTimeout { get; set; }
4747

4848
/// <summary>
49-
/// Gets or sets the local endpoint for the socket client.
49+
/// Gets or sets the local endpoint for the socket client. Default value is <see cref="IPAddress.Loopback"/>
5050
/// </summary>
5151
/// <remarks>This property specifies the local network endpoint that the socket client will bind to when establishing a connection.</remarks>
52-
public IPEndPoint? LocalEndPoint { get; set; }
52+
public IPEndPoint LocalEndPoint { get; set; } = new IPEndPoint(IPAddress.Loopback, 0);
5353
}

src/BootstrapBlazor/Services/TcpSocket/TcpSocketClientBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public abstract class TcpSocketClientBase<TSocketClient> : ITcpSocketClient wher
3636
/// <summary>
3737
/// <inheritdoc/>
3838
/// </summary>
39-
public IPEndPoint? LocalEndPoint { get; set; }
39+
public IPEndPoint LocalEndPoint { get; set; } = new IPEndPoint(IPAddress.Loopback, 0);
4040

4141
/// <summary>
4242
/// <inheritdoc/>
@@ -82,7 +82,7 @@ public abstract class TcpSocketClientBase<TSocketClient> : ITcpSocketClient wher
8282
/// </summary>
8383
/// <param name="localEndPoint">The network endpoint to which the socket client will connect. Cannot be null.</param>
8484
/// <returns>An instance of <typeparamref name="TSocketClient"/> configured for the specified endpoint.</returns>
85-
protected abstract TSocketClient CreateSocketClient(IPEndPoint? localEndPoint);
85+
protected abstract TSocketClient CreateSocketClient(IPEndPoint localEndPoint);
8686

8787
/// <summary>
8888
/// <inheritdoc/>

0 commit comments

Comments
 (0)