Skip to content

Commit 8f6d41a

Browse files
committed
feat: 更改 DefaultTcpSocketClient 构造函数
1 parent 1281c3c commit 8f6d41a

File tree

3 files changed

+8
-38
lines changed

3 files changed

+8
-38
lines changed

src/BootstrapBlazor/Extensions/ITcpSocketClientExtensions.cs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
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;
6+
using System.Runtime.Versioning;
77
using System.Text;
88

99
namespace BootstrapBlazor.Components;
1010

1111
/// <summary>
1212
/// <see cref="ITcpSocketClient"/> 扩展方法类
1313
/// </summary>
14+
[UnsupportedOSPlatform("browser")]
1415
public static class ITcpSocketClientExtensions
1516
{
1617
/// <summary>
@@ -44,22 +45,7 @@ public static ValueTask<bool> SendAsync(this ITcpSocketClient client, string con
4445
/// is successfully established; otherwise, <see langword="false"/>.</returns>
4546
public static ValueTask<bool> ConnectAsync(this ITcpSocketClient client, string ipString, int port, CancellationToken token = default)
4647
{
47-
if (string.IsNullOrEmpty(ipString))
48-
{
49-
throw new ArgumentNullException(nameof(ipString), "IP address cannot be null or empty.");
50-
}
51-
52-
if (port < 0 || port > 65535)
53-
{
54-
throw new ArgumentOutOfRangeException(nameof(port), "Port must be between 0 and 65535.");
55-
}
56-
57-
if (!IPAddress.TryParse(ipString, out var address))
58-
{
59-
throw new InvalidOperationException($"Invalid IP address format: {ipString}");
60-
}
61-
62-
var endPoint = new IPEndPoint(address, port);
48+
var endPoint = Utility.ConvertToIpEndPoint(ipString, port);
6349
return client.ConnectAsync(endPoint, token);
6450
}
6551
}

src/BootstrapBlazor/Services/TcpSocket/DefaultTcpSocketClient.cs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace BootstrapBlazor.Components;
1313

1414
[UnsupportedOSPlatform("browser")]
15-
class DefaultTcpSocketClient : ITcpSocketClient
15+
class DefaultTcpSocketClient(IPEndPoint endPoint) : ITcpSocketClient
1616
{
1717
private TcpClient? _client;
1818
private IDataPackageHandler? _dataPackageHandler;
@@ -21,7 +21,7 @@ class DefaultTcpSocketClient : ITcpSocketClient
2121

2222
public bool IsConnected => _client?.Connected ?? false;
2323

24-
public IPEndPoint LocalEndPoint { get; set; }
24+
public IPEndPoint LocalEndPoint { get; set; } = endPoint;
2525

2626
[NotNull]
2727
public ILogger<DefaultTcpSocketClient>? Logger { get; set; }
@@ -30,29 +30,11 @@ class DefaultTcpSocketClient : ITcpSocketClient
3030

3131
public Func<ReadOnlyMemory<byte>, ValueTask>? ReceivedCallBack { get; set; }
3232

33-
public DefaultTcpSocketClient(string host, int port = 0)
34-
{
35-
LocalEndPoint = new IPEndPoint(GetIPAddress(host), port);
36-
}
37-
38-
private static IPAddress GetIPAddress(string host) => host.Equals("localhost", StringComparison.OrdinalIgnoreCase)
39-
? IPAddress.Loopback
40-
: IPAddress.TryParse(host, out var ip) ? ip : IPAddressByHostName;
41-
42-
[ExcludeFromCodeCoverage]
43-
private static IPAddress IPAddressByHostName => Dns.GetHostAddresses(Dns.GetHostName(), AddressFamily.InterNetwork).FirstOrDefault() ?? IPAddress.Loopback;
44-
4533
public void SetDataHandler(IDataPackageHandler handler)
4634
{
4735
_dataPackageHandler = handler;
4836
}
4937

50-
public ValueTask<bool> ConnectAsync(string host, int port, CancellationToken token = default)
51-
{
52-
var endPoint = new IPEndPoint(GetIPAddress(host), port);
53-
return ConnectAsync(endPoint, token);
54-
}
55-
5638
public async ValueTask<bool> ConnectAsync(IPEndPoint endPoint, CancellationToken token = default)
5739
{
5840
var ret = false;

src/BootstrapBlazor/Services/TcpSocket/DefaultTcpSocketFactory.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.Extensions.DependencyInjection;
77
using Microsoft.Extensions.Logging;
88
using System.Collections.Concurrent;
9+
using System.Net;
910
using System.Runtime.Versioning;
1011

1112
namespace BootstrapBlazor.Components;
@@ -19,7 +20,8 @@ public ITcpSocketClient GetOrCreate(string host, int port = 0)
1920
{
2021
return _pool.GetOrAdd($"{host}:{port}", key =>
2122
{
22-
var client = new DefaultTcpSocketClient(host, port)
23+
var endPoint = Utility.ConvertToIpEndPoint(host, port);
24+
var client = new DefaultTcpSocketClient(endPoint)
2325
{
2426
Logger = provider.GetService<ILogger<DefaultTcpSocketClient>>()
2527
};

0 commit comments

Comments
 (0)