Skip to content

Commit 6837391

Browse files
committed
Fixed various messageChunker issues and added more helpers to verify chunks
1 parent 64b3bed commit 6837391

File tree

1 file changed

+80
-6
lines changed

1 file changed

+80
-6
lines changed

MLAPI/NetworkingManagerComponents/MessageChunker.cs

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.IO;
43

54
namespace MLAPI.NetworkingManagerComponents
65
{
@@ -37,21 +36,67 @@ public static List<byte[]> GetChunkedMessage(ref byte[] message, int chunkSize)
3736
return chunks;
3837
}
3938

39+
public static bool HasMissingParts(ref List<byte[]> chunks, uint expectedChunksCount)
40+
{
41+
if (chunks.Count < expectedChunksCount)
42+
return true;
43+
44+
HashSet<uint> chunkIndexes = new HashSet<uint>();
45+
uint duplicateCount = 0;
46+
for (int i = 0; i < chunks.Count; i++)
47+
{
48+
uint chunkIndex = BitConverter.ToUInt32(chunks[i], 0);
49+
if (chunkIndexes.Contains(chunkIndex))
50+
duplicateCount++;
51+
else
52+
chunkIndexes.Add(chunkIndex);
53+
}
54+
return chunks.Count - duplicateCount != expectedChunksCount;
55+
}
56+
57+
public static bool IsOrdered(ref List<byte[]> chunks)
58+
{
59+
uint lastChunkIndex = 0;
60+
for (int i = 0; i < chunks.Count; i++)
61+
{
62+
uint chunkIndex = BitConverter.ToUInt32(chunks[i], 0);
63+
//This can't be right?
64+
if (chunkIndex <= lastChunkIndex)
65+
lastChunkIndex++;
66+
else
67+
return false;
68+
}
69+
return true;
70+
}
71+
72+
public static bool HasDuplicates(ref List<byte[]> chunks, uint expectedChunksCount)
73+
{
74+
if (chunks.Count > expectedChunksCount)
75+
return true;
76+
77+
HashSet<uint> chunkIndexes = new HashSet<uint>();
78+
for (int i = 0; i < chunks.Count; i++)
79+
{
80+
uint chunkIndex = BitConverter.ToUInt32(chunks[i], 0);
81+
if (chunkIndexes.Contains(chunkIndex))
82+
return true;
83+
else
84+
chunkIndexes.Add(chunkIndex);
85+
}
86+
return false;
87+
}
88+
89+
4090
public static byte[] GetMessageOrdered(ref List<byte[]> chunks, int chunkSize = -1)
4191
{
4292
if (chunks.Count == 0)
4393
return new byte[0];
4494
if (chunkSize == -1)
4595
chunkSize = chunks[0].Length - 4;
4696

47-
uint lastIndex = 0;
4897
uint messageSize = 0;
4998
for (int i = 0; i < chunks.Count; i++)
5099
{
51-
uint chunkIndex = BitConverter.ToUInt32(chunks[i], 0);
52-
if (chunkIndex <= lastIndex)
53-
throw new ArgumentException("Chunks not in order");
54-
lastIndex = chunkIndex;
55100
messageSize += Convert.ToUInt32(chunks[i].Length - 4);
56101
}
57102
byte[] message = new byte[messageSize];
@@ -61,5 +106,34 @@ public static byte[] GetMessageOrdered(ref List<byte[]> chunks, int chunkSize =
61106
}
62107
return message;
63108
}
109+
110+
public static byte[] GetMessageUnordered(ref List<byte[]> chunks, int chunkSize = -1)
111+
{
112+
if (chunks.Count == 0)
113+
return new byte[0];
114+
if (chunkSize == -1)
115+
chunkSize = chunks[0].Length - 4;
116+
117+
uint messageSize = 0;
118+
for (int i = 0; i < chunks.Count; i++)
119+
{
120+
messageSize += Convert.ToUInt32(chunks[i].Length - 4);
121+
}
122+
byte[] message = new byte[messageSize];
123+
uint nextIndex = 0;
124+
//Loop as many times as there are chunks.
125+
for (int i = 0; i < chunks.Count; i++)
126+
{
127+
//For each chunk, find the right chunk
128+
for (int j = 0; j < chunks.Count; j++)
129+
{
130+
if(BitConverter.ToUInt32(chunks[j], 0) == nextIndex)
131+
{
132+
Array.Copy(chunks[j], 3, message, nextIndex * chunkSize, chunkSize);
133+
}
134+
}
135+
}
136+
return message;
137+
}
64138
}
65139
}

0 commit comments

Comments
 (0)