From 88a854c8b0b8327a59a54dc95e8fb824e98a83c2 Mon Sep 17 00:00:00 2001 From: Forest Date: Thu, 9 Aug 2018 16:41:22 -0700 Subject: [PATCH] More or less require a message at base level --- Hazel.UnitTests/MessageReaderTests.cs | 63 --------------------------- Hazel/MessageReader.cs | 22 ++++++++-- Hazel/MessageWriter.cs | 31 +++++++++++++ 3 files changed, 49 insertions(+), 67 deletions(-) diff --git a/Hazel.UnitTests/MessageReaderTests.cs b/Hazel.UnitTests/MessageReaderTests.cs index 4e507d7..f0454e3 100644 --- a/Hazel.UnitTests/MessageReaderTests.cs +++ b/Hazel.UnitTests/MessageReaderTests.cs @@ -122,69 +122,6 @@ namespace Hazel.UnitTests Assert.AreEqual("NO", sub.ReadString()); } - [TestMethod] - public void TestMessage() - { - string test = "5 32 0 0 0 5 0 5 255 255 255 255 15 2 0 2 2 2 9 0 1 4 110 123 233 131 255 127 255 127"; - byte[] testValues = test.Replace("-", "").Split(' ').Select(b => byte.Parse(b)).ToArray(); - - MessageReader msg = MessageReader.Get(testValues, 0, testValues.Length); - - msg.ReadInt32(); - msg.ReadByte(); - - while (msg.Position < msg.Length) - { - var sub = msg.ReadMessage(); - Console.WriteLine($"Position: {msg.Position}/{msg.Length}"); - - if (sub.Tag == 4) // Spawn - { - uint spawnId = sub.ReadPackedUInt32(); - if (spawnId == 4) - { - int ownerId = sub.ReadPackedInt32(); - int numChild = sub.ReadPackedInt32(); - Console.WriteLine($"Spawning {spawnId} for {ownerId} with {numChild} children"); - for (int i = 0; i < numChild; ++i) - { - uint childId = sub.ReadPackedUInt32(); - var childReader = sub.ReadMessage(); - Console.WriteLine($"Child {childId} has data ({childReader.Tag}) len={childReader.Length}"); - } - } - else if (spawnId == 3) - { - int ownerId = sub.ReadPackedInt32(); - int numChild = sub.ReadPackedInt32(); - Console.WriteLine($"Spawning {spawnId} for {ownerId} with {numChild} children"); - for (int i = 0; i < numChild; ++i) - { - uint childId = sub.ReadPackedUInt32(); - var childReader = sub.ReadMessage(); - - var gameGuid = new Guid(childReader.ReadBytesAndSize()); - var numPlayers = childReader.ReadByte(); - Console.WriteLine($"Child {childId} has data: {gameGuid} NumPlayers= {numPlayers}"); - Console.WriteLine($"Remainder Data = {string.Join(" ", childReader.ReadBytes(childReader.Length - childReader.Position))}"); - - } - } - else - { - sub.Position = 0; - Console.WriteLine($"Tag: {sub.Tag}\tLength: {sub.Length}\tData = {string.Join(" ", sub.ReadBytes(sub.Length).Select(s => s.ToString()).ToArray())}"); - } - } - else - { - sub.Position = 0; - Console.WriteLine($"Tag: {sub.Tag}\tLength: {sub.Length}\tData = {string.Join(" ", sub.ReadBytes(sub.Length).Select(s => s.ToString()).ToArray())}"); - } - - } - } - [TestMethod] public void GetLittleEndian() { diff --git a/Hazel/MessageReader.cs b/Hazel/MessageReader.cs index 1cea852..7363ddd 100644 --- a/Hazel/MessageReader.cs +++ b/Hazel/MessageReader.cs @@ -29,14 +29,25 @@ namespace Hazel private int readHead; - public static MessageReader Get(byte[] buffer, int offset, int length) + public static MessageReader Get(MessageReader srcMsg) + { + var output = objectPool.GetObject(); + output.Buffer = srcMsg.Buffer; + output.Offset = srcMsg.Offset; + output.Position = srcMsg.Position; + output.Length = srcMsg.Length; + output.Tag = srcMsg.Tag; + return output; + } + + public static MessageReader Get(byte[] buffer) { var output = objectPool.GetObject(); output.Buffer = buffer; - output.Offset = offset; + output.Offset = 0; output.Position = 0; - output.Length = length; - output.Tag = output.ReadByte(); + output.Length = buffer.Length; + output.Tag = byte.MaxValue; return output; } @@ -48,6 +59,7 @@ namespace Hazel output.Offset = offset; output.Position = 0; + if (output.readHead + 3 > output.Buffer.Length) return null; output.Length = output.ReadUInt16(); output.Tag = output.ReadByte(); @@ -61,6 +73,8 @@ namespace Hazel public MessageReader ReadMessage() { var output = MessageReader.Get(this.Buffer, this.readHead); + if (output == null) return null; + this.Position += output.Length + 3; return output; } diff --git a/Hazel/MessageWriter.cs b/Hazel/MessageWriter.cs index adee5a5..29a4941 100644 --- a/Hazel/MessageWriter.cs +++ b/Hazel/MessageWriter.cs @@ -25,6 +25,37 @@ namespace Hazel this.Buffer = new byte[bufferSize]; } + public byte[] ToByteArray(bool includeHeader) + { + if (includeHeader) + { + byte[] output = new byte[this.Length]; + System.Buffer.BlockCopy(this.Buffer, 0, output, 0, this.Length); + return output; + } + else + { + switch (this.SendOption) + { + case Hazel.SendOption.Reliable: + { + byte[] output = new byte[this.Length - 3]; + System.Buffer.BlockCopy(this.Buffer, 3, output, 0, this.Length - 3); + return output; + } + case Hazel.SendOption.None: + { + byte[] output = new byte[this.Length - 1]; + System.Buffer.BlockCopy(this.Buffer, 1, output, 0, this.Length - 1); + return output; + } + + } + } + + throw new NotImplementedException(); + } + /// /// The option specifying how the message should be sent. public static MessageWriter Get(SendOption sendOption = SendOption.None) -- 2.39.5