Update IP4address if have null reading.

Create Nullchannel and null exception
This commit is contained in:
atef 2023-10-23 10:36:54 +02:00
parent af0da881f3
commit 25ef41b330
3 changed files with 30 additions and 5 deletions

View File

@ -0,0 +1,14 @@
namespace InnovEnergy.Lib.Protocols.Modbus.Channels;
public class NullChannel : Channel
{
public override IReadOnlyList<Byte> Read(Int32 nBytes) => throw new NullChannelException();
public override void Write(IReadOnlyList<Byte> bytes)
{
}
}
public class NullChannelException : Exception
{
}

View File

@ -11,8 +11,8 @@ public class TcpChannel : ConnectionChannel<TcpClient>
public TcpChannel(Ip4Address ip4Address, public TcpChannel(Ip4Address ip4Address,
Boolean closeAfterSuccessfulRead = false, Boolean closeAfterSuccessfulRead = false,
Boolean closeAfterSuccessfulWrite = false) : Boolean closeAfterSuccessfulWrite = false) :
this(ip4Address.Host, this(ip4Address.Host ?? "null",
ip4Address.Port, ip4Address.Port.HasValue ? (Int32)ip4Address.Port.Value : 0,
closeAfterSuccessfulRead, closeAfterSuccessfulRead,
closeAfterSuccessfulWrite) closeAfterSuccessfulWrite)
{ {

View File

@ -1,9 +1,11 @@
using System.Net;
namespace InnovEnergy.Lib.Utils.Net; namespace InnovEnergy.Lib.Utils.Net;
public class Ip4Address public class Ip4Address
{ {
public required String Host { get; init; } public required String? Host { get; init; }
public required UInt16 Port { get; init; } public required UInt16? Port { get; init; }
public override String ToString() => $"{Host}:{Port}"; public override String ToString() => $"{Host}:{Port}";
@ -18,6 +20,7 @@ public class Ip4Address
if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true; if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false; if (obj.GetType() != GetType()) return false;
return Equals((Ip4Address)obj); return Equals((Ip4Address)obj);
} }
@ -25,4 +28,12 @@ public class Ip4Address
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(); public override Int32 GetHashCode() => ToString().GetHashCode();
} }