]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
Apparently I have made a lot of changes that were not committed. But I fixed a bug...
authorForest <chocozilla@gmail.com>
Wed, 3 Oct 2018 19:54:16 +0000 (12:54 -0700)
committerForest <chocozilla@gmail.com>
Wed, 3 Oct 2018 19:54:16 +0000 (12:54 -0700)
Hazel.UnitTests/BroadcastTests.cs
Hazel.UnitTests/MessageReaderTests.cs
Hazel.UnitTests/MessageWriterTests.cs
Hazel/MessageWriter.cs
Hazel/Udp/UdpBroadcastListener.cs
Hazel/Udp/UdpConnection.KeepAlive.cs
Hazel/Udp/UdpConnection.Reliable.cs
Hazel/Udp/UdpConnection.cs
Hazel/Udp/UdpConnectionListener.cs

index d9164463f7e1b2301ff45a79aa82002a178290c0..53b313917e41285713e656e95e41bfd04d438a5a 100644 (file)
@@ -15,8 +15,8 @@ namespace Hazel.UnitTests
         {
             const string TestData = "pwerowerower";
 
-            using (UdpBroadcaster caster = new UdpBroadcaster(4777))
-            using (UdpBroadcastListener listener = new UdpBroadcastListener(4777))
+            using (UdpBroadcaster caster = new UdpBroadcaster(47777))
+            using (UdpBroadcastListener listener = new UdpBroadcastListener(47777))
             {
                 listener.StartListen();
 
index f0454e314ad47fd193c6ae616e9551da55021814..56894e2d3763d320697b8fd2731c148e0766f745 100644 (file)
@@ -131,10 +131,73 @@ namespace Hazel.UnitTests
         [TestMethod]
         public void Test()
         {
-            sbyte s = -1;
-            Assert.AreEqual(255, (byte)s);
-            byte b = 255;
-            Assert.AreEqual(-1, (sbyte)b);
+            string dataStr = "4 0 5 32 0 0 0 6 0 1 5";
+            byte[] data = dataStr.Split(' ').Select(b => byte.Parse(b)).ToArray();
+            MessageReader readerParent1 = MessageReader.Get(data);
+            while (readerParent1.Position < readerParent1.Length)
+            {
+                var readerParent = readerParent1.ReadMessage(); // Loop of InnerNetClient
+                while (readerParent.Position < readerParent.Length)
+                {
+
+                    Console.WriteLine($"{readerParent.Tag} = {readerParent.Length}");
+                    switch (readerParent.Tag)
+                    {
+                        case 1:
+                            {
+                                int gameIdConfirm = readerParent.ReadInt32();
+                                break;
+                            }
+                        case 5:
+                            {
+                                int gameIdConfirm = readerParent.ReadInt32();
+                                HandleGameData(readerParent);
+                                break;
+                            }
+                        case 6:
+                            {
+                                int gameIdConfirm = readerParent.ReadInt32();
+                                int targetId = readerParent.ReadPackedInt32(); // Skip target id
+                                HandleGameData(readerParent);
+                            }
+                            break;
+                    }
+                }
+            }
+        }
+
+        private static void HandleGameData(MessageReader readerParent)
+        {
+            while (readerParent.Position < readerParent.Length)
+            {
+                var reader = readerParent.ReadMessage();
+
+                switch (reader.Tag)
+                {
+                    case 4:
+                        {
+                            Console.WriteLine($"\t{reader.Tag} = SpawnId: {reader.ReadPackedUInt32()}");
+                            Console.WriteLine($"\t{reader.Tag} = OwnerId: {reader.ReadPackedInt32()}");
+                            Console.WriteLine($"\t{reader.Tag} = Flags: {reader.ReadByte()}");
+                            var numChildren = reader.ReadPackedInt32();
+                            Console.WriteLine($"\t{reader.Tag} = NumChildren: {numChildren}");
+                            for (int i = 0; i < numChildren; ++i)
+                            {
+                                Console.WriteLine($"\t{reader.Tag} = NetId: {reader.ReadPackedUInt32()}");
+                                var datam = reader.ReadMessage();
+                                Console.WriteLine($"\t{reader.Tag} = Data: {string.Join(" ", datam.ReadBytes(datam.Length))}");
+                            }
+                            break;
+                        }
+                    default:
+                        {
+                            Console.WriteLine($"\t{reader.Tag} = {string.Join(" ", reader.ReadBytes(reader.Length))}");
+                        }
+                        break;
+                }
+
+                Console.WriteLine();
+            }
         }
     }
 }
\ No newline at end of file
index b72e0dac6fe9021b719ceb55c622f00bd65a75e0..c4096be3301807957fc0650cb7b7df618f478928 100644 (file)
@@ -7,6 +7,44 @@ namespace Hazel.UnitTests
     [TestClass]
     public class MessageWriterTests
     {
+
+        [TestMethod]
+        public void CancelMessages()
+        {
+            var msg = new MessageWriter(128);
+
+            msg.StartMessage(1);
+            msg.Write(32);
+
+            msg.StartMessage(2);
+            msg.Write(2);
+            msg.CancelMessage();
+
+            Assert.AreEqual(7, msg.Length);
+            Assert.IsFalse(msg.HasBytes(7));
+
+            msg.CancelMessage();
+
+            Assert.AreEqual(0, msg.Length);
+            Assert.IsFalse(msg.HasBytes(1));
+        }
+
+        [TestMethod]
+        public void HasBytes()
+        {
+            var msg = new MessageWriter(128);
+
+            msg.StartMessage(1);
+            msg.Write(32);
+
+            msg.StartMessage(2);
+            msg.Write(2);
+            msg.EndMessage();
+
+            // Assert.AreEqual(7, msg.Length);
+            Assert.IsTrue(msg.HasBytes(7));
+        }
+
         [TestMethod]
         public void WriteProperInt()
         {
index 29a49415abafa6f9bf851ee52d7534656f24328c..d9ad29851c588d96e1fe3c1c3fe12ceecbbb5210 100644 (file)
@@ -97,6 +97,7 @@ namespace Hazel
         public void CancelMessage()
         {
             this.Position = this.messageStarts.Pop();
+            this.Length = this.Position;
         }
 
         public void Clear(SendOption sendOption)
index a9b182866393419ee0116f191cbf1ff16dbc49d2..530433257104d6ac39e83eb9ff5558e587b9ef22 100644 (file)
@@ -42,6 +42,8 @@ namespace Hazel.Udp
 
         private List<BroadcastPacket> packets = new List<BroadcastPacket>();
 
+        public bool Running { get; private set; }
+
         ///
         public UdpBroadcastListener(int port)
         {
@@ -53,7 +55,8 @@ namespace Hazel.Udp
         ///
         public void StartListen()
         {
-            if (this.socket == null) return;
+            if (this.Running) return;
+            this.Running = true;
             
             try
             {
@@ -70,9 +73,10 @@ namespace Hazel.Udp
             }
         }
 
-        ///
-        public void HandleData(IAsyncResult result)
+        private void HandleData(IAsyncResult result)
         {
+            this.Running = false;
+
             int numBytes;
             EndPoint endpt = new IPEndPoint(IPAddress.Any, 0);
             try
@@ -85,8 +89,12 @@ namespace Hazel.Udp
                 return;
             }
 
-            if (numBytes < 2) return;
-            if (buffer[0] != 4 || buffer[1] != 2) return;
+            if (numBytes < 2
+                || buffer[0] != 4 || buffer[1] != 2)
+            {
+                this.StartListen();
+                return;
+            }
 
             IPEndPoint ipEnd = (IPEndPoint)endpt;
             string data = ASCIIEncoding.ASCII.GetString(buffer, 2, numBytes - 2);
index f1d6f3c68d88f0da0957769cbd4a07e2d9ce9ce1..1c0504989f563a81f806e76874ac17a673ee9010 100644 (file)
@@ -65,8 +65,16 @@ namespace Hazel.Udp
                 keepAliveTimer = new Timer(
                     (o) =>
                     {
-                        Trace.WriteLine("Keepalive packet sent.");
-                        SendHello(null, null);
+                        try
+                        {
+                            SendHello(null, null);
+                            Trace.WriteLine("Keepalive packet sent.");
+                        }
+                        catch
+                        {
+                            Trace.WriteLine("Keepalive packet failed to send.");
+                            DisposeKeepAliveTimer();
+                        }
                     },
                     null,
                     keepAliveInterval,
index 5b8758d554212fa85e9252e643ecdb91f7230ebd..f262f7de03885865cb2494818f006fef51169beb 100644 (file)
@@ -249,6 +249,9 @@ namespace Hazel.Udp
         /// <param name="ackCallback">The callback to make once the packet has been acknowledged.</param>
         void ReliableSend(byte sendOption, byte[] data, int offset, int length, Action ackCallback = null)
         {
+            //Inform keepalive not to send for a while
+            ResetKeepAliveTimer();
+
             byte[] bytes = new byte[length + 3];
 
             //Add message type
index d18c03e7669743cfb092b90bc8fe43650b80c8f7..dfa00eceea4e30fc2c2f67bdfab165db8d607455 100644 (file)
@@ -45,12 +45,11 @@ namespace Hazel.Udp
             byte[] buffer = new byte[msg.Length];
             Buffer.BlockCopy(msg.Buffer, 0, buffer, 0, msg.Length);
 
-            //Inform keepalive not to send for a while
-            ResetKeepAliveTimer();
-
             switch (msg.SendOption)
             {
                 case SendOption.Reliable:
+                    // Inform keepalive not to send for a while
+                    ResetKeepAliveTimer();
                     AttachReliableID(buffer, 1, buffer.Length);
                     WriteBytesToConnection(buffer, buffer.Length);
                     Statistics.LogReliableSend(buffer.Length - 3, buffer.Length);
@@ -106,10 +105,6 @@ namespace Hazel.Udp
             if (State != ConnectionState.Connected)
                 throw new InvalidOperationException("Could not send data as this Connection is not connected. Did you disconnect?");
 
-
-            //Inform keepalive not to send for a while
-            ResetKeepAliveTimer();
-
             switch (sendOption)
             {
                 //Handle reliable header and hellos
@@ -138,9 +133,6 @@ namespace Hazel.Udp
         /// <returns>The bytes that should actually be sent.</returns>
         protected void HandleSend(byte[] data, byte sendOption, Action ackCallback = null)
         {
-            //Inform keepalive not to send for a while
-            ResetKeepAliveTimer();
-
             switch (sendOption)
             {
                 //Handle reliable header and hellos
@@ -167,10 +159,7 @@ namespace Hazel.Udp
         protected internal void HandleReceive(byte[] buffer)
         {
             InvokeDataReceivedRaw(buffer);
-
-            //Inform keepalive not to send for a while
-            ResetKeepAliveTimer();
-            
+                        
             switch (buffer[0])
             {
                 //Handle reliable receives
index 26d95eea3d612b10af5c98d4d141744935cfcf20..2fcd09d30408a5bc116cc77eb68d4ff890932e1c 100644 (file)
@@ -257,6 +257,23 @@ namespace Hazel.Udp
         /// <inheritdoc />
         protected override void Dispose(bool disposing)
         {
+            lock (connections)
+            {
+                foreach (var kvp in this.connections)
+                {
+                    if (kvp.Value.State == ConnectionState.Connected)
+                    {
+                        try
+                        {
+                            kvp.Value.SendDisconnect();
+                        }
+                        catch { }
+                    }
+                }
+
+                connections.Clear();
+            }
+
             if (listener != null)
             {
                 listener.Close();