From 58002dad5f8ec40612032dfb39376f501c64bfae Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Mon, 23 Jun 2025 13:57:09 +0800 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=94=B9=20GetOrCreat?= =?UTF-8?q?e=20=E7=AD=BE=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/ITcpSocketFactoryExtensions.cs | 30 ------------------- .../TcpSocket/DefaultTcpSocketFactory.cs | 3 +- .../Services/TcpSocket/ITcpSocketFactory.cs | 15 +++++----- 3 files changed, 9 insertions(+), 39 deletions(-) delete mode 100644 src/BootstrapBlazor/Extensions/ITcpSocketFactoryExtensions.cs diff --git a/src/BootstrapBlazor/Extensions/ITcpSocketFactoryExtensions.cs b/src/BootstrapBlazor/Extensions/ITcpSocketFactoryExtensions.cs deleted file mode 100644 index 0c639db77f9..00000000000 --- a/src/BootstrapBlazor/Extensions/ITcpSocketFactoryExtensions.cs +++ /dev/null @@ -1,30 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the Apache 2.0 License -// See the LICENSE file in the project root for more information. -// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone - -using System.Runtime.Versioning; - -namespace BootstrapBlazor.Components; - -/// -/// 扩展方法类 -/// -[UnsupportedOSPlatform("browser")] -public static class ITcpSocketFactoryExtensions -{ - /// - /// Retrieves an existing TCP socket client by name or creates a new one using the specified IP address and port. - /// - /// The instance used to manage TCP socket clients. - /// The unique name identifying the TCP socket client. Cannot be null or empty. - /// The IP address of the endpoint to connect to. Must be a valid IPv4 or IPv6 address. - /// The port number of the endpoint to connect to. Must be within the range 0 to 65535. - /// An instance representing the TCP socket client associated with the specified - /// name. If no client exists with the given name, a new client is created and returned. - public static ITcpSocketClient GetOrCreate(this ITcpSocketFactory factory, string name, string ipAddress, int port) - { - var endPoint = Utility.ConvertToIpEndPoint(ipAddress, port); - return factory.GetOrCreate(name, endPoint); - } -} diff --git a/src/BootstrapBlazor/Services/TcpSocket/DefaultTcpSocketFactory.cs b/src/BootstrapBlazor/Services/TcpSocket/DefaultTcpSocketFactory.cs index 3ce84679043..4ff18905def 100644 --- a/src/BootstrapBlazor/Services/TcpSocket/DefaultTcpSocketFactory.cs +++ b/src/BootstrapBlazor/Services/TcpSocket/DefaultTcpSocketFactory.cs @@ -16,10 +16,11 @@ class DefaultTcpSocketFactory(IServiceProvider provider) : ITcpSocketFactory { private readonly ConcurrentDictionary _pool = new(); - public ITcpSocketClient GetOrCreate(string name, IPEndPoint endPoint) + public ITcpSocketClient GetOrCreate(string name, Func valueFactory) { return _pool.GetOrAdd(name, key => { + var endPoint = valueFactory(key); var client = new DefaultTcpSocketClient(endPoint) { Logger = provider.GetService>() diff --git a/src/BootstrapBlazor/Services/TcpSocket/ITcpSocketFactory.cs b/src/BootstrapBlazor/Services/TcpSocket/ITcpSocketFactory.cs index f479250a5d1..44db6076ca1 100644 --- a/src/BootstrapBlazor/Services/TcpSocket/ITcpSocketFactory.cs +++ b/src/BootstrapBlazor/Services/TcpSocket/ITcpSocketFactory.cs @@ -13,15 +13,14 @@ namespace BootstrapBlazor.Components; public interface ITcpSocketFactory : IDisposable { /// - /// 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. /// - /// The unique name used to identify the TCP socket client. Cannot be null or empty. - /// The network endpoint to associate with the TCP socket client. Must be a valid instance. - /// An instance of 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. - ITcpSocketClient GetOrCreate(string name, IPEndPoint endPoint); + /// The unique name identifying the TCP socket client. Cannot be null or empty. + /// A factory function that generates an for the client. The function is invoked if a + /// client with the specified name does not already exist. + /// An instance of 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. + ITcpSocketClient GetOrCreate(string name, Func valueFactory); /// /// Removes the TCP socket client associated with the specified name. From 070db4f9c52b362da95467b7618af7c99557b94d Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Mon, 23 Jun 2025 14:00:38 +0800 Subject: [PATCH 2/2] =?UTF-8?q?test:=20=E6=9B=B4=E6=96=B0=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Services/TcpSocketFactoryTest.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/UnitTest/Services/TcpSocketFactoryTest.cs b/test/UnitTest/Services/TcpSocketFactoryTest.cs index dc5ca89ecbc..d0567beb6eb 100644 --- a/test/UnitTest/Services/TcpSocketFactoryTest.cs +++ b/test/UnitTest/Services/TcpSocketFactoryTest.cs @@ -24,17 +24,17 @@ public void GetOrCreate_Ok() sc.AddBootstrapBlazorTcpSocketFactory(); var provider = sc.BuildServiceProvider(); var factory = provider.GetRequiredService(); - 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); @@ -450,7 +450,7 @@ private static ITcpSocketClient CreateClient() sc.AddBootstrapBlazorTcpSocketFactory(); var provider = sc.BuildServiceProvider(); var factory = provider.GetRequiredService(); - var client = factory.GetOrCreate("test", "localhost", 0); + var client = factory.GetOrCreate("test", key => Utility.ConvertToIpEndPoint("localhost", 0)); return client; }