Improve Ip4Address class

This commit is contained in:
ig 2023-07-03 17:29:12 +02:00
parent cc8621d632
commit 9049b3e4d5
2 changed files with 31 additions and 2 deletions

View File

@ -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<TcpClient>
{
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,

View File

@ -2,6 +2,27 @@ namespace InnovEnergy.Lib.Utils.Net;
public class Ip4Address
{
public required String Address { 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();
}