Skip to content

Commit

Permalink
Merge pull request #1047 from xPaw/beginconnect-exception
Browse files Browse the repository at this point in the history
Catch BeginConnect exceptions in TcpConnection
  • Loading branch information
yaakov-h authored Nov 10, 2021
2 parents 338615c + 1a5c6c4 commit ef20df8
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions SteamKit2/SteamKit2/Networking/Steam3/TcpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,19 @@ private void ConnectCompleted(bool success)
// If we have no cancellation token source, we were already Release()'ed
if (cancellationToken?.IsCancellationRequested ?? true)
{
log.LogDebug("TcpConnection", "Connection request to {0} was cancelled", CurrentEndPoint);
log.LogDebug( nameof( TcpConnection ), "Connection request to {0} was cancelled", CurrentEndPoint );
if (success) Shutdown();
Release( userRequestedDisconnect: true );
return;
}
else if (!success)
{
log.LogDebug( "TcpConnection", "Timed out while connecting to {0}", CurrentEndPoint);
log.LogDebug( nameof( TcpConnection ), "Failed connecting to {0}", CurrentEndPoint );
Release( userRequestedDisconnect: false );
return;
}

log.LogDebug( "TcpConnection", "Connected to {0}", CurrentEndPoint);
log.LogDebug( nameof( TcpConnection ), "Connected to {0}", CurrentEndPoint );

try
{
Expand All @@ -140,7 +140,7 @@ private void ConnectCompleted(bool success)
}
catch (Exception ex)
{
log.LogDebug( "TcpConnection", "Exception while setting up connection to {0}: {1}", CurrentEndPoint, ex);
log.LogDebug( nameof( TcpConnection ), "Exception while setting up connection to {0}: {1}", CurrentEndPoint, ex );
Release( userRequestedDisconnect: false );
}
}
Expand All @@ -152,12 +152,24 @@ private void TryConnect(object sender)
int timeout = (int)sender;
if (cancellationToken.IsCancellationRequested)
{
log.LogDebug( "TcpConnection", "Connection to {0} cancelled by user", CurrentEndPoint);
log.LogDebug( nameof( TcpConnection ), "Connection to {0} cancelled by user", CurrentEndPoint );
Release( userRequestedDisconnect: true );
return;
}

var asyncResult = socket!.BeginConnect(CurrentEndPoint, null, null );

IAsyncResult asyncResult;

try
{
asyncResult = socket!.BeginConnect( CurrentEndPoint, null, null );
}
catch ( Exception ex )
{
log.LogDebug( nameof( TcpConnection ), "Exception while beginning connection request to {0}: {1}", CurrentEndPoint, ex );
ConnectCompleted( false );
return;
}

if ( WaitHandle.WaitAny( new WaitHandle[] { asyncResult.AsyncWaitHandle, cancellationToken.Token.WaitHandle }, timeout ) == 0 )
{
try
Expand All @@ -167,7 +179,7 @@ private void TryConnect(object sender)
}
catch ( Exception ex )
{
log.LogDebug( "TcpConnection", "Socket exception while completing connection request to {0}: {1}", CurrentEndPoint, ex );
log.LogDebug( nameof( TcpConnection ), "Exception while completing connection request to {0}: {1}", CurrentEndPoint, ex );
ConnectCompleted( false );
}
}
Expand All @@ -194,7 +206,7 @@ public void Connect(EndPoint endPoint, int timeout)
socket.SendTimeout = timeout;

CurrentEndPoint = endPoint;
log.LogDebug( "TcpConnection", "Connecting to {0}...", CurrentEndPoint);
log.LogDebug( nameof( TcpConnection ), "Connecting to {0}...", CurrentEndPoint );
TryConnect( timeout );
}

Expand Down Expand Up @@ -231,7 +243,7 @@ void NetLoop()
}
catch (SocketException ex)
{
log.LogDebug( "TcpConnection", "Socket exception while polling: {0}", ex);
log.LogDebug( nameof( TcpConnection ), "Socket exception while polling: {0}", ex );
break;
}

Expand All @@ -250,7 +262,7 @@ void NetLoop()
}
catch (IOException ex)
{
log.LogDebug("TcpConnection", "Socket exception occurred while reading packet: {0}", ex);
log.LogDebug( nameof( TcpConnection ), "Socket exception occurred while reading packet: {0}", ex );
break;
}

Expand All @@ -260,7 +272,7 @@ void NetLoop()
}
catch (Exception ex)
{
log.LogDebug( "TcpConnection", "Unexpected exception propogated back to NetLoop: {0}", ex);
log.LogDebug( nameof( TcpConnection ), "Unexpected exception propogated back to NetLoop: {0}", ex );
}
}

Expand Down Expand Up @@ -313,7 +325,7 @@ public void Send( byte[] data )
{
if (socket == null || netStream == null)
{
log.LogDebug( "TcpConnection", "Attempting to send client data when not connected.");
log.LogDebug( nameof( TcpConnection ), "Attempting to send client data when not connected." );
return;
}

Expand All @@ -325,7 +337,7 @@ public void Send( byte[] data )
}
catch (IOException ex)
{
log.LogDebug( "TcpConnection", "Socket exception while writing data: {0}", ex);
log.LogDebug( nameof( TcpConnection ), "Socket exception while writing data: {0}", ex );
}
}
}
Expand All @@ -345,7 +357,7 @@ public IPAddress GetLocalIP()
}
catch (Exception ex)
{
log.LogDebug( "TcpConnection", "Socket exception trying to read bound IP: {0}", ex);
log.LogDebug( nameof( TcpConnection ), "Socket exception trying to read bound IP: {0}", ex );
return IPAddress.None;
}
}
Expand Down

0 comments on commit ef20df8

Please sign in to comment.