From 25ef41b330dc3074b4c19cc3db741b64598905e3 Mon Sep 17 00:00:00 2001 From: atef Date: Mon, 23 Oct 2023 10:36:54 +0200 Subject: [PATCH] Update IP4address if have null reading. Create Nullchannel and null exception --- .../Protocols/Modbus/Channels/NullChannel.cs | 14 ++++++++++++++ .../Lib/Protocols/Modbus/Channels/TcpChannel.cs | 4 ++-- csharp/Lib/Utils/Net/Ip4Address.cs | 17 ++++++++++++++--- 3 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 csharp/Lib/Protocols/Modbus/Channels/NullChannel.cs diff --git a/csharp/Lib/Protocols/Modbus/Channels/NullChannel.cs b/csharp/Lib/Protocols/Modbus/Channels/NullChannel.cs new file mode 100644 index 000000000..34436f75e --- /dev/null +++ b/csharp/Lib/Protocols/Modbus/Channels/NullChannel.cs @@ -0,0 +1,14 @@ +namespace InnovEnergy.Lib.Protocols.Modbus.Channels; + +public class NullChannel : Channel +{ + public override IReadOnlyList Read(Int32 nBytes) => throw new NullChannelException(); + + public override void Write(IReadOnlyList bytes) + { + } +} + +public class NullChannelException : Exception +{ +} \ No newline at end of file diff --git a/csharp/Lib/Protocols/Modbus/Channels/TcpChannel.cs b/csharp/Lib/Protocols/Modbus/Channels/TcpChannel.cs index 4aa3bd71b..22a20d018 100644 --- a/csharp/Lib/Protocols/Modbus/Channels/TcpChannel.cs +++ b/csharp/Lib/Protocols/Modbus/Channels/TcpChannel.cs @@ -11,8 +11,8 @@ public class TcpChannel : ConnectionChannel public TcpChannel(Ip4Address ip4Address, Boolean closeAfterSuccessfulRead = false, Boolean closeAfterSuccessfulWrite = false) : - this(ip4Address.Host, - ip4Address.Port, + this(ip4Address.Host ?? "null", + ip4Address.Port.HasValue ? (Int32)ip4Address.Port.Value : 0, closeAfterSuccessfulRead, closeAfterSuccessfulWrite) { diff --git a/csharp/Lib/Utils/Net/Ip4Address.cs b/csharp/Lib/Utils/Net/Ip4Address.cs index db1eda3de..1352f1025 100644 --- a/csharp/Lib/Utils/Net/Ip4Address.cs +++ b/csharp/Lib/Utils/Net/Ip4Address.cs @@ -1,9 +1,11 @@ +using System.Net; + namespace InnovEnergy.Lib.Utils.Net; public class Ip4Address { - public required String Host { 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}"; @@ -18,6 +20,7 @@ public class Ip4Address if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(this, obj)) return true; if (obj.GetType() != GetType()) return false; + return Equals((Ip4Address)obj); } @@ -25,4 +28,12 @@ public class Ip4Address public static Boolean operator !=(Ip4Address? left, Ip4Address? right) => !Equals(left, right); public override Int32 GetHashCode() => ToString().GetHashCode(); -} \ No newline at end of file +} + + + + + + + +