Improve Ip4Address class
This commit is contained in:
parent
cc8621d632
commit
9049b3e4d5
|
@ -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,
|
||||
|
|
|
@ -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();
|
||||
}
|
Loading…
Reference in New Issue