diff --git a/NET Core/LibUA/Client.cs b/NET Core/LibUA/Client.cs index 27afa5f..5756ab9 100644 --- a/NET Core/LibUA/Client.cs +++ b/NET Core/LibUA/Client.cs @@ -2038,9 +2038,16 @@ public StatusCode CloseSession() } } - public StatusCode Read(ReadValueId[] Ids, out DataValue[] results) + public StatusCode Read(ReadValueId[] Ids, DataValue[] results, int Count) { - results = null; + if (Ids.Length == 0) + return StatusCode.Good; + + if (Ids.Length < Count) + throw new Exception("Count cannot be larger than Ids"); + + if (results.Length < Count) + throw new Exception("Results cannot be less than Count"); try { @@ -2055,7 +2062,7 @@ public StatusCode Read(ReadValueId[] Ids, out DataValue[] results) var reqHeader = new RequestHeader() { RequestHandle = nextRequestHandle++, - Timestamp = DateTime.Now, + Timestamp = DateTime.UtcNow, AuthToken = config.AuthToken, }; @@ -2066,9 +2073,9 @@ public StatusCode Read(ReadValueId[] Ids, out DataValue[] results) // maxAge succeeded &= sendBuf.Encode((double)0); // LocaleIds - succeeded &= sendBuf.Encode((UInt32)TimestampsToReturn.Both); - succeeded &= sendBuf.Encode((UInt32)Ids.Length); - for (int i = 0; i < Ids.Length; i++) + succeeded &= sendBuf.Encode((uint)TimestampsToReturn.Both); + succeeded &= sendBuf.Encode((uint)Count); + for (int i = 0; i < Count; i++) { succeeded &= sendBuf.Encode(Ids[i]); } @@ -2121,10 +2128,12 @@ public StatusCode Read(ReadValueId[] Ids, out DataValue[] results) succeeded &= recvHandler.RecvBuf.DecodeArraySize(out uint numRecv); - results = new DataValue[numRecv]; for (int i = 0; i < numRecv && succeeded; i++) { - succeeded &= recvHandler.RecvBuf.Decode(out results[i]); + if (results[i] == null) + succeeded &= recvHandler.RecvBuf.Decode(out results[i]); + else + succeeded &= recvHandler.RecvBuf.Decode(results[i]); } if (!succeeded) @@ -2132,7 +2141,7 @@ public StatusCode Read(ReadValueId[] Ids, out DataValue[] results) return StatusCode.BadDecodingError; } - if (numRecv != Ids.Length) + if (numRecv != Count) { return StatusCode.GoodResultsMayBeIncomplete; } @@ -2146,9 +2155,22 @@ public StatusCode Read(ReadValueId[] Ids, out DataValue[] results) } } - public StatusCode Write(WriteValue[] Ids, out uint[] results) + public StatusCode Read(ReadValueId[] Ids, out DataValue[] results) { - results = null; + results = new DataValue[Ids.Length]; + return Read(Ids, results,Ids.Length); + } + + public StatusCode Write(WriteValue[] Ids, uint[] results, int Count) + { + if (Ids.Length == 0) + return StatusCode.Good; + + if (Count > Ids.Length) + throw new Exception("Count cannot be larger than Ids"); + + if (results.Length < Count) + throw new Exception("Results cannot be less than count"); try { @@ -2171,8 +2193,8 @@ public StatusCode Write(WriteValue[] Ids, out uint[] results) succeeded &= sendBuf.Encode(new NodeId(RequestCode.WriteRequest)); succeeded &= sendBuf.Encode(reqHeader); - succeeded &= sendBuf.Encode((UInt32)Ids.Length); - for (int i = 0; i < Ids.Length; i++) + succeeded &= sendBuf.Encode((UInt32)Count); + for (int i = 0; i < Count; i++) { succeeded &= sendBuf.Encode(Ids[i]); } @@ -2225,7 +2247,6 @@ public StatusCode Write(WriteValue[] Ids, out uint[] results) succeeded &= recvHandler.RecvBuf.DecodeArraySize(out uint numRecv); - results = new uint[numRecv]; for (int i = 0; i < numRecv && succeeded; i++) { succeeded &= recvHandler.RecvBuf.Decode(out results[i]); @@ -2236,7 +2257,7 @@ public StatusCode Write(WriteValue[] Ids, out uint[] results) return StatusCode.BadDecodingError; } - if (numRecv != Ids.Length) + if (numRecv != Count) { return StatusCode.GoodResultsMayBeIncomplete; } @@ -2250,6 +2271,12 @@ public StatusCode Write(WriteValue[] Ids, out uint[] results) } } + public StatusCode Write(WriteValue[] Ids, out uint[] results) + { + results = new uint[Ids.Length]; + return Write(Ids, results, Ids.Length); + } + public StatusCode AddNodes(AddNodesItem[] addNodesItems, out AddNodesResult[] results) { results = null; diff --git a/NET Core/LibUA/MemoryBufferExtensions.cs b/NET Core/LibUA/MemoryBufferExtensions.cs index a77d1c7..fd76501 100644 --- a/NET Core/LibUA/MemoryBufferExtensions.cs +++ b/NET Core/LibUA/MemoryBufferExtensions.cs @@ -1174,9 +1174,11 @@ public static bool Encode(this MemoryBuffer mem, BrowseDescription bd) return true; } - public static bool Decode(this MemoryBuffer mem, out DataValue dv) + public static bool Decode(this MemoryBuffer mem, DataValue dv) { - dv = null; + if (dv == null) + throw new Exception("Cannot decode empty dv"); + object Value = null; uint statusCode = 0; Int64 sourceTimestamp = 0; @@ -1210,10 +1212,10 @@ public static bool Decode(this MemoryBuffer mem, out DataValue dv) try { - dv = new DataValue(Value, - hasStatusCode ? (StatusCode?)statusCode : null, - hasSourceTimestamp ? (DateTime?)DateTime.FromFileTimeUtc(sourceTimestamp) : null, - hasServerTimestamp ? (DateTime?)DateTime.FromFileTimeUtc(serverTimestamp) : null); + dv.Value = Value; + dv.StatusCode = hasStatusCode ? statusCode : null; + dv.SourceTimestamp = hasSourceTimestamp ? DateTime.FromFileTimeUtc(sourceTimestamp) : null; + dv.ServerTimestamp = hasServerTimestamp ? DateTime.FromFileTimeUtc(serverTimestamp) : null; } catch { @@ -1223,6 +1225,18 @@ public static bool Decode(this MemoryBuffer mem, out DataValue dv) return true; } + public static bool Decode(this MemoryBuffer mem, out DataValue dv) + { + dv = new DataValue(); + if (!mem.Decode(dv)) + { + dv = null; + return false; + } + return true; + + } + public static int CodingSize(this MemoryBuffer mem, DataValue dv) { int sum = 0; diff --git a/NET Core/LibUA/Types.cs b/NET Core/LibUA/Types.cs index 94f7271..acbeba2 100644 --- a/NET Core/LibUA/Types.cs +++ b/NET Core/LibUA/Types.cs @@ -5739,6 +5739,7 @@ public enum DataValueSpecifierMask public struct QualifiedName { + public ushort NamespaceIndex; public string Name; @@ -5961,9 +5962,9 @@ public TPayload Value public class DataValue { - public object Value { get; protected set; } - public uint? StatusCode { get; protected set; } - public DateTime? SourceTimestamp { get; protected set; } + public object Value { get; set; } + public uint? StatusCode { get; set; } + public DateTime? SourceTimestamp { get; set; } public DateTime? ServerTimestamp { get; set; } public DataValue(object Value = null, uint? StatusCode = null, DateTime? SourceTimestamp = null, DateTime? ServerTimestamp = null)