Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private async Task CreateClient()
}
</Pre>

<p>内置数据库处理器</p>
<p>内置数据处理器</p>

<ul class="ul-demo">
<li><code>FixLengthDataPackageHandler</code> <b>固定长度数据处理器</b> 即每个通讯包都是固定长度</li>
Expand Down
32 changes: 32 additions & 0 deletions src/BootstrapBlazor/Extensions/ITcpSocketClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone

using System.Net;
using System.Text;

namespace BootstrapBlazor.Components;
Expand Down Expand Up @@ -30,4 +31,35 @@ public static ValueTask<bool> SendAsync(this ITcpSocketClient client, string con
var buffer = encoding?.GetBytes(content) ?? Encoding.UTF8.GetBytes(content);
return client.SendAsync(buffer, token);
}

/// <summary>
/// Establishes an asynchronous connection to the specified host and port.
/// </summary>
/// <param name="client">The TCP socket client to which the content will be sent. Cannot be <see langword="null"/>.</param>
/// <param name="ipString">The hostname or IP address of the server to connect to. Cannot be null or empty.</param>
/// <param name="port">The port number on the server to connect to. Must be a valid port number between 0 and 65535.</param>
/// <param name="token">An optional <see cref="CancellationToken"/> to cancel the connection attempt. Defaults to <see
/// langword="default"/> if not provided.</param>
/// <returns>A task that represents the asynchronous operation. The task result is <see langword="true"/> if the connection
/// is successfully established; otherwise, <see langword="false"/>.</returns>
public static ValueTask<bool> ConnectAsync(this ITcpSocketClient client, string ipString, int port, CancellationToken token = default)
{
if (string.IsNullOrEmpty(ipString))
{
throw new ArgumentNullException(nameof(ipString), "IP address cannot be null or empty.");
}

if (port < 0 || port > 65535)
{
throw new ArgumentOutOfRangeException(nameof(port), "Port must be between 0 and 65535.");
}

if (!IPAddress.TryParse(ipString, out var address))
{
throw new InvalidOperationException($"Invalid IP address format: {ipString}");
}

var endPoint = new IPEndPoint(address, port);
return client.ConnectAsync(endPoint, token);
}
}
11 changes: 0 additions & 11 deletions src/BootstrapBlazor/Services/TcpSocket/ITcpSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,6 @@ public interface ITcpSocketClient : IDisposable
/// <param name="handler">The handler responsible for processing data packages. Cannot be null.</param>
void SetDataHandler(IDataPackageHandler handler);

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

/// <summary>
/// Establishes an asynchronous connection to the specified endpoint.
/// </summary>
Expand Down
Loading