From: Forest Date: Fri, 18 Oct 2019 19:27:31 +0000 (-0700) Subject: * Fix some issues that popped up in Hazel-Examples because of overzealous refactoring. X-Git-Tag: 1.0.0~35 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=eb9255a3adb4ee77fdb3a7f1904972dd93e89fdd;p=rhonda%2Fimpostor.hazel.git * Fix some issues that popped up in Hazel-Examples because of overzealous refactoring. * Add more data into the early AcceptConnectionCheck to benefit IP-based connection rejection. * Add a metric for message resends * Tune message resends a little more --- diff --git a/Hazel/Connection.cs b/Hazel/Connection.cs index 50daf44..2e68f73 100644 --- a/Hazel/Connection.cs +++ b/Hazel/Connection.cs @@ -196,7 +196,7 @@ namespace Hazel Action handler = DataReceived; if (handler != null) { - handler(new DataReceivedEventArgs(msg, sendOption)); + handler(new DataReceivedEventArgs(this, msg, sendOption)); } else { diff --git a/Hazel/ConnectionStatistics.cs b/Hazel/ConnectionStatistics.cs index 22a09c2..c183062 100644 --- a/Hazel/ConnectionStatistics.cs +++ b/Hazel/ConnectionStatistics.cs @@ -386,6 +386,9 @@ namespace Hazel /// long totalBytesReceived; + public int MessagesResent { get { return messagesResent; } } + int messagesResent; + /// /// Logs the sending of an unreliable data packet in the statistics. /// @@ -555,5 +558,10 @@ namespace Hazel Interlocked.Increment(ref helloMessagesReceived); Interlocked.Add(ref totalBytesReceived, totalLength); } + + internal void LogMessageResent() + { + Interlocked.Increment(ref messagesResent); + } } } diff --git a/Hazel/DataReceivedEventArgs.cs b/Hazel/DataReceivedEventArgs.cs index a063852..35609fc 100644 --- a/Hazel/DataReceivedEventArgs.cs +++ b/Hazel/DataReceivedEventArgs.cs @@ -7,6 +7,8 @@ namespace Hazel { public struct DataReceivedEventArgs { + public readonly Connection Sender; + /// /// The bytes received from the client. /// @@ -17,8 +19,9 @@ namespace Hazel /// public readonly SendOption SendOption; - public DataReceivedEventArgs(MessageReader msg, SendOption sendOption) + public DataReceivedEventArgs(Connection sender, MessageReader msg, SendOption sendOption) { + this.Sender = sender; this.Message = msg; this.SendOption = sendOption; } diff --git a/Hazel/ObjectPool.cs b/Hazel/ObjectPool.cs index 725353e..163301d 100644 --- a/Hazel/ObjectPool.cs +++ b/Hazel/ObjectPool.cs @@ -16,6 +16,7 @@ namespace Hazel public int NumberInUse { get { return this.inuse.Count; } } public int NumberNotInUse { get { return this.pool.Count; } } + public int Size { get { return this.NumberInUse + this.NumberNotInUse; } } // Available objects private readonly ConcurrentBag pool = new ConcurrentBag(); diff --git a/Hazel/Properties/AssemblyInfo.cs b/Hazel/Properties/AssemblyInfo.cs index bcdc4ad..0e00915 100644 --- a/Hazel/Properties/AssemblyInfo.cs +++ b/Hazel/Properties/AssemblyInfo.cs @@ -10,7 +10,7 @@ using System.Runtime.InteropServices; [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("Hazel")] -[assembly: AssemblyCopyright("Copyright © 2016")] +[assembly: AssemblyCopyright("Copyright © 2019")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] diff --git a/Hazel/Udp/UdpClientConnection.cs b/Hazel/Udp/UdpClientConnection.cs index c02cd31..503025d 100644 --- a/Hazel/Udp/UdpClientConnection.cs +++ b/Hazel/Udp/UdpClientConnection.cs @@ -19,6 +19,11 @@ namespace Hazel.Udp private Timer reliablePacketTimer; +#if DEBUG + public event Action DataSentRaw; + public event Action DataReceivedRaw; +#endif + /// /// Creates a new UdpClientConnection. /// @@ -62,22 +67,23 @@ namespace Hazel.Udp /// 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); } } - public event Action DataSentRaw; - public event Action DataReceivedRaw; - private void WriteBytesToConnectionReal(byte[] bytes, int length) { +#if DEBUG DataSentRaw?.Invoke(bytes, length); +#endif try { @@ -183,6 +189,13 @@ namespace Hazel.Udp /// void StartListeningForData() { +#if DEBUG + if (this.TestLagMs > 0) + { + Thread.Sleep(this.TestLagMs); + } +#endif + var msg = MessageReader.GetSized(ushort.MaxValue); try { @@ -241,11 +254,7 @@ namespace Hazel.Udp return; } - if (this.TestLagMs > 0) - { - Thread.Sleep(this.TestLagMs); - } - +#if DEBUG if (this.TestDropRate > 0) { if ((this.testDropCount++ % this.TestDropRate) == 0) @@ -255,6 +264,7 @@ namespace Hazel.Udp } DataReceivedRaw?.Invoke(msg.Buffer, msg.Length); +#endif HandleReceive(msg, msg.Length); } diff --git a/Hazel/Udp/UdpConnection.Reliable.cs b/Hazel/Udp/UdpConnection.Reliable.cs index a2a2450..303d670 100644 --- a/Hazel/Udp/UdpConnection.Reliable.cs +++ b/Hazel/Udp/UdpConnection.Reliable.cs @@ -171,10 +171,11 @@ namespace Hazel.Udp return 0; } - this.NextTimeout += (int)Math.Min(this.NextTimeout * connection.ResendPingMultiplier, 500); + this.NextTimeout += (int)Math.Min(this.NextTimeout * connection.ResendPingMultiplier, 1000); try { connection.WriteBytesToConnection(this.Data, this.Length); + connection.Statistics.LogMessageResent(); return 1; } catch (InvalidOperationException) @@ -238,7 +239,7 @@ namespace Hazel.Udp this, buffer, sendLength, - ResendTimeout > 0 ? ResendTimeout : ClampToInt(AveragePingMs * this.ResendPingMultiplier, 300, 1000), + ResendTimeout > 0 ? ResendTimeout : (int)Math.Min(AveragePingMs * this.ResendPingMultiplier, 300), ackCallback); if (!reliableDataPacketsSent.TryAdd(id, packet)) diff --git a/Hazel/Udp/UdpConnectionListener.cs b/Hazel/Udp/UdpConnectionListener.cs index 5b61885..3d67fda 100644 --- a/Hazel/Udp/UdpConnectionListener.cs +++ b/Hazel/Udp/UdpConnectionListener.cs @@ -16,7 +16,7 @@ namespace Hazel.Udp public int MinConnectionLength = 0; - public delegate bool AcceptConnectionCheck(byte[] input, out byte[] response); + public delegate bool AcceptConnectionCheck(IPEndPoint endPoint, byte[] input, out byte[] response); public AcceptConnectionCheck AcceptConnection; /// @@ -212,7 +212,7 @@ namespace Hazel.Udp if (AcceptConnection != null) { - if (!AcceptConnection(message.Buffer, out var response)) + if (!AcceptConnection((IPEndPoint)remoteEndPoint, message.Buffer, out var response)) { message.Recycle(); SendData(response, response.Length, remoteEndPoint);