Fixed various messageChunker issues and added more helpers to verify chunks
This commit is contained in:
parent
64b3bedc62
commit
6837391ace
@ -1,6 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
|
||||||
|
|
||||||
namespace MLAPI.NetworkingManagerComponents
|
namespace MLAPI.NetworkingManagerComponents
|
||||||
{
|
{
|
||||||
@ -37,6 +36,57 @@ namespace MLAPI.NetworkingManagerComponents
|
|||||||
return chunks;
|
return chunks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static bool HasMissingParts(ref List<byte[]> chunks, uint expectedChunksCount)
|
||||||
|
{
|
||||||
|
if (chunks.Count < expectedChunksCount)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
HashSet<uint> chunkIndexes = new HashSet<uint>();
|
||||||
|
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<byte[]> 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<byte[]> chunks, uint expectedChunksCount)
|
||||||
|
{
|
||||||
|
if (chunks.Count > expectedChunksCount)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
HashSet<uint> chunkIndexes = new HashSet<uint>();
|
||||||
|
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<byte[]> chunks, int chunkSize = -1)
|
public static byte[] GetMessageOrdered(ref List<byte[]> chunks, int chunkSize = -1)
|
||||||
{
|
{
|
||||||
if (chunks.Count == 0)
|
if (chunks.Count == 0)
|
||||||
@ -44,14 +94,9 @@ namespace MLAPI.NetworkingManagerComponents
|
|||||||
if (chunkSize == -1)
|
if (chunkSize == -1)
|
||||||
chunkSize = chunks[0].Length - 4;
|
chunkSize = chunks[0].Length - 4;
|
||||||
|
|
||||||
uint lastIndex = 0;
|
|
||||||
uint messageSize = 0;
|
uint messageSize = 0;
|
||||||
for (int i = 0; i < chunks.Count; i++)
|
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);
|
messageSize += Convert.ToUInt32(chunks[i].Length - 4);
|
||||||
}
|
}
|
||||||
byte[] message = new byte[messageSize];
|
byte[] message = new byte[messageSize];
|
||||||
@ -61,5 +106,34 @@ namespace MLAPI.NetworkingManagerComponents
|
|||||||
}
|
}
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static byte[] GetMessageUnordered(ref List<byte[]> 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user