]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
Reimplement debug lag features for UnityClientConnection
authorForest <forest@innersloth.com>
Thu, 18 Mar 2021 01:44:56 +0000 (18:44 -0700)
committerForest <forest@innersloth.com>
Thu, 18 Mar 2021 01:44:56 +0000 (18:44 -0700)
Hazel/Udp/UnityUdpClientConnection.cs

index 83096020d4afd2b7325c267a03ee9a06a1ad4d25..422b28e3484f6f1dfedd1d3919024113c28de9f7 100644 (file)
@@ -44,9 +44,24 @@ namespace Hazel.Udp
             base.ManageReliablePackets();
         }
 
+
         /// <inheritdoc />
         protected override void WriteBytesToConnection(byte[] bytes, int length)
         {
+#if DEBUG
+            if (TestLagMs > 0)
+            {
+                ThreadPool.QueueUserWorkItem(a => { Thread.Sleep(this.TestLagMs); WriteBytesToConnectionReal(bytes, length); });
+            }
+            else
+#endif
+            {
+                WriteBytesToConnectionReal(bytes, length);
+            }
+        }
+
+        private void WriteBytesToConnectionReal(byte[] bytes, int length)
+        {
             try
             {
                 socket.BeginSendTo(
@@ -194,6 +209,13 @@ namespace Hazel.Udp
         /// <param name="result">The asyncronous operation's result.</param>
         void ReadCallback(IAsyncResult result)
         {
+#if DEBUG
+            if (this.TestLagMs > 0)
+            {
+                Thread.Sleep(this.TestLagMs);
+            }
+#endif
+
             var msg = (MessageReader)result.AsyncState;
 
             try
@@ -236,6 +258,16 @@ namespace Hazel.Udp
                 return;
             }
 
+#if DEBUG
+            if (this.TestDropRate > 0)
+            {
+                if ((this.testDropCount++ % this.TestDropRate) == 0)
+                {
+                    return;
+                }
+            }
+#endif
+
             HandleReceive(msg, msg.Length);
         }