Skip to content

Commit

Permalink
Cleanup, refactor Read and Write to work with ArraySegments.
Browse files Browse the repository at this point in the history
  • Loading branch information
nauful committed Mar 5, 2024
1 parent da17f96 commit e36f62c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
58 changes: 32 additions & 26 deletions NET Core/LibUA/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2038,16 +2038,17 @@ public StatusCode CloseSession()
}
}

public StatusCode Read(ReadValueId[] Ids, DataValue[] results, int Count)
public StatusCode Read(ArraySegment<ReadValueId> Ids, ArraySegment<DataValue> results)
{
if (Ids.Length == 0)
if (Ids.Count == 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");
if (Ids.Count != results.Count)
{
throw new Exception("Number of results must match number of Ids.");
}

try
{
Expand All @@ -2074,8 +2075,8 @@ public StatusCode Read(ReadValueId[] Ids, DataValue[] results, int Count)
succeeded &= sendBuf.Encode((double)0);
// LocaleIds
succeeded &= sendBuf.Encode((uint)TimestampsToReturn.Both);
succeeded &= sendBuf.Encode((uint)Count);
for (int i = 0; i < Count; i++)
succeeded &= sendBuf.Encode((uint)Ids.Count);
for (int i = 0; i < Ids.Count; i++)
{
succeeded &= sendBuf.Encode(Ids[i]);
}
Expand Down Expand Up @@ -2131,17 +2132,19 @@ public StatusCode Read(ReadValueId[] Ids, DataValue[] results, int Count)
for (int i = 0; i < numRecv && succeeded; i++)
{
if (results[i] == null)
succeeded &= recvHandler.RecvBuf.Decode(out results[i]);
else
succeeded &= recvHandler.RecvBuf.Decode(results[i]);
{
results[i] = new DataValue();
}

succeeded &= recvHandler.RecvBuf.Decode(results[i]);
}

if (!succeeded)
{
return StatusCode.BadDecodingError;
}

if (numRecv != Count)
if (numRecv != Ids.Count)
{
return StatusCode.GoodResultsMayBeIncomplete;
}
Expand All @@ -2158,19 +2161,20 @@ public StatusCode Read(ReadValueId[] Ids, DataValue[] results, int Count)
public StatusCode Read(ReadValueId[] Ids, out DataValue[] results)
{
results = new DataValue[Ids.Length];
return Read(Ids, results,Ids.Length);
return Read(Ids, results);
}

public StatusCode Write(WriteValue[] Ids, uint[] results, int Count)
public StatusCode Write(ArraySegment<WriteValue> Ids, ArraySegment<uint> results)
{
if (Ids.Length == 0)
if (Ids.Count == 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");
if (Ids.Count != results.Count)
{
throw new Exception("Number of results must match number of Ids.");
}

try
{
Expand All @@ -2193,8 +2197,8 @@ public StatusCode Write(WriteValue[] Ids, uint[] results, int Count)
succeeded &= sendBuf.Encode(new NodeId(RequestCode.WriteRequest));
succeeded &= sendBuf.Encode(reqHeader);

succeeded &= sendBuf.Encode((UInt32)Count);
for (int i = 0; i < Count; i++)
succeeded &= sendBuf.Encode((UInt32)Ids.Count);
for (int i = 0; i < Ids.Count; i++)
{
succeeded &= sendBuf.Encode(Ids[i]);
}
Expand Down Expand Up @@ -2247,17 +2251,19 @@ public StatusCode Write(WriteValue[] Ids, uint[] results, int Count)

succeeded &= recvHandler.RecvBuf.DecodeArraySize(out uint numRecv);

uint v;
for (int i = 0; i < numRecv && succeeded; i++)
{
succeeded &= recvHandler.RecvBuf.Decode(out results[i]);
succeeded &= recvHandler.RecvBuf.Decode(out v);
results[i] = v;
}

if (!succeeded)
{
return StatusCode.BadDecodingError;
}

if (numRecv != Count)
if (numRecv != Ids.Count)
{
return StatusCode.GoodResultsMayBeIncomplete;
}
Expand All @@ -2274,7 +2280,7 @@ public StatusCode Write(WriteValue[] Ids, uint[] results, int Count)
public StatusCode Write(WriteValue[] Ids, out uint[] results)
{
results = new uint[Ids.Length];
return Write(Ids, results, Ids.Length);
return Write(Ids, results);
}

public StatusCode AddNodes(AddNodesItem[] addNodesItems, out AddNodesResult[] results)
Expand Down
2 changes: 2 additions & 0 deletions NET Core/LibUA/MemoryBufferExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,9 @@ public static bool Encode(this MemoryBuffer mem, BrowseDescription bd)
public static bool Decode(this MemoryBuffer mem, DataValue dv)
{
if (dv == null)
{
throw new Exception("Cannot decode empty dv");
}

object Value = null;
uint statusCode = 0;
Expand Down
1 change: 0 additions & 1 deletion NET Core/LibUA/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5739,7 +5739,6 @@ public enum DataValueSpecifierMask

public struct QualifiedName
{

public ushort NamespaceIndex;

public string Name;
Expand Down

0 comments on commit e36f62c

Please sign in to comment.