@@ -21,7 +21,7 @@ sealed class DefaultTcpSocketClient(IPEndPoint localEndPoint) : TcpSocketClientB
2121 public override bool IsConnected => _client ? . Connected ?? false ;
2222
2323 [ NotNull ]
24- public ILogger < DefaultTcpSocketClient > ? Logger { get ; set ; }
24+ public ILogger < DefaultTcpSocketClient > ? Logger { get ; init ; }
2525
2626 public override async ValueTask < bool > ConnectAsync ( IPEndPoint endPoint , CancellationToken token = default )
2727 {
@@ -33,7 +33,8 @@ public override async ValueTask<bool> ConnectAsync(IPEndPoint endPoint, Cancella
3333
3434 // 创建新的 TcpClient 实例
3535 _client ??= new TcpClient ( localEndPoint ) ;
36- _remoteEndPoint = endPoint ;
36+ LocalEndPoint = localEndPoint ;
37+ _remoteEndPoint = null ;
3738
3839 var connectionToken = token ;
3940 if ( ConnectTimeout > 0 )
@@ -46,28 +47,25 @@ public override async ValueTask<bool> ConnectAsync(IPEndPoint endPoint, Cancella
4647
4748 if ( _client . Connected )
4849 {
50+ _remoteEndPoint = endPoint ;
51+
4952 // 设置本地端点信息
5053 if ( _client . Client . LocalEndPoint is IPEndPoint local )
5154 {
5255 LocalEndPoint = local ;
5356 }
5457 if ( IsAutoReceive )
5558 {
56- _ = Task . Run ( AutoReceiveAsync ) ;
59+ _ = Task . Run ( AutoReceiveAsync , token ) ;
5760 }
5861 }
5962 ret = _client . Connected ;
6063 }
6164 catch ( OperationCanceledException ex )
6265 {
63- if ( token . IsCancellationRequested )
64- {
65- Logger . LogWarning ( ex , "TCP Socket connect operation was canceled from {LocalEndPoint} to {RemoteEndPoint}" , LocalEndPoint , endPoint ) ;
66- }
67- else
68- {
69- Logger . LogWarning ( ex , "TCP Socket connect operation timed out from {LocalEndPoint} to {RemoteEndPoint}" , LocalEndPoint , endPoint ) ;
70- }
66+ Logger . LogWarning ( ex , token . IsCancellationRequested
67+ ? "TCP Socket connect operation was canceled from {LocalEndPoint} to {RemoteEndPoint}"
68+ : "TCP Socket connect operation timed out from {LocalEndPoint} to {RemoteEndPoint}" , LocalEndPoint , endPoint ) ;
7169 }
7270 catch ( Exception ex )
7371 {
@@ -106,14 +104,9 @@ public override async ValueTask<bool> SendAsync(ReadOnlyMemory<byte> data, Cance
106104 }
107105 catch ( OperationCanceledException ex )
108106 {
109- if ( token . IsCancellationRequested )
110- {
111- Logger . LogWarning ( ex , "TCP Socket send operation was canceled from {LocalEndPoint} to {RemoteEndPoint}" , LocalEndPoint , _remoteEndPoint ) ;
112- }
113- else
114- {
115- Logger . LogWarning ( ex , "TCP Socket send operation timed out from {LocalEndPoint} to {RemoteEndPoint}" , LocalEndPoint , _remoteEndPoint ) ;
116- }
107+ Logger . LogWarning ( ex , token . IsCancellationRequested
108+ ? "TCP Socket send operation was canceled from {LocalEndPoint} to {RemoteEndPoint}"
109+ : "TCP Socket send operation timed out from {LocalEndPoint} to {RemoteEndPoint}" , LocalEndPoint , _remoteEndPoint ) ;
117110 }
118111 catch ( Exception ex )
119112 {
@@ -137,7 +130,7 @@ public override async ValueTask<Memory<byte>> ReceiveAsync(CancellationToken tok
137130 using var block = MemoryPool < byte > . Shared . Rent ( ReceiveBufferSize ) ;
138131 var buffer = block . Memory ;
139132 var len = await ReceiveCoreAsync ( _client , buffer , token ) ;
140- return buffer [ 0 ..len ] ;
133+ return buffer [ ..len ] ;
141134 }
142135
143136 private async ValueTask AutoReceiveAsync ( )
@@ -198,14 +191,9 @@ private async ValueTask<int> ReceiveCoreAsync(TcpClient client, Memory<byte> buf
198191 }
199192 catch ( OperationCanceledException ex )
200193 {
201- if ( token . IsCancellationRequested )
202- {
203- Logger . LogWarning ( ex , "TCP Socket receive operation canceled from {LocalEndPoint} to {RemoteEndPoint}" , LocalEndPoint , _remoteEndPoint ) ;
204- }
205- else
206- {
207- Logger . LogWarning ( ex , "TCP Socket receive operation timed out from {LocalEndPoint} to {RemoteEndPoint}" , LocalEndPoint , _remoteEndPoint ) ;
208- }
194+ Logger . LogWarning ( ex , token . IsCancellationRequested
195+ ? "TCP Socket receive operation canceled from {LocalEndPoint} to {RemoteEndPoint}"
196+ : "TCP Socket receive operation timed out from {LocalEndPoint} to {RemoteEndPoint}" , LocalEndPoint , _remoteEndPoint ) ;
209197 }
210198 catch ( Exception ex )
211199 {
@@ -220,9 +208,6 @@ protected override async ValueTask DisposeAsync(bool disposing)
220208
221209 if ( disposing )
222210 {
223- LocalEndPoint = null ;
224- _remoteEndPoint = null ;
225-
226211 // 取消接收数据的任务
227212 if ( _receiveCancellationTokenSource != null )
228213 {
0 commit comments