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: 30 additions & 0 deletions src/BootstrapBlazor/Extensions/ITcpSocketFactoryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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([email protected]) Website: https://www.blazor.zone

using System.Runtime.Versioning;

namespace BootstrapBlazor.Components;

/// <summary>
/// <see cref="ITcpSocketFactory"/> 扩展方法类
/// </summary>
[UnsupportedOSPlatform("browser")]
public static class ITcpSocketFactoryExtensions
{
/// <summary>
/// Retrieves an existing TCP socket client by name or creates a new one using the specified IP address and port.
/// </summary>
/// <param name="factory">The <see cref="ITcpSocketFactory"/> instance used to manage TCP socket clients.</param>
/// <param name="name">The unique name identifying the TCP socket client. Cannot be null or empty.</param>
/// <param name="ipAddress">The IP address of the endpoint to connect to. Must be a valid IPv4 or IPv6 address.</param>
/// <param name="port">The port number of the endpoint to connect to. Must be within the range 0 to 65535.</param>
/// <returns>An <see cref="ITcpSocketClient"/> 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.</returns>
public static ITcpSocketClient GetOrCreate(this ITcpSocketFactory factory, string name, string ipAddress, int port)
{
var endPoint = Utility.ConvertToIpEndPoint(ipAddress, port);
return factory.GetOrCreate(name, endPoint);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ class DefaultTcpSocketFactory(IServiceProvider provider) : ITcpSocketFactory
{
private readonly ConcurrentDictionary<string, ITcpSocketClient> _pool = new();

public ITcpSocketClient GetOrCreate(string host, int port = 0)
public ITcpSocketClient GetOrCreate(string name, IPEndPoint endPoint)
{
return _pool.GetOrAdd($"{host}:{port}", key =>
return _pool.GetOrAdd(name, key =>
{
var endPoint = Utility.ConvertToIpEndPoint(host, port);
var client = new DefaultTcpSocketClient(endPoint)
{
Logger = provider.GetService<ILogger<DefaultTcpSocketClient>>()
Expand All @@ -29,10 +28,10 @@ public ITcpSocketClient GetOrCreate(string host, int port = 0)
});
}

public ITcpSocketClient? Remove(string host, int port)
public ITcpSocketClient? Remove(string name)
{
ITcpSocketClient? client = null;
if (_pool.TryRemove($"{host}:{port}", out var c))
if (_pool.TryRemove(name, out var c))
{
client = c;
}
Expand Down
28 changes: 15 additions & 13 deletions src/BootstrapBlazor/Services/TcpSocket/ITcpSocketFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone

using System.Net;

namespace BootstrapBlazor.Components;

/// <summary>
Expand All @@ -11,21 +13,21 @@ namespace BootstrapBlazor.Components;
public interface ITcpSocketFactory : IDisposable
{
/// <summary>
/// Retrieves an existing TCP socket associated with the specified host and port, or creates a new one if none
/// exists.
/// Retrieves an existing TCP socket client by name or creates a new one if it does not exist.
/// </summary>
/// <param name="host">The hostname or IP address of the remote endpoint. Cannot be null or empty.</param>
/// <param name="port">The port number of the remote endpoint. Must be a valid port number between 0 and 65535.</param>
/// <returns>An <see cref="ITcpSocketClient"/> instance representing the TCP socket for the specified host and port.</returns>
ITcpSocketClient GetOrCreate(string host, int port);
/// <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);

/// <summary>
/// Removes the specified host and port combination from the collection.
/// Removes the TCP socket client associated with the specified name.
/// </summary>
/// <remarks>If the specified host and port combination does not exist in the collection, the method has
/// no effect.</remarks>
/// <param name="host">The hostname to remove. Cannot be null or empty.</param>
/// <param name="port">The port number associated with the host to remove. Must be a valid port number (0-65535).</param>
/// <returns>An <see cref="ITcpSocketClient"/> instance representing the TCP socket for the specified host and port.</returns>
ITcpSocketClient? Remove(string host, int port);
/// <param name="name">The name of the TCP socket client to remove. Cannot be <see langword="null"/> or empty.</param>
/// <returns>The removed <see cref="ITcpSocketClient"/> instance if a client with the specified name exists; otherwise, <see
/// langword="null"/>.</returns>
ITcpSocketClient? Remove(string name);
}
13 changes: 6 additions & 7 deletions test/UnitTest/Services/TcpSocketFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,21 @@ public void GetOrCreate_Ok()
builder.AddProvider(new MockLoggerProvider());
});
sc.AddBootstrapBlazorTcpSocketFactory();

var provider = sc.BuildServiceProvider();
var factory = provider.GetRequiredService<ITcpSocketFactory>();
var client1 = factory.GetOrCreate("localhost", 0);
var client1 = factory.GetOrCreate("demo", "localhost", 0);
client1.Close();

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

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

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

var client5 = factory.Remove("256.0.0.1", 0);
var client5 = factory.Remove("demo2");
Assert.Equal(client4, client5);
Assert.NotNull(client5);

Expand Down Expand Up @@ -451,7 +450,7 @@ private static ITcpSocketClient CreateClient()
sc.AddBootstrapBlazorTcpSocketFactory();
var provider = sc.BuildServiceProvider();
var factory = provider.GetRequiredService<ITcpSocketFactory>();
var client = factory.GetOrCreate("localhost", 0);
var client = factory.GetOrCreate("test", "localhost", 0);
return client;
}

Expand Down
4 changes: 4 additions & 0 deletions test/UnitTest/Utils/UtilityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,10 @@ public void ConvertToIpEndPoint_Ok()
{
var ex = Assert.Throws<ArgumentOutOfRangeException>(() => Utility.ConvertToIpEndPoint("localhost", 88990));
Assert.NotNull(ex);

ex = null;
ex = Assert.Throws<ArgumentOutOfRangeException>(() => Utility.ConvertToIpEndPoint("localhost", -1000));
Assert.NotNull(ex);
}

[AutoGenerateClass(Align = Alignment.Center)]
Expand Down