@ -15,10 +15,10 @@ public class UdpProtocol : CommunicationProtocol
public override Types . ProtocolType ProtocolType = > Types . ProtocolType . Udp ;
public override Types . ProtocolType ProtocolType = > Types . ProtocolType . Udp ;
public override string InstanceIdentifier { get ; protected set ; } = string . Empty ;
public override string InstanceIdentifier { get ; protected set ; } = string . Empty ;
public override string LongInstanceIdentifier { get ; protected set ; } = string . Empty ;
private INetworkProtocolSettings ? settings ;
private INetworkProtocolSettings ? settings ;
private UdpClient ? receivingUdpClient ;
private UdpClient ? udpClient ;
private UdpClient ? sendingUdpClient ;
private const int ReadTimeoutMs = 1 0 0 ; // milliseconds until the read operation timeouts
private const int ReadTimeoutMs = 1 0 0 ; // milliseconds until the read operation timeouts
private const int WriteTimeoutMs = 1 0 0 ; // milliseconds until the write operation timeouts
private const int WriteTimeoutMs = 1 0 0 ; // milliseconds until the write operation timeouts
@ -38,68 +38,49 @@ public class UdpProtocol : CommunicationProtocol
// store locally
// store locally
this . settings = networkSettings ;
this . settings = networkSettings ;
// update identifier
// update identifiers
this . InstanceIdentifier = NetworkProtocolHelpers . GetLimitedLengthHostname ( this . settings ) ;
this . InstanceIdentifier = NetworkProtocolHelpers . GetLimitedLengthHostname ( this . settings ) ;
this . LongInstanceIdentifier = this . settings . Hostname ;
// check if clients are null
// check if client is null
if ( this . receivingUdpClient ! = n ull | | this . sendingU dpClient ! = null )
if ( this . udpClient ! = null )
{
{
throw new Exception ( $"A UDP client was not null when {nameof(InternalConnect)} was called: " +
throw new Exception ( $"UDP client was not null when {nameof(InternalConnect)} was called: " +
$"{nameof(receivingUdpClient)} isnull={this.receivingUdpClient == null}," +
$"{nameof(udpClient)} isnull={this.udpClient == null}," ) ;
$"{nameof(sendingUdpClient)} isnull={this.sendingUdpClient == null}" ) ;
}
}
/* create udp clients */
/* create udp client */
// try opening receiving udp socket
// try opening udp socket
try
try
{
{
this . receivingUdpClient = new UdpClient ( this . settings . Port ) ;
// opens an udp client on this pc and binds it to the port provided
this . udpClient = new UdpClient ( this . settings . Port ) ;
// define default remote host and port
this . udpClient . Connect ( this . settings . Hostname ! , this . settings . Port ) ;
}
}
catch ( Exception ex )
catch ( Exception ex )
{
{
this . logger . LogException ( ex , $"'{nameof(InternalConnect)}()' Opening Receiving UDP socket failed:" , nameof ( UdpProtocol ) ) ;
this . logger . LogException ( ex , $"'{nameof(InternalConnect)}()' Opening UDP socket failed:" , nameof ( UdpProtocol ) ) ;
// rollback
this . InternalDisconnect ( ) ;
return false ;
}
// try opening sending udp socket
try
{
this . sendingUdpClient = new UdpClient ( ) ;
this . sendingUdpClient . Connect ( this . settings . Hostname ! , this . settings . Port ) ;
}
catch ( Exception ex )
{
this . logger . LogException ( ex , $"'{nameof(InternalConnect)}()' Opening Sending UDP Socket failed:" , nameof ( UdpProtocol ) ) ;
// rollback
// rollback
this . InternalDisconnect ( ) ;
this . InternalDisconnect ( ) ;
return false ;
return false ;
}
}
// set static settings
// set static settings
this . receivingU dpClient. Client . ReceiveTimeout = ReadTimeoutMs ;
this . udpClient . Client . ReceiveTimeout = ReadTimeoutMs ;
this . sendingU dpClient. Client . SendTimeout = WriteTimeoutMs ;
this . udpClient . Client . SendTimeout = WriteTimeoutMs ;
return true ;
return true ;
}
}
protected override void InternalDisconnect ( )
protected override void InternalDisconnect ( )
{
{
// close receiving udp client
// close udp client
if ( this . receivingUdpClient ! = null )
if ( this . udpClient ! = null )
{
this . receivingUdpClient ? . Close ( ) ;
this . receivingUdpClient ? . Dispose ( ) ;
this . receivingUdpClient = null ;
}
// close sending udp client
if ( this . sendingUdpClient ! = null )
{
{
this . sendingU dpClient? . Close ( ) ;
this . udpClient ? . Close ( ) ;
this . sendingU dpClient? . Dispose ( ) ;
this . udpClient ? . Dispose ( ) ;
this . sendingU dpClient = null ;
this . udpClient = null ;
}
}
// reset settings
// reset settings
this . settings = null ;
this . settings = null ;
@ -117,7 +98,7 @@ public class UdpProtocol : CommunicationProtocol
try
try
{
{
// will throw ObjectDisposedException if null
// will throw ObjectDisposedException if null
receivedBytes = this . receivingU dpClient! . Receive ( ref remoteEndPoint ) ;
receivedBytes = this . u dpClient! . Receive ( ref remoteEndPoint ) ;
}
}
catch ( OperationCanceledException ) // intentionally cancelled => just break
catch ( OperationCanceledException ) // intentionally cancelled => just break
{
{
@ -171,7 +152,7 @@ public class UdpProtocol : CommunicationProtocol
try
try
{
{
// will throw ObjectDisposedException if null
// will throw ObjectDisposedException if null
this . sendingU dpClient! . Send ( bytes , bytes . Length ) ;
this . u dpClient! . Send ( bytes , bytes . Length ) ;
}
}
catch ( ObjectDisposedException objex )
catch ( ObjectDisposedException objex )
{
{