@ -1,7 +1,9 @@
using Common.Logging ;
using Common.Logging ;
using Common.Messaging ;
using Common.Messaging ;
using CommunityToolkit.Mvvm.Messaging ;
using CommunityToolkit.Mvvm.Messaging ;
using MultiTerm.Protocols.Helpers ;
using MultiTerm.Protocols.Model ;
using MultiTerm.Protocols.Model ;
using MultiTerm.Protocols.Network ;
using System.Net.Sockets ;
using System.Net.Sockets ;
namespace MultiTerm.Protocols.Udp ;
namespace MultiTerm.Protocols.Udp ;
@ -12,40 +14,27 @@ public class UdpProtocol : CommunicationProtocol
public override string InstanceIdentifier { get ; protected set ; } = string . Empty ;
public override string InstanceIdentifier { get ; protected set ; } = string . Empty ;
private IUdpProtocolSettings ? udpS ettings ;
private INetworkProtocolSettings ? s ettings ;
private UdpClient ? receivingUdpClient ;
private UdpClient ? receivingUdpClient ;
private UdpClient ? sendingUdpClient ;
private UdpClient ? sendingUdpClient ;
private const int MaxInstanceIdentifierLength = 2 0 ; // maximum number of characters for the InstanceIdentifier
public UdpProtocol ( ILogger logger , IMessenger messenger ) : base ( logger , messenger ) { }
public UdpProtocol ( ILogger logger , IMessenger messenger ) : base ( logger , messenger ) { }
protected override bool InternalConnect ( IProtocolSettings settings )
protected override bool InternalConnect ( IProtocolSettings settings )
{
{
// check if settings are of correct type
// check if settings are of correct type
if ( settings is not IUdpProtocolSettings udpProtocol Settings )
if ( settings is not INetworkProtocolSettings network Settings )
{
{
this . udpS ettings = null ;
this . s ettings = null ;
throw new ArgumentException ( $"Cannot connect due to wrong type of Protocol Settings. " +
throw new ArgumentException ( $"Cannot connect due to wrong type of Protocol Settings. " +
$"Check parameter {nameof(settings)}' of '{nameof(InternalConnect)}()' in {nameof(UdpProtocol)}." ) ;
$"Check parameter {nameof(settings)}' of '{nameof(InternalConnect)}()' in {nameof(UdpProtocol)}." ) ;
}
}
// store locally
// store locally
this . udpSettings = udpProtocol Settings;
this . settings = network Settings;
// update identifier
// update identifier
if ( this . udpSettings . Hostname ! = null & & this . udpSettings . Hostname . Length > = MaxInstanceIdentifierLength )
this . InstanceIdentifier = NetworkProtocolHelpers . GetLimitedLengthHostname ( this . settings ) ;
{
this . InstanceIdentifier = $"...{this.udpSettings.Hostname.Substring(this.udpSettings.Hostname.Length - MaxInstanceIdentifierLength, MaxInstanceIdentifierLength)}" ;
}
else if ( this . udpSettings . Hostname ! = null )
{
this . InstanceIdentifier = $"{this.udpSettings.Hostname}" ;
}
else
{
this . InstanceIdentifier = "invalid" ;
}
// check if clients are null
// check if clients are null
if ( this . receivingUdpClient ! = null | | this . sendingUdpClient ! = null )
if ( this . receivingUdpClient ! = null | | this . sendingUdpClient ! = null )
@ -59,7 +48,7 @@ public class UdpProtocol : CommunicationProtocol
// try opening receiving udp socket
// try opening receiving udp socket
try
try
{
{
this . receivingUdpClient = new UdpClient ( this . udpS ettings. Port ) ;
this . receivingUdpClient = new UdpClient ( this . s ettings. Port ) ;
}
}
catch ( Exception ex )
catch ( Exception ex )
{
{
@ -74,7 +63,7 @@ public class UdpProtocol : CommunicationProtocol
try
try
{
{
this . sendingUdpClient = new UdpClient ( ) ;
this . sendingUdpClient = new UdpClient ( ) ;
this . sendingUdpClient . Connect ( this . udpS ettings. Hostname ! , this . udpS ettings. Port ) ;
this . sendingUdpClient . Connect ( this . s ettings. Hostname ! , this . s ettings. Port ) ;
}
}
catch ( Exception ex )
catch ( Exception ex )
{
{
@ -136,7 +125,7 @@ public class UdpProtocol : CommunicationProtocol
{
{
this . logger . LogException ( ex , $"Exception while reading data in {nameof(InternalRead)}" , nameof ( UdpProtocol ) ) ;
this . logger . LogException ( ex , $"Exception while reading data in {nameof(InternalRead)}" , nameof ( UdpProtocol ) ) ;
this . messenger . Send < IUserInterfaceMessage > ( new GenericUserInterfaceMessage ( $"UDP client ended reading on port " +
this . messenger . Send < IUserInterfaceMessage > ( new GenericUserInterfaceMessage ( $"UDP client ended reading on port " +
$"{this.udpS ettings!.Port} because of exception." , MessageImportance . Medium ) ) ;
$"{this.s ettings!.Port} because of exception." , MessageImportance . Medium ) ) ;
break ; // break loop
break ; // break loop
}
}