Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -13,7 +13,7 @@ namespace BootstrapBlazor.Components;
/// TcpSocket 客户端默认实现
/// </summary>
[UnsupportedOSPlatform("browser")]
class DefaultSocketClient(IPEndPoint localEndPoint) : ISocketClient
class DefaultSocketClientProvider : ISocketClientProvider
{
private TcpClient? _client;

Expand All @@ -25,7 +25,7 @@ class DefaultSocketClient(IPEndPoint localEndPoint) : ISocketClient
/// <summary>
/// <inheritdoc/>
/// </summary>
public IPEndPoint LocalEndPoint { get; set; } = localEndPoint;
public IPEndPoint LocalEndPoint { get; set; } = new IPEndPoint(IPAddress.Any, 0);

/// <summary>
/// <inheritdoc/>
Expand Down
22 changes: 1 addition & 21 deletions src/BootstrapBlazor/Services/TcpSocket/DefaultTcpSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,12 @@
// 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.Runtime.Versioning;

namespace BootstrapBlazor.Components;

[UnsupportedOSPlatform("browser")]
sealed class DefaultTcpSocketClient : TcpSocketClientBase<DefaultSocketClient>
sealed class DefaultTcpSocketClient(SocketClientOptions options) : TcpSocketClientBase(options)
{
public DefaultTcpSocketClient(SocketClientOptions options)
{
ReceiveBufferSize = Math.Max(1024, options.ReceiveBufferSize);
IsAutoReceive = options.IsAutoReceive;
ConnectTimeout = options.ConnectTimeout;
SendTimeout = options.SendTimeout;
ReceiveTimeout = options.ReceiveTimeout;
LocalEndPoint = options.LocalEndPoint;
}

/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="localEndPoint"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
protected override DefaultSocketClient CreateSocketClient(IPEndPoint localEndPoint)
{
return new DefaultSocketClient(localEndPoint);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Collections.Concurrent;
using System.Runtime.Versioning;

Expand All @@ -23,7 +21,7 @@ public ITcpSocketClient GetOrCreate(string name, Action<SocketClientOptions> val
valueFactory(options);
var client = new DefaultTcpSocketClient(options)
{
Logger = provider.GetService<ILogger<DefaultTcpSocketClient>>()
ServiceProvider = provider,
};
return client;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ public static class TcpSocketExtensions
/// <returns></returns>
public static IServiceCollection AddBootstrapBlazorTcpSocketFactory(this IServiceCollection services)
{
// 添加 ITcpSocket 实现
services.TryAddSingleton<ITcpSocketFactory, DefaultTcpSocketFactory>();
// 添加 ITcpSocketFactory 服务
services.AddSingleton<ITcpSocketFactory, DefaultTcpSocketFactory>();

// 增加 ISocketClientProvider 服务
services.TryAddTransient<ISocketClientProvider, DefaultSocketClientProvider>();
return services;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace BootstrapBlazor.Components;
/// establishing connections, transmitting data, and receiving data asynchronously. Implementations of this interface
/// should ensure proper resource management, including closing connections and releasing resources when no longer
/// needed.</remarks>
public interface ISocketClient
public interface ISocketClientProvider
{
/// <summary>
/// Gets a value indicating whether the connection is currently active.
Expand All @@ -25,10 +25,7 @@ public interface ISocketClient
/// <summary>
/// Gets the local network endpoint that the socket is bound to.
/// </summary>
/// <remarks>This property provides information about the local endpoint of the socket, which is typically
/// used to identify the local address and port being used for communication. If the socket is not bound to a
/// specific local endpoint, this property may return <see langword="null"/>.</remarks>
IPEndPoint LocalEndPoint { get; }
IPEndPoint LocalEndPoint { get; set; }

/// <summary>
/// Establishes an asynchronous connection to the specified endpoint.
Expand Down
29 changes: 2 additions & 27 deletions src/BootstrapBlazor/Services/TcpSocket/ITcpSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,15 @@ namespace BootstrapBlazor.Components;
/// </summary>
public interface ITcpSocketClient : IAsyncDisposable
{
/// <summary>
/// Gets or sets the size, in bytes, of the receive buffer used for network operations.
/// </summary>
int ReceiveBufferSize { get; set; }

/// <summary>
/// Gets a value indicating whether the system is currently connected. Default is false.
/// </summary>
bool IsConnected { get; }

/// <summary>
/// Gets or sets a value indicating whether automatic receiving data is enabled. Default is true.
/// </summary>
bool IsAutoReceive { get; set; }

/// <summary>
/// Gets or sets the timeout duration, in milliseconds, for establishing a connection.
/// </summary>
int ConnectTimeout { get; set; }

/// <summary>
/// Gets or sets the duration, in milliseconds, to wait for a send operation to complete before timing out.
/// </summary>
/// <remarks>If the send operation does not complete within the specified timeout period, an exception may
/// be thrown.</remarks>
int SendTimeout { get; set; }

/// <summary>
/// Gets or sets the amount of time, in milliseconds, that the receiver will wait for a response before timing out.
/// Gets or sets the configuration options for the socket client.
/// </summary>
/// <remarks>Use this property to configure the maximum wait time for receiving a response. Setting an
/// appropriate timeout can help prevent indefinite blocking in scenarios where responses may be delayed or
/// unavailable.</remarks>
int ReceiveTimeout { get; set; }
SocketClientOptions Options { get; }

/// <summary>
/// Gets the local network endpoint that the socket is bound to.
Expand Down
4 changes: 2 additions & 2 deletions src/BootstrapBlazor/Services/TcpSocket/SocketClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public class SocketClientOptions
public int ReceiveTimeout { get; set; }

/// <summary>
/// Gets or sets the local endpoint for the socket client. Default value is <see cref="IPAddress.Loopback"/>
/// Gets or sets the local endpoint for the socket client. Default value is <see cref="IPAddress.Any"/>
/// </summary>
/// <remarks>This property specifies the local network endpoint that the socket client will bind to when establishing a connection.</remarks>
public IPEndPoint LocalEndPoint { get; set; } = new IPEndPoint(IPAddress.Loopback, 0);
public IPEndPoint LocalEndPoint { get; set; } = new IPEndPoint(IPAddress.Any, 0);
}
Loading