From 6837391ace9d710f814289ae17fd1aeb3aa62746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albin=20Cor=C3=A9n?= Date: Thu, 22 Mar 2018 12:57:48 +0100 Subject: [PATCH] Fixed various messageChunker issues and added more helpers to verify chunks --- .../MessageChunker.cs | 86 +++++++++++++++++-- 1 file changed, 80 insertions(+), 6 deletions(-) diff --git a/MLAPI/NetworkingManagerComponents/MessageChunker.cs b/MLAPI/NetworkingManagerComponents/MessageChunker.cs index ba22aca..60b25b0 100644 --- a/MLAPI/NetworkingManagerComponents/MessageChunker.cs +++ b/MLAPI/NetworkingManagerComponents/MessageChunker.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.IO; namespace MLAPI.NetworkingManagerComponents { @@ -37,6 +36,57 @@ namespace MLAPI.NetworkingManagerComponents return chunks; } + public static bool HasMissingParts(ref List chunks, uint expectedChunksCount) + { + if (chunks.Count < expectedChunksCount) + return true; + + HashSet chunkIndexes = new HashSet(); + uint duplicateCount = 0; + for (int i = 0; i < chunks.Count; i++) + { + uint chunkIndex = BitConverter.ToUInt32(chunks[i], 0); + if (chunkIndexes.Contains(chunkIndex)) + duplicateCount++; + else + chunkIndexes.Add(chunkIndex); + } + return chunks.Count - duplicateCount != expectedChunksCount; + } + + public static bool IsOrdered(ref List chunks) + { + uint lastChunkIndex = 0; + for (int i = 0; i < chunks.Count; i++) + { + uint chunkIndex = BitConverter.ToUInt32(chunks[i], 0); + //This can't be right? + if (chunkIndex <= lastChunkIndex) + lastChunkIndex++; + else + return false; + } + return true; + } + + public static bool HasDuplicates(ref List chunks, uint expectedChunksCount) + { + if (chunks.Count > expectedChunksCount) + return true; + + HashSet chunkIndexes = new HashSet(); + for (int i = 0; i < chunks.Count; i++) + { + uint chunkIndex = BitConverter.ToUInt32(chunks[i], 0); + if (chunkIndexes.Contains(chunkIndex)) + return true; + else + chunkIndexes.Add(chunkIndex); + } + return false; + } + + public static byte[] GetMessageOrdered(ref List chunks, int chunkSize = -1) { if (chunks.Count == 0) @@ -44,14 +94,9 @@ namespace MLAPI.NetworkingManagerComponents if (chunkSize == -1) chunkSize = chunks[0].Length - 4; - uint lastIndex = 0; uint messageSize = 0; for (int i = 0; i < chunks.Count; i++) { - uint chunkIndex = BitConverter.ToUInt32(chunks[i], 0); - if (chunkIndex <= lastIndex) - throw new ArgumentException("Chunks not in order"); - lastIndex = chunkIndex; messageSize += Convert.ToUInt32(chunks[i].Length - 4); } byte[] message = new byte[messageSize]; @@ -61,5 +106,34 @@ namespace MLAPI.NetworkingManagerComponents } return message; } + + public static byte[] GetMessageUnordered(ref List chunks, int chunkSize = -1) + { + if (chunks.Count == 0) + return new byte[0]; + if (chunkSize == -1) + chunkSize = chunks[0].Length - 4; + + uint messageSize = 0; + for (int i = 0; i < chunks.Count; i++) + { + messageSize += Convert.ToUInt32(chunks[i].Length - 4); + } + byte[] message = new byte[messageSize]; + uint nextIndex = 0; + //Loop as many times as there are chunks. + for (int i = 0; i < chunks.Count; i++) + { + //For each chunk, find the right chunk + for (int j = 0; j < chunks.Count; j++) + { + if(BitConverter.ToUInt32(chunks[j], 0) == nextIndex) + { + Array.Copy(chunks[j], 3, message, nextIndex * chunkSize, chunkSize); + } + } + } + return message; + } } }