Skip to content

Commit a2767ab

Browse files
committed
refactor: 更新 ITcpSocketFactory 接口定义
1 parent 0f86b3c commit a2767ab

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

src/BootstrapBlazor/Services/TcpSocket/DefaultTcpSocketFactory.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ class DefaultTcpSocketFactory(IServiceProvider provider) : ITcpSocketFactory
1616
{
1717
private readonly ConcurrentDictionary<string, ITcpSocketClient> _pool = new();
1818

19-
public ITcpSocketClient GetOrCreate(string host, int port = 0)
19+
public ITcpSocketClient GetOrCreate(string name, IPEndPoint endPoint)
2020
{
21-
return _pool.GetOrAdd($"{host}:{port}", key =>
21+
return _pool.GetOrAdd(name, key =>
2222
{
23-
var endPoint = Utility.ConvertToIpEndPoint(host, port);
2423
var client = new DefaultTcpSocketClient(endPoint)
2524
{
2625
Logger = provider.GetService<ILogger<DefaultTcpSocketClient>>()
@@ -29,10 +28,10 @@ public ITcpSocketClient GetOrCreate(string host, int port = 0)
2928
});
3029
}
3130

32-
public ITcpSocketClient? Remove(string host, int port)
31+
public ITcpSocketClient? Remove(string name)
3332
{
3433
ITcpSocketClient? client = null;
35-
if (_pool.TryRemove($"{host}:{port}", out var c))
34+
if (_pool.TryRemove(name, out var c))
3635
{
3736
client = c;
3837
}

src/BootstrapBlazor/Services/TcpSocket/ITcpSocketFactory.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
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;
7+
68
namespace BootstrapBlazor.Components;
79

810
/// <summary>
@@ -11,21 +13,21 @@ namespace BootstrapBlazor.Components;
1113
public interface ITcpSocketFactory : IDisposable
1214
{
1315
/// <summary>
14-
/// Retrieves an existing TCP socket associated with the specified host and port, or creates a new one if none
15-
/// exists.
16+
/// Retrieves an existing TCP socket client by name or creates a new one if it does not exist.
1617
/// </summary>
17-
/// <param name="host">The hostname or IP address of the remote endpoint. Cannot be null or empty.</param>
18-
/// <param name="port">The port number of the remote endpoint. Must be a valid port number between 0 and 65535.</param>
19-
/// <returns>An <see cref="ITcpSocketClient"/> instance representing the TCP socket for the specified host and port.</returns>
20-
ITcpSocketClient GetOrCreate(string host, int port);
18+
/// <param name="name">The unique name used to identify the TCP socket client. Cannot be null or empty.</param>
19+
/// <param name="endPoint">The network endpoint to associate with the TCP socket client. Must be a valid <see
20+
/// cref="System.Net.IPEndPoint"/> instance.</param>
21+
/// <returns>An instance of <see cref="ITcpSocketClient"/> representing the TCP socket client associated with the specified
22+
/// name and endpoint. If a client with the given name already exists, the existing instance is returned; otherwise,
23+
/// a new client is created.</returns>
24+
ITcpSocketClient GetOrCreate(string name, IPEndPoint endPoint);
2125

2226
/// <summary>
23-
/// Removes the specified host and port combination from the collection.
27+
/// Removes the TCP socket client associated with the specified name.
2428
/// </summary>
25-
/// <remarks>If the specified host and port combination does not exist in the collection, the method has
26-
/// no effect.</remarks>
27-
/// <param name="host">The hostname to remove. Cannot be null or empty.</param>
28-
/// <param name="port">The port number associated with the host to remove. Must be a valid port number (0-65535).</param>
29-
/// <returns>An <see cref="ITcpSocketClient"/> instance representing the TCP socket for the specified host and port.</returns>
30-
ITcpSocketClient? Remove(string host, int port);
29+
/// <param name="name">The name of the TCP socket client to remove. Cannot be <see langword="null"/> or empty.</param>
30+
/// <returns>The removed <see cref="ITcpSocketClient"/> instance if a client with the specified name exists; otherwise, <see
31+
/// langword="null"/>.</returns>
32+
ITcpSocketClient? Remove(string name);
3133
}

0 commit comments

Comments
 (0)