Skip to content

Commit

Permalink
Improved error logging when add arrays to messages
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-weiland committed Jun 6, 2021
1 parent 38f1ce8 commit ce677e0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
Binary file not shown.
Binary file not shown.
49 changes: 43 additions & 6 deletions RiptideNetworking/RiptideNetworking/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,15 @@ public Message Add(byte[] value)
/// <returns>The <see langword="byte"/> array that was retrieved.</returns>
public byte[] GetByteArray(int length)
{
byte[] value = new byte[length];

if (UnreadLength < length)
{
RiptideLogger.Log("ERROR", $"Message contains insufficient unread bytes ({UnreadLength}) to read type 'byte[]', array will contain default elements!");
length = UnreadLength;
}

// If there are enough unread bytes
byte[] value = new byte[length];
Array.Copy(Bytes, readPos, value, 0, length); // Copy the bytes at readPos' position to the array that will be returned
readPos += (ushort)length;
return value;
Expand Down Expand Up @@ -309,13 +313,16 @@ public bool GetBool()
/// <returns>The Message instance that the <see langword="bool"/> array was added to.</returns>
public Message Add(bool[] array, bool includeLength = true)
{
ushort byteLength = (ushort)(array.Length / 8 + (array.Length % 8 == 0 ? 0 : 1));
if (UnwrittenLength < byteLength)
throw new Exception($"Message has insufficient remaining capacity ({UnwrittenLength}) to add type 'bool[]'!");

if (includeLength)
Add((ushort)array.Length);

BitArray bits = new BitArray(array);
byte[] bytes = new byte[array.Length / 8 + (array.Length % 8 == 0 ? 0 : 1)];
bits.CopyTo(bytes, 0);
Add(bytes);
bits.CopyTo(Bytes, writePos);
writePos += byteLength;
return this;
}

Expand All @@ -330,8 +337,14 @@ public bool[] GetBoolArray()
/// <returns>The <see langword="bool"/> array that was retrieved.</returns>
public bool[] GetBoolArray(ushort length)
{
byte[] bytes = GetByteArray(length / 8 + (length % 8 == 0 ? 0 : 1));
BitArray bits = new BitArray(bytes);
ushort byteLength = (ushort)(length / 8 + (length % 8 == 0 ? 0 : 1));
if (UnreadLength < byteLength)
{
RiptideLogger.Log("ERROR", $"Message contains insufficient unread bytes ({UnreadLength}) to read type 'bool[]', array will contain default elements!");
length = (ushort)(UnreadLength / shortLength);
}

BitArray bits = new BitArray(GetByteArray(byteLength));
bool[] array = new bool[length];
for (int i = 0; i < array.Length; i++)
array[i] = bits.Get(i);
Expand Down Expand Up @@ -387,6 +400,9 @@ public Message Add(short[] array, bool includeLength = true)
if (includeLength)
Add((ushort)array.Length);

if (UnwrittenLength < array.Length * shortLength)
throw new Exception($"Message has insufficient remaining capacity ({UnwrittenLength}) to add type 'short[]'!");

for (int i = 0; i < array.Length; i++)
Add(array[i]);

Expand Down Expand Up @@ -485,6 +501,9 @@ public Message Add(ushort[] array, bool includeLength = true)
if (includeLength)
Add((ushort)array.Length);

if (UnwrittenLength < array.Length * shortLength)
throw new Exception($"Message has insufficient remaining capacity ({UnwrittenLength}) to add type 'ushort[]'!");

for (int i = 0; i < array.Length; i++)
Add(array[i]);

Expand Down Expand Up @@ -566,6 +585,9 @@ public Message Add(int[] array, bool includeLength = true)
if (includeLength)
Add((ushort)array.Length);

if (UnwrittenLength < array.Length * intLength)
throw new Exception($"Message has insufficient remaining capacity ({UnwrittenLength}) to add type 'int[]'!");

for (int i = 0; i < array.Length; i++)
Add(array[i]);

Expand Down Expand Up @@ -647,6 +669,9 @@ public Message Add(uint[] array, bool includeLength = true)
if (includeLength)
Add((ushort)array.Length);

if (UnwrittenLength < array.Length * intLength)
throw new Exception($"Message has insufficient remaining capacity ({UnwrittenLength}) to add type 'uint[]'!");

for (int i = 0; i < array.Length; i++)
Add(array[i]);

Expand Down Expand Up @@ -732,6 +757,9 @@ public Message Add(long[] array, bool includeLength = true)
if (includeLength)
Add((ushort)array.Length);

if (UnwrittenLength < array.Length * longLength)
throw new Exception($"Message has insufficient remaining capacity ({UnwrittenLength}) to add type 'long[]'!");

for (int i = 0; i < array.Length; i++)
Add(array[i]);

Expand Down Expand Up @@ -817,6 +845,9 @@ public Message Add(ulong[] array, bool includeLength = true)
if (includeLength)
Add((ushort)array.Length);

if (UnwrittenLength < array.Length * longLength)
throw new Exception($"Message has insufficient remaining capacity ({UnwrittenLength}) to add type 'ulong[]'!");

for (int i = 0; i < array.Length; i++)
Add(array[i]);

Expand Down Expand Up @@ -898,6 +929,9 @@ public Message Add(float[] array, bool includeLength = true)
if (includeLength)
Add((ushort)array.Length);

if (UnwrittenLength < array.Length * floatLength)
throw new Exception($"Message has insufficient remaining capacity ({UnwrittenLength}) to add type 'float[]'!");

for (int i = 0; i < array.Length; i++)
Add(array[i]);

Expand Down Expand Up @@ -983,6 +1017,9 @@ public Message Add(double[] array, bool includeLength = true)
if (includeLength)
Add((ushort)array.Length);

if (UnwrittenLength < array.Length * doubleLength)
throw new Exception($"Message has insufficient remaining capacity ({UnwrittenLength}) to add type 'double[]'!");

for (int i = 0; i < array.Length; i++)
Add(array[i]);

Expand Down

0 comments on commit ce677e0

Please sign in to comment.