Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions src/BootstrapBlazor/Extensions/ITcpSocketFactoryExtensions.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ class DefaultTcpSocketFactory(IServiceProvider provider) : ITcpSocketFactory
{
private readonly ConcurrentDictionary<string, ITcpSocketClient> _pool = new();

public ITcpSocketClient GetOrCreate(string name, IPEndPoint endPoint)
public ITcpSocketClient GetOrCreate(string name, Func<string, IPEndPoint> valueFactory)
{
return _pool.GetOrAdd(name, key =>
{
var endPoint = valueFactory(key);
var client = new DefaultTcpSocketClient(endPoint)
{
Logger = provider.GetService<ILogger<DefaultTcpSocketClient>>()
Expand Down
15 changes: 7 additions & 8 deletions src/BootstrapBlazor/Services/TcpSocket/ITcpSocketFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ namespace BootstrapBlazor.Components;
public interface ITcpSocketFactory : IDisposable
{
/// <summary>
/// Retrieves an existing TCP socket client by name or creates a new one if it does not exist.
/// Retrieves an existing TCP socket client by name or creates a new one using the specified factory function.
/// </summary>
/// <param name="name">The unique name used to identify the TCP socket client. Cannot be null or empty.</param>
/// <param name="endPoint">The network endpoint to associate with the TCP socket client. Must be a valid <see
/// cref="System.Net.IPEndPoint"/> instance.</param>
/// <returns>An instance of <see cref="ITcpSocketClient"/> representing the TCP socket client associated with the specified
/// name and endpoint. If a client with the given name already exists, the existing instance is returned; otherwise,
/// a new client is created.</returns>
ITcpSocketClient GetOrCreate(string name, IPEndPoint endPoint);
/// <param name="name">The unique name identifying the TCP socket client. Cannot be null or empty.</param>
/// <param name="valueFactory">A factory function that generates an <see cref="IPEndPoint"/> for the client. The function is invoked if a
/// client with the specified name does not already exist.</param>
/// <returns>An instance of <see cref="ITcpSocketClient"/> associated with the specified name. If a client with the given
/// name already exists, the existing instance is returned; otherwise, a new instance is created.</returns>
ITcpSocketClient GetOrCreate(string name, Func<string, IPEndPoint> valueFactory);

/// <summary>
/// Removes the TCP socket client associated with the specified name.
Expand Down
10 changes: 5 additions & 5 deletions test/UnitTest/Services/TcpSocketFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ public void GetOrCreate_Ok()
sc.AddBootstrapBlazorTcpSocketFactory();
var provider = sc.BuildServiceProvider();
var factory = provider.GetRequiredService<ITcpSocketFactory>();
var client1 = factory.GetOrCreate("demo", "localhost", 0);
var client1 = factory.GetOrCreate("demo", key => Utility.ConvertToIpEndPoint("localhost", 0));
client1.Close();

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

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

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

var client5 = factory.Remove("demo2");
Assert.Equal(client4, client5);
Expand Down Expand Up @@ -450,7 +450,7 @@ private static ITcpSocketClient CreateClient()
sc.AddBootstrapBlazorTcpSocketFactory();
var provider = sc.BuildServiceProvider();
var factory = provider.GetRequiredService<ITcpSocketFactory>();
var client = factory.GetOrCreate("test", "localhost", 0);
var client = factory.GetOrCreate("test", key => Utility.ConvertToIpEndPoint("localhost", 0));
return client;
}

Expand Down