]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
More or less require a message at base level
authorForest <chocozilla@gmail.com>
Thu, 9 Aug 2018 23:41:22 +0000 (16:41 -0700)
committerForest <chocozilla@gmail.com>
Thu, 9 Aug 2018 23:41:22 +0000 (16:41 -0700)
Hazel.UnitTests/MessageReaderTests.cs
Hazel/MessageReader.cs
Hazel/MessageWriter.cs

index 4e507d7bca1e131f6d2f9a928501541ded394e4f..f0454e314ad47fd193c6ae616e9551da55021814 100644 (file)
@@ -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()
         {
index 1cea85290725ccd56fb8dcf354dfe7807988cc59..7363dddba623276f6cbe15c2f45cf5d1fdbd5948 100644 (file)
@@ -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;
         }
index adee5a5fd48803ef4eb9ec4b3640799fe2001bba..29a49415abafa6f9bf851ee52d7534656f24328c 100644 (file)
@@ -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();
+        }
+
         ///
         /// <param name="sendOption">The option specifying how the message should be sent.</param>
         public static MessageWriter Get(SendOption sendOption = SendOption.None)