From: Forest Date: Thu, 18 Mar 2021 01:44:56 +0000 (-0700) Subject: Reimplement debug lag features for UnityClientConnection X-Git-Tag: 1.0.0~16 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=b2cd9a8b3049a3423d6d7f92ae3173d759c16def;p=rhonda%2Fimpostor.hazel.git Reimplement debug lag features for UnityClientConnection --- diff --git a/Hazel/Udp/UnityUdpClientConnection.cs b/Hazel/Udp/UnityUdpClientConnection.cs index 8309602..422b28e 100644 --- a/Hazel/Udp/UnityUdpClientConnection.cs +++ b/Hazel/Udp/UnityUdpClientConnection.cs @@ -44,9 +44,24 @@ namespace Hazel.Udp base.ManageReliablePackets(); } + /// 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 /// The asyncronous operation's result. 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); }