diff --git a/csharp/Lib/Protocols/Modbus/Channels/TcpChannel.cs b/csharp/Lib/Protocols/Modbus/Channels/TcpChannel.cs index f21946408..d026cf6cf 100644 --- a/csharp/Lib/Protocols/Modbus/Channels/TcpChannel.cs +++ b/csharp/Lib/Protocols/Modbus/Channels/TcpChannel.cs @@ -1,10 +1,18 @@ using System.Net.Sockets; using InnovEnergy.Lib.Protocols.Modbus.Protocol; +using InnovEnergy.Lib.Utils.Net; namespace InnovEnergy.Lib.Protocols.Modbus.Channels; public class TcpChannel : ConnectionChannel { + + public TcpChannel(Ip4Address ip4Address,Boolean closeAfterSuccessfulRead = false, + Boolean closeAfterSuccessfulWrite = false) : this(ip4Address.Host, ip4Address.Port, closeAfterSuccessfulRead, closeAfterSuccessfulWrite) + { + + } + public TcpChannel(String hostname, Int32 port, Boolean closeAfterSuccessfulRead = false, diff --git a/csharp/Lib/Utils/Net/Ip4Address.cs b/csharp/Lib/Utils/Net/Ip4Address.cs index 8c343247b..db1eda3de 100644 --- a/csharp/Lib/Utils/Net/Ip4Address.cs +++ b/csharp/Lib/Utils/Net/Ip4Address.cs @@ -2,6 +2,27 @@ namespace InnovEnergy.Lib.Utils.Net; public class Ip4Address { - public required String Address { get; init; } - public required UInt16 Port { get; init; } + public required String Host { get; init; } + public required UInt16 Port { get; init; } + + public override String ToString() => $"{Host}:{Port}"; + + protected Boolean Equals(Ip4Address other) + { + return Host == other.Host + && Port == other.Port; + } + + public override Boolean Equals(Object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != GetType()) return false; + return Equals((Ip4Address)obj); + } + + public static Boolean operator ==(Ip4Address? left, Ip4Address? right) => Equals(left, right); + public static Boolean operator !=(Ip4Address? left, Ip4Address? right) => !Equals(left, right); + + public override Int32 GetHashCode() => ToString().GetHashCode(); } \ No newline at end of file