Skip to content

Commit

Permalink
optimized code in SocketConnector
Browse files Browse the repository at this point in the history
  • Loading branch information
kerryjiang committed Feb 10, 2024
1 parent 27553dc commit 76ad533
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/SuperSocket.Client/SocketConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ protected override async ValueTask<ConnectState> ConnectAsync(EndPoint remoteEnd
#if NET5_0_OR_GREATER
await socket.ConnectAsync(remoteEndPoint, cancellationToken);
#else
Task result = socket.ConnectAsync(remoteEndPoint);
int index = Task.WaitAny(new[] { result }, cancellationToken);
var connected = socket.Connected;
if (!connected)
Task connectTask = socket.ConnectAsync(remoteEndPoint);

var tcs = new TaskCompletionSource<bool>();
cancellationToken.Register(() => tcs.SetResult(false));

await Task.WhenAny(new[] { connectTask, tcs.Task });

if (!socket.Connected)
{
socket.Close();

Expand Down

0 comments on commit 76ad533

Please sign in to comment.