Skip to content

Commit 618b956

Browse files
authored
feat(ISocketClientProvider): add ISocketClientProvider service (#6329)
* refactor: 重构实现逻辑增加 ISocketClientProvider 服务 * wip: 移除注释 * test: 增加单元测试 * refactor: 精简代码 * test: 测试连接超时
1 parent f43449c commit 618b956

File tree

12 files changed

+143
-364
lines changed

12 files changed

+143
-364
lines changed

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

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

@@ -25,7 +25,7 @@ class DefaultSocketClient(IPEndPoint localEndPoint) : ISocketClient
2525
/// <summary>
2626
/// <inheritdoc/>
2727
/// </summary>
28-
public IPEndPoint LocalEndPoint { get; set; } = localEndPoint;
28+
public IPEndPoint LocalEndPoint { get; set; } = new IPEndPoint(IPAddress.Any, 0);
2929

3030
/// <summary>
3131
/// <inheritdoc/>

src/BootstrapBlazor/Services/TcpSocket/DefaultTcpSocketClient.cs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,12 @@
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
55

6-
using System.Net;
76
using System.Runtime.Versioning;
87

98
namespace BootstrapBlazor.Components;
109

1110
[UnsupportedOSPlatform("browser")]
12-
sealed class DefaultTcpSocketClient : TcpSocketClientBase<DefaultSocketClient>
11+
sealed class DefaultTcpSocketClient(SocketClientOptions options) : TcpSocketClientBase(options)
1312
{
14-
public DefaultTcpSocketClient(SocketClientOptions options)
15-
{
16-
ReceiveBufferSize = Math.Max(1024, options.ReceiveBufferSize);
17-
IsAutoReceive = options.IsAutoReceive;
18-
ConnectTimeout = options.ConnectTimeout;
19-
SendTimeout = options.SendTimeout;
20-
ReceiveTimeout = options.ReceiveTimeout;
21-
LocalEndPoint = options.LocalEndPoint;
22-
}
2313

24-
/// <summary>
25-
/// <inheritdoc/>
26-
/// </summary>
27-
/// <param name="localEndPoint"></param>
28-
/// <returns></returns>
29-
/// <exception cref="NotImplementedException"></exception>
30-
protected override DefaultSocketClient CreateSocketClient(IPEndPoint localEndPoint)
31-
{
32-
return new DefaultSocketClient(localEndPoint);
33-
}
3414
}

src/BootstrapBlazor/Services/TcpSocket/DefaultTcpSocketFactory.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
55

6-
using Microsoft.Extensions.DependencyInjection;
7-
using Microsoft.Extensions.Logging;
86
using System.Collections.Concurrent;
97
using System.Runtime.Versioning;
108

@@ -23,7 +21,7 @@ public ITcpSocketClient GetOrCreate(string name, Action<SocketClientOptions> val
2321
valueFactory(options);
2422
var client = new DefaultTcpSocketClient(options)
2523
{
26-
Logger = provider.GetService<ILogger<DefaultTcpSocketClient>>()
24+
ServiceProvider = provider,
2725
};
2826
return client;
2927
});

src/BootstrapBlazor/Services/TcpSocket/Extensions/TcpSocketExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ public static class TcpSocketExtensions
2222
/// <returns></returns>
2323
public static IServiceCollection AddBootstrapBlazorTcpSocketFactory(this IServiceCollection services)
2424
{
25-
// 添加 ITcpSocket 实现
26-
services.TryAddSingleton<ITcpSocketFactory, DefaultTcpSocketFactory>();
25+
// 添加 ITcpSocketFactory 服务
26+
services.AddSingleton<ITcpSocketFactory, DefaultTcpSocketFactory>();
2727

28+
// 增加 ISocketClientProvider 服务
29+
services.TryAddTransient<ISocketClientProvider, DefaultSocketClientProvider>();
2830
return services;
2931
}
3032
}

src/BootstrapBlazor/Services/TcpSocket/ISocketClient.cs renamed to src/BootstrapBlazor/Services/TcpSocket/ISocketClientProvider.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace BootstrapBlazor.Components;
1515
/// establishing connections, transmitting data, and receiving data asynchronously. Implementations of this interface
1616
/// should ensure proper resource management, including closing connections and releasing resources when no longer
1717
/// needed.</remarks>
18-
public interface ISocketClient
18+
public interface ISocketClientProvider
1919
{
2020
/// <summary>
2121
/// Gets a value indicating whether the connection is currently active.
@@ -25,10 +25,7 @@ public interface ISocketClient
2525
/// <summary>
2626
/// Gets the local network endpoint that the socket is bound to.
2727
/// </summary>
28-
/// <remarks>This property provides information about the local endpoint of the socket, which is typically
29-
/// used to identify the local address and port being used for communication. If the socket is not bound to a
30-
/// specific local endpoint, this property may return <see langword="null"/>.</remarks>
31-
IPEndPoint LocalEndPoint { get; }
28+
IPEndPoint LocalEndPoint { get; set; }
3229

3330
/// <summary>
3431
/// Establishes an asynchronous connection to the specified endpoint.

src/BootstrapBlazor/Services/TcpSocket/ITcpSocketClient.cs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,15 @@ namespace BootstrapBlazor.Components;
1212
/// </summary>
1313
public interface ITcpSocketClient : IAsyncDisposable
1414
{
15-
/// <summary>
16-
/// Gets or sets the size, in bytes, of the receive buffer used for network operations.
17-
/// </summary>
18-
int ReceiveBufferSize { get; set; }
19-
2015
/// <summary>
2116
/// Gets a value indicating whether the system is currently connected. Default is false.
2217
/// </summary>
2318
bool IsConnected { get; }
2419

2520
/// <summary>
26-
/// Gets or sets a value indicating whether automatic receiving data is enabled. Default is true.
27-
/// </summary>
28-
bool IsAutoReceive { get; set; }
29-
30-
/// <summary>
31-
/// Gets or sets the timeout duration, in milliseconds, for establishing a connection.
32-
/// </summary>
33-
int ConnectTimeout { get; set; }
34-
35-
/// <summary>
36-
/// Gets or sets the duration, in milliseconds, to wait for a send operation to complete before timing out.
37-
/// </summary>
38-
/// <remarks>If the send operation does not complete within the specified timeout period, an exception may
39-
/// be thrown.</remarks>
40-
int SendTimeout { get; set; }
41-
42-
/// <summary>
43-
/// Gets or sets the amount of time, in milliseconds, that the receiver will wait for a response before timing out.
21+
/// Gets or sets the configuration options for the socket client.
4422
/// </summary>
45-
/// <remarks>Use this property to configure the maximum wait time for receiving a response. Setting an
46-
/// appropriate timeout can help prevent indefinite blocking in scenarios where responses may be delayed or
47-
/// unavailable.</remarks>
48-
int ReceiveTimeout { get; set; }
23+
SocketClientOptions Options { get; }
4924

5025
/// <summary>
5126
/// Gets the local network endpoint that the socket is bound to.

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. Default value is <see cref="IPAddress.Loopback"/>
49+
/// Gets or sets the local endpoint for the socket client. Default value is <see cref="IPAddress.Any"/>
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; } = new IPEndPoint(IPAddress.Loopback, 0);
52+
public IPEndPoint LocalEndPoint { get; set; } = new IPEndPoint(IPAddress.Any, 0);
5353
}

0 commit comments

Comments
 (0)