From 64b3bedc623f9e4c500c7ae13eb8b200379576ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albin=20Cor=C3=A9n?= Date: Thu, 22 Mar 2018 09:55:26 +0100 Subject: [PATCH] Added helper class to chunk byte arrays --- MLAPI/MLAPI.csproj | 1 + .../MessageChunker.cs | 65 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 MLAPI/NetworkingManagerComponents/MessageChunker.cs diff --git a/MLAPI/MLAPI.csproj b/MLAPI/MLAPI.csproj index 3706f5e..44c7b80 100644 --- a/MLAPI/MLAPI.csproj +++ b/MLAPI/MLAPI.csproj @@ -76,6 +76,7 @@ + \ No newline at end of file diff --git a/MLAPI/NetworkingManagerComponents/MessageChunker.cs b/MLAPI/NetworkingManagerComponents/MessageChunker.cs new file mode 100644 index 0000000..ba22aca --- /dev/null +++ b/MLAPI/NetworkingManagerComponents/MessageChunker.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace MLAPI.NetworkingManagerComponents +{ + public static class MessageChunker + { + public static List GetChunkedMessage(ref byte[] message, int chunkSize) + { + List chunks = new List((int)Math.Ceiling((double)message.Length / chunkSize)); + //The extra 4 bytes is the chunkIndex + byte[] chunk = new byte[chunkSize + 4]; + + for (int i = 0; i < message.Length; i++) + { + uint currentChunkIndex = (uint)Math.Floor((double)i / (double)chunkSize); + int currentByteIndex = i % chunkSize; + + if (currentByteIndex == 0) + { + byte[] chunkIndexBytes = BitConverter.GetBytes(currentChunkIndex); + if (BitConverter.IsLittleEndian) + Array.Reverse(chunkIndexBytes); + chunk[0] = chunkIndexBytes[0]; + chunk[1] = chunkIndexBytes[1]; + chunk[2] = chunkIndexBytes[2]; + chunk[3] = chunkIndexBytes[3]; + } + chunk[4 + currentByteIndex] = message[i]; + if(currentByteIndex == (chunkSize -1) || i == (message.Length -1)) + { + //This is the last byte in the chunk + chunks.Add(chunk); + } + } + return chunks; + } + + public static byte[] GetMessageOrdered(ref List chunks, int chunkSize = -1) + { + if (chunks.Count == 0) + return new byte[0]; + 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]; + for (int i = 0; i < chunks.Count; i++) + { + Array.Copy(chunks[i], 3, message, i * chunkSize, chunkSize); + } + return message; + } + } +}