Skip to content
Merged
Changes from 5 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
22 changes: 14 additions & 8 deletions src/BootstrapBlazor/Services/TcpSocket/DefaultTcpSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public override async ValueTask<bool> ConnectAsync(IPEndPoint endPoint, Cancella

// 创建新的 TcpClient 实例
_client ??= new TcpClient(localEndPoint);
_remoteEndPoint = endPoint;

var connectionToken = token;
if (ConnectTimeout > 0)
Expand All @@ -43,15 +44,19 @@ public override async ValueTask<bool> ConnectAsync(IPEndPoint endPoint, Cancella
}
await _client.ConnectAsync(endPoint, connectionToken);

// 设置本地以及远端端点信息
LocalEndPoint = (IPEndPoint)_client.Client.LocalEndPoint!;
_remoteEndPoint = endPoint;

if (IsAutoReceive)
if (_client.Connected)
{
_ = Task.Run(AutoReceiveAsync);
// 设置本地端点信息
if (_client.Client.LocalEndPoint is IPEndPoint local)
{
LocalEndPoint = local;
}
if (IsAutoReceive)
{
_ = Task.Run(AutoReceiveAsync);
}
}
ret = true;
ret = _client.Connected;
}
catch (OperationCanceledException ex)
{
Expand Down Expand Up @@ -119,7 +124,7 @@ public override async ValueTask<bool> SendAsync(ReadOnlyMemory<byte> data, Cance

public override async ValueTask<Memory<byte>> ReceiveAsync(CancellationToken token = default)
{
if (_client == null || !_client.Connected)
if (_client is not { Connected: true })
{
throw new InvalidOperationException($"TCP Socket is not connected {LocalEndPoint}");
}
Expand Down Expand Up @@ -188,6 +193,7 @@ private async ValueTask<int> ReceiveCoreAsync(TcpClient client, Memory<byte> buf
{
await DataPackageHandler.ReceiveAsync(buffer, receiveToken);
}
len = buffer.Length;
}
}
catch (OperationCanceledException ex)
Expand Down