Skip to content

Commit 02e4071

Browse files
committed
refactor: 增加 ConnectAsync 扩展方法
1 parent d691746 commit 02e4071

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

src/BootstrapBlazor/Extensions/ITcpSocketClientExtensions.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
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;
67
using System.Text;
78

89
namespace BootstrapBlazor.Components;
@@ -30,4 +31,35 @@ public static ValueTask<bool> SendAsync(this ITcpSocketClient client, string con
3031
var buffer = encoding?.GetBytes(content) ?? Encoding.UTF8.GetBytes(content);
3132
return client.SendAsync(buffer, token);
3233
}
34+
35+
/// <summary>
36+
/// Establishes an asynchronous connection to the specified host and port.
37+
/// </summary>
38+
/// <param name="client">The TCP socket client to which the content will be sent. Cannot be <see langword="null"/>.</param>
39+
/// <param name="ipString">The hostname or IP address of the server to connect to. Cannot be null or empty.</param>
40+
/// <param name="port">The port number on the server to connect to. Must be a valid port number between 0 and 65535.</param>
41+
/// <param name="token">An optional <see cref="CancellationToken"/> to cancel the connection attempt. Defaults to <see
42+
/// langword="default"/> if not provided.</param>
43+
/// <returns>A task that represents the asynchronous operation. The task result is <see langword="true"/> if the connection
44+
/// is successfully established; otherwise, <see langword="false"/>.</returns>
45+
public static ValueTask<bool> ConnectAsync(this ITcpSocketClient client, string ipString, int port, CancellationToken token = default)
46+
{
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);
63+
return client.ConnectAsync(endPoint, token);
64+
}
3365
}

src/BootstrapBlazor/Services/TcpSocket/ITcpSocketClient.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,6 @@ public interface ITcpSocketClient : IDisposable
4444
/// <param name="handler">The handler responsible for processing data packages. Cannot be null.</param>
4545
void SetDataHandler(IDataPackageHandler handler);
4646

47-
/// <summary>
48-
/// Establishes an asynchronous connection to the specified host and port.
49-
/// </summary>
50-
/// <param name="host">The hostname or IP address of the server to connect to. Cannot be null or empty.</param>
51-
/// <param name="port">The port number on the server to connect to. Must be a valid port number between 0 and 65535.</param>
52-
/// <param name="token">An optional <see cref="CancellationToken"/> to cancel the connection attempt. Defaults to <see
53-
/// langword="default"/> if not provided.</param>
54-
/// <returns>A task that represents the asynchronous operation. The task result is <see langword="true"/> if the connection
55-
/// is successfully established; otherwise, <see langword="false"/>.</returns>
56-
ValueTask<bool> ConnectAsync(string host, int port, CancellationToken token = default);
57-
5847
/// <summary>
5948
/// Establishes an asynchronous connection to the specified endpoint.
6049
/// </summary>

0 commit comments

Comments
 (0)