Skip to content

Commit 0df17f5

Browse files
authored
feat(ITcpSocketFactory): redesign GetOrCreate method (#6282)
* refactor: 更改 GetOrCreate 签名 * test: 更新单元测试
1 parent 7826f22 commit 0df17f5

File tree

4 files changed

+14
-44
lines changed

4 files changed

+14
-44
lines changed

src/BootstrapBlazor/Extensions/ITcpSocketFactoryExtensions.cs

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

src/BootstrapBlazor/Services/TcpSocket/DefaultTcpSocketFactory.cs

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

19-
public ITcpSocketClient GetOrCreate(string name, IPEndPoint endPoint)
19+
public ITcpSocketClient GetOrCreate(string name, Func<string, IPEndPoint> valueFactory)
2020
{
2121
return _pool.GetOrAdd(name, key =>
2222
{
23+
var endPoint = valueFactory(key);
2324
var client = new DefaultTcpSocketClient(endPoint)
2425
{
2526
Logger = provider.GetService<ILogger<DefaultTcpSocketClient>>()

src/BootstrapBlazor/Services/TcpSocket/ITcpSocketFactory.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@ namespace BootstrapBlazor.Components;
1313
public interface ITcpSocketFactory : IDisposable
1414
{
1515
/// <summary>
16-
/// Retrieves an existing TCP socket client by name or creates a new one if it does not exist.
16+
/// Retrieves an existing TCP socket client by name or creates a new one using the specified factory function.
1717
/// </summary>
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);
18+
/// <param name="name">The unique name identifying the TCP socket client. Cannot be null or empty.</param>
19+
/// <param name="valueFactory">A factory function that generates an <see cref="IPEndPoint"/> for the client. The function is invoked if a
20+
/// client with the specified name does not already exist.</param>
21+
/// <returns>An instance of <see cref="ITcpSocketClient"/> associated with the specified name. If a client with the given
22+
/// name already exists, the existing instance is returned; otherwise, a new instance is created.</returns>
23+
ITcpSocketClient GetOrCreate(string name, Func<string, IPEndPoint> valueFactory);
2524

2625
/// <summary>
2726
/// Removes the TCP socket client associated with the specified name.

test/UnitTest/Services/TcpSocketFactoryTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ public void GetOrCreate_Ok()
2424
sc.AddBootstrapBlazorTcpSocketFactory();
2525
var provider = sc.BuildServiceProvider();
2626
var factory = provider.GetRequiredService<ITcpSocketFactory>();
27-
var client1 = factory.GetOrCreate("demo", "localhost", 0);
27+
var client1 = factory.GetOrCreate("demo", key => Utility.ConvertToIpEndPoint("localhost", 0));
2828
client1.Close();
2929

30-
var client2 = factory.GetOrCreate("demo", "localhost", 0);
30+
var client2 = factory.GetOrCreate("demo", key => Utility.ConvertToIpEndPoint("localhost", 0));
3131
Assert.Equal(client1, client2);
3232

3333
var ip = Dns.GetHostAddresses(Dns.GetHostName(), AddressFamily.InterNetwork).FirstOrDefault() ?? IPAddress.Loopback;
34-
var client3 = factory.GetOrCreate("demo1", ip.ToString(), 0);
34+
var client3 = factory.GetOrCreate("demo1", key => Utility.ConvertToIpEndPoint(ip.ToString(), 0));
3535

3636
// 测试不合格 IP 地址
37-
var client4 = factory.GetOrCreate("demo2", "256.0.0.1", 0);
37+
var client4 = factory.GetOrCreate("demo2", key => Utility.ConvertToIpEndPoint("256.0.0.1", 0));
3838

3939
var client5 = factory.Remove("demo2");
4040
Assert.Equal(client4, client5);
@@ -450,7 +450,7 @@ private static ITcpSocketClient CreateClient()
450450
sc.AddBootstrapBlazorTcpSocketFactory();
451451
var provider = sc.BuildServiceProvider();
452452
var factory = provider.GetRequiredService<ITcpSocketFactory>();
453-
var client = factory.GetOrCreate("test", "localhost", 0);
453+
var client = factory.GetOrCreate("test", key => Utility.ConvertToIpEndPoint("localhost", 0));
454454
return client;
455455
}
456456

0 commit comments

Comments
 (0)