@ -47,7 +47,8 @@ public class TcpClientProtocol : CommunicationProtocol
// check if client is null
// check if client is null
if ( this . client ! = null )
if ( this . client ! = null )
{
{
throw new Exception ( $"The TCP client was not null when {nameof(InternalConnect)} was called." ) ;
this . client = null ;
this . logger . LogWarn ( $"The TCP client was not null when {nameof(InternalConnect)} was called." , nameof ( TcpClientProtocol ) ) ;
}
}
/* create client */
/* create client */
@ -73,7 +74,14 @@ public class TcpClientProtocol : CommunicationProtocol
// send message to inform about resolved IP address
// send message to inform about resolved IP address
if ( this . client . Client . RemoteEndPoint is IPEndPoint remoteEndpoint )
if ( this . client . Client . RemoteEndPoint is IPEndPoint remoteEndpoint )
{
{
this . messenger . Send ( new TcpConnectedMessage ( remoteEndpoint . Address ) ) ;
IPAddress remoteEndpointAddress = remoteEndpoint . Address ;
// special handling for IPv4MappedToIPv6 addresses
if ( remoteEndpointAddress . IsIPv4MappedToIPv6 )
{
remoteEndpointAddress = remoteEndpoint . Address . MapToIPv4 ( ) ;
}
this . messenger . Send ( new TcpConnectedMessage ( remoteEndpointAddress ) ) ;
}
}
return true ;
return true ;
@ -128,25 +136,30 @@ public class TcpClientProtocol : CommunicationProtocol
catch ( ObjectDisposedException objex )
catch ( ObjectDisposedException objex )
{
{
this . logger . LogException ( objex , $"ObjectDisposedException while sending data in {nameof(InternalRead)}" , nameof ( TcpClientProtocol ) ) ;
this . logger . LogException ( objex , $"ObjectDisposedException while sending data in {nameof(InternalRead)}" , nameof ( TcpClientProtocol ) ) ;
this . OnUnintentionallyDisconnected ( ) ;
this . OnUnintentionallyDisconnected ( informUser : true ) ;
break ; // break loop
break ; // break loop
}
}
catch ( IOException ioEx )
catch ( IOException ioEx ) when ( ioEx . InnerException is SocketException sockEx )
{
{
// socket exception and timeout => normal use case
// socket exception timeout => normal use case => do nothing
if ( ioEx . InnerException is SocketException sockEx & & sockEx ? . SocketErrorCode = = SocketError . TimedOut ) { }
if ( sockEx . SocketErrorCode ! = SocketError . TimedOut )
// other IO Exception
else
{
{
this . logger . LogException ( ioEx , $"IOException while reading data in {nameof(InternalRead)}" , nameof ( TcpClientProtocol ) ) ;
this . logger . LogException ( sockEx , $"SocketException with SocketErrorCode={sockEx.SocketErrorCode} while reading data in {nameof(InternalRead)}" , nameof ( TcpClientProtocol ) ) ;
this . messenger . Send < IUserInterfaceMessage > ( new StoppedReadingUIMessage ( this , ioEx . ToString ( ) ) ) ;
this . messenger . Send < IUserInterfaceMessage > ( new StoppedReadingUIMessage ( this , $"Socket Error: {sockEx.SocketErrorCode}" ) ) ;
this . OnUnintentionallyDisconnected ( informUser : false ) ;
break ; // break loop
break ; // break loop
}
}
}
}
catch ( IOException ioEx )
{
this . logger . LogException ( ioEx , $"IOException while reading data in {nameof(InternalRead)}" , nameof ( TcpClientProtocol ) ) ;
this . OnUnintentionallyDisconnected ( informUser : true ) ;
break ; // break loop
}
catch ( Exception ex )
catch ( Exception ex )
{
{
this . logger . LogException ( ex , $"Exception while reading data in {nameof(InternalRead)}" , nameof ( TcpClientProtocol ) ) ;
this . logger . LogException ( ex , $"Exception while reading data in {nameof(InternalRead)}" , nameof ( TcpClientProtocol ) ) ;
this . messenger . Send < IUserInterfaceMessage > ( new StoppedReadingUIMessage ( this , ex . Message ) ) ;
this . OnUnintentionallyDisconnected ( informUser : true ) ;
break ; // break loop
break ; // break loop
}
}