]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
Remove a test that didn't belong in this project, work towards separating hello from...
authorForest <chocozilla@gmail.com>
Wed, 28 Nov 2018 22:39:20 +0000 (14:39 -0800)
committerForest <chocozilla@gmail.com>
Wed, 28 Nov 2018 22:40:17 +0000 (14:40 -0800)
Hazel.UnitTests/MessageReaderTests.cs
Hazel/Udp/SendOptionInternal.cs
Hazel/Udp/UdpClientConnection.cs
Hazel/Udp/UdpConnection.KeepAlive.cs
Hazel/Udp/UdpConnection.Reliable.cs
Hazel/Udp/UdpConnection.cs

index fbcd9ec2e5b702019364ae44ead9449418aa4fa7..9655ba5a8ea37f4c20af6b6e58baf6ace069ec43 100644 (file)
@@ -127,77 +127,5 @@ namespace Hazel.UnitTests
         {
             Assert.IsTrue(MessageWriter.IsLittleEndian());
         }
-
-        // [TestMethod]
-        public void Test()
-        {
-            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 164ab83b211872667a1ec902e93ee2c570c39c6e..397334f144a010a6270f575d35850eb1d022db07 100644 (file)
@@ -16,6 +16,11 @@ namespace Hazel.Udp
         /// </summary>
         Hello = 8,
 
+        /// <summary>
+        /// A single byte of continued existence
+        /// </summary>
+        Ping = 12,
+
         /// <summary>
         ///     Message for discontinuing communication.
         /// </summary>
@@ -29,6 +34,6 @@ namespace Hazel.Udp
         /// <summary>
         ///     Message that is part of a larger, fragmented message.
         /// </summary>
-        Fragment = 11
+        Fragment = 11,
     }
 }
index 0fab2717fc27fb382a385c700cdc76cf944245f8..3976af6805baedabeca5fe2f6313ec4bede88da2 100644 (file)
@@ -61,6 +61,16 @@ namespace Hazel.Udp
         /// <inheritdoc />
         protected override void WriteBytesToConnection(byte[] bytes, int length)
         {
+            if (TestLagMs > 0)
+            {
+                ThreadPool.QueueUserWorkItem(a => { Thread.Sleep(this.TestLagMs); WriteBytesToConnectionReal(bytes, length); });
+            }
+
+            WriteBytesToConnectionReal(bytes, length);
+        }
+
+        private void WriteBytesToConnectionReal(byte[] bytes, int length)
+        { 
             InvokeDataSentRaw(bytes, length);
 
             lock (stateLock)
index 1c0504989f563a81f806e76874ac17a673ee9010..57beecb1b341c85f1fd44fe399b575d72f4d4534 100644 (file)
@@ -49,12 +49,7 @@ namespace Hazel.Udp
         ///     Lock for keep alive timer.
         /// </summary>
         Object keepAliveTimerLock = new Object();
-
-        /// <summary>
-        ///     Has the keep alive timer been disposed already?
-        /// </summary>
-        bool keepAliveTimerDisposed;
-
+        
         /// <summary>
         ///     Starts the keepalive timer.
         /// </summary>
@@ -67,7 +62,7 @@ namespace Hazel.Udp
                     {
                         try
                         {
-                            SendHello(null, null);
+                            ReliableSend((byte)UdpSendOption.Hello); // TODO: Change to ping after server can handle it, before clients update
                             Trace.WriteLine("Keepalive packet sent.");
                         }
                         catch
@@ -89,7 +84,9 @@ namespace Hazel.Udp
         void ResetKeepAliveTimer()
         {
             lock (keepAliveTimerLock)
+            {
                 keepAliveTimer.Change(keepAliveInterval, keepAliveInterval);
+            }
         }
 
         /// <summary>
@@ -97,11 +94,13 @@ namespace Hazel.Udp
         /// </summary>
         void DisposeKeepAliveTimer()
         {
-            lock(keepAliveTimerLock)
+            lock (keepAliveTimerLock)
             {
-                if (!keepAliveTimerDisposed)
+                if (keepAliveTimer != null)
+                {
                     keepAliveTimer.Dispose();
-                keepAliveTimerDisposed = true;
+                    keepAliveTimer = null;
+                }
             }
         }
     }
index 81663b6827ebb4ac776b19969918d6b98318394a..7f802ec4c31ba08d99ec38bd60dbdc936357d3f0 100644 (file)
@@ -269,6 +269,20 @@ namespace Hazel.Udp
             Statistics.LogReliableSend(length, bytes.Length);
         }
 
+        void ReliableSend(byte sendOption)
+        {
+            byte[] bytes = new byte[3];
+            bytes[0] = sendOption;
+
+            //Add reliable ID
+            AttachReliableID(bytes, 1, bytes.Length, null);
+
+            //Write to connection
+            WriteBytesToConnection(bytes, bytes.Length);
+
+            Statistics.LogReliableSend(0, bytes.Length);
+        }
+
         /// <summary>
         ///     Handles a reliable message being received and invokes the data event.
         /// </summary>
index ccfde7c4b0e65c5645992fcb10bf1351d4622298..16a85d950eb1d014ba11ca3e9e623195c80af2c7 100644 (file)
@@ -135,7 +135,7 @@ namespace Hazel.Udp
         {
             switch (sendOption)
             {
-                //Handle reliable header and hellos
+                case (byte)UdpSendOption.Ping:
                 case (byte)SendOption.Reliable:
                 case (byte)UdpSendOption.Hello:
                     ReliableSend(sendOption, data, ackCallback);
@@ -172,7 +172,8 @@ namespace Hazel.Udp
                     AcknowledgementMessageReceive(buffer);
                     break;
 
-                //We need to acknowledge hello messages but dont want to invoke any events!
+                //We need to acknowledge hello and ping messages but dont want to invoke any events!
+                case (byte)UdpSendOption.Ping:
                 case (byte)UdpSendOption.Hello:
                     ushort id;
                     ProcessReliableReceive(buffer, 1, out id);