From b7cc274a3a2f9381c3222196c4139d6590ae25d7 Mon Sep 17 00:00:00 2001 From: Forest Date: Wed, 4 Nov 2020 23:00:13 -0800 Subject: [PATCH] A bit of testing and pushing abstractions around. Readers and Writers now clear their buffer upon get. --- Hazel.UnitTests/MessageReaderTests.cs | 6 ++++++ .../ThreadLimitedUdpConnectionListener.cs | 5 ++++- Hazel/Hazel.csproj | 2 +- Hazel/MessageReader.cs | 11 ++++++++--- Hazel/MessageWriter.cs | 1 + Hazel/NetworkConnection.cs | 2 ++ Hazel/Udp/UdpClientConnection.cs | 15 +++++++++++---- Hazel/Udp/UdpConnection.Reliable.cs | 8 ++++---- Hazel/Udp/UdpConnection.cs | 3 ++- 9 files changed, 39 insertions(+), 14 deletions(-) diff --git a/Hazel.UnitTests/MessageReaderTests.cs b/Hazel.UnitTests/MessageReaderTests.cs index ee3b427..ef4c208 100644 --- a/Hazel.UnitTests/MessageReaderTests.cs +++ b/Hazel.UnitTests/MessageReaderTests.cs @@ -237,6 +237,9 @@ namespace Hazel.UnitTests Assert.AreEqual(65534, reader.ReadInt32()); // Content var sub = reader.ReadMessageAsNewBuffer(); + Assert.AreEqual(0, sub.Position); + Assert.AreEqual(0, sub.Offset); + Assert.AreEqual(3, sub.Length); Assert.AreEqual(2, sub.Tag); Assert.AreEqual("HO", sub.ReadString()); @@ -244,6 +247,9 @@ namespace Hazel.UnitTests sub.Recycle(); sub = reader.ReadMessageAsNewBuffer(); + Assert.AreEqual(0, sub.Position); + Assert.AreEqual(0, sub.Offset); + Assert.AreEqual(0, sub.Length); Assert.AreEqual(232, sub.Tag); sub.Recycle(); diff --git a/Hazel/FewerThreads/ThreadLimitedUdpConnectionListener.cs b/Hazel/FewerThreads/ThreadLimitedUdpConnectionListener.cs index 9e227fb..3d59106 100644 --- a/Hazel/FewerThreads/ThreadLimitedUdpConnectionListener.cs +++ b/Hazel/FewerThreads/ThreadLimitedUdpConnectionListener.cs @@ -283,7 +283,7 @@ namespace Hazel.Udp.FewerThreads internal void SendDataRaw(byte[] response, EndPoint remoteEndPoint) { - this.sendQueue.Add(new SendMessageInfo() { Buffer = response, Recipient = remoteEndPoint }); + this.sendQueue.TryAdd(new SendMessageInfo() { Buffer = response, Recipient = remoteEndPoint }); } /// @@ -315,6 +315,9 @@ namespace Hazel.Udp.FewerThreads this.sendThread.Join(); this.receiveThread.Join(); this.processThreads.Join(); + + this.receiveQueue.Dispose(); + this.sendQueue.Dispose(); } public void Dispose() diff --git a/Hazel/Hazel.csproj b/Hazel/Hazel.csproj index 6ca0b0a..f6b10e6 100644 --- a/Hazel/Hazel.csproj +++ b/Hazel/Hazel.csproj @@ -31,7 +31,7 @@ portable true bin\Release\ - TRACE;HAZEL_BAG + TRACE prompt 4 diff --git a/Hazel/MessageReader.cs b/Hazel/MessageReader.cs index 43ed4f0..39ed20c 100644 --- a/Hazel/MessageReader.cs +++ b/Hazel/MessageReader.cs @@ -36,10 +36,15 @@ namespace Hazel public static MessageReader GetSized(int minSize) { var output = ReaderPool.GetObject(); + if (output.Buffer == null || output.Buffer.Length < minSize) { output.Buffer = new byte[minSize]; } + else + { + Array.Clear(output.Buffer, 0, output.Buffer.Length); + } output.Offset = 0; output.Position = 0; @@ -74,7 +79,7 @@ namespace Hazel public static MessageReader Get(MessageReader source) { - var output = GetSized(source.Buffer.Length); + var output = MessageReader.GetSized(source.Buffer.Length); System.Buffer.BlockCopy(source.Buffer, 0, output.Buffer, 0, source.Buffer.Length); output.Offset = source.Offset; @@ -129,7 +134,7 @@ namespace Hazel output.Offset += 3; output.Position = 0; - if (this.BytesRemaining < output.Length + 3) throw new InvalidDataException($"Message length is longer than message length: {output.Length + 3} of {this.BytesRemaining}"); + if (this.BytesRemaining < output.Length + 3) throw new InvalidDataException($"Message Length at Position {this.readHead} is longer than message length: {output.Length + 3} of {this.BytesRemaining}"); this.Position += output.Length + 3; return output; @@ -145,7 +150,7 @@ namespace Hazel var len = this.ReadUInt16(); var tag = this.ReadByte(); - if (this.BytesRemaining < len) throw new InvalidDataException($"Message length is longer than message length: {len} of {this.BytesRemaining}"); + if (this.BytesRemaining < len) throw new InvalidDataException($"Message Length at Position {this.readHead} is longer than message length: {len} of {this.BytesRemaining}"); var output = MessageReader.GetSized(len); diff --git a/Hazel/MessageWriter.cs b/Hazel/MessageWriter.cs index 0c94643..0cabb66 100644 --- a/Hazel/MessageWriter.cs +++ b/Hazel/MessageWriter.cs @@ -110,6 +110,7 @@ namespace Hazel public void Clear(SendOption sendOption) { + Array.Clear(this.Buffer, 0, this.Buffer.Length); this.messageStarts.Clear(); this.SendOption = sendOption; this.Buffer[0] = (byte)sendOption; diff --git a/Hazel/NetworkConnection.cs b/Hazel/NetworkConnection.cs index a76bb9c..58915b7 100644 --- a/Hazel/NetworkConnection.cs +++ b/Hazel/NetworkConnection.cs @@ -37,6 +37,8 @@ namespace Hazel /// public EndPoint RemoteEndPoint { get; protected set; } + public virtual float AveragePingMs { get; } + public long GetIP4Address() { if (IPMode == IPMode.IPv4) diff --git a/Hazel/Udp/UdpClientConnection.cs b/Hazel/Udp/UdpClientConnection.cs index 4500fe5..bb947bc 100644 --- a/Hazel/Udp/UdpClientConnection.cs +++ b/Hazel/Udp/UdpClientConnection.cs @@ -206,10 +206,17 @@ namespace Hazel.Udp protected override void SetState(ConnectionState state) { - if (state == ConnectionState.Connected) - connectWaitLock.Set(); - else - connectWaitLock.Reset(); + try + { + if (state == ConnectionState.Connected) + connectWaitLock.Set(); + else + connectWaitLock.Reset(); + } + catch (ObjectDisposedException) + { + + } } /// diff --git a/Hazel/Udp/UdpConnection.Reliable.cs b/Hazel/Udp/UdpConnection.Reliable.cs index 01ab488..8fc3d7e 100644 --- a/Hazel/Udp/UdpConnection.Reliable.cs +++ b/Hazel/Udp/UdpConnection.Reliable.cs @@ -67,7 +67,7 @@ namespace Hazel.Udp /// This returns the average ping for a one-way trip as calculated from the reliable packets that have been sent /// and acknowledged by the endpoint. /// - public float AveragePingMs = 500; + private float _pingMs = 500; /// /// The maximum times a message should be resent before marking the endpoint as disconnected. @@ -234,7 +234,7 @@ namespace Hazel.Udp this, buffer, buffer.Length, - ResendTimeout > 0 ? ResendTimeout : (int)Math.Min(AveragePingMs * this.ResendPingMultiplier, 300), + ResendTimeout > 0 ? ResendTimeout : (int)Math.Min(_pingMs * this.ResendPingMultiplier, 300), ackCallback); if (!reliableDataPacketsSent.TryAdd(id, packet)) @@ -429,7 +429,7 @@ namespace Hazel.Udp lock (PingLock) { - this.AveragePingMs = Math.Max(50, this.AveragePingMs * .7f + rt * .3f); + this._pingMs = Math.Max(50, this._pingMs * .7f + rt * .3f); } } else if (this.activePingPackets.TryRemove(id, out PingPacket pingPkt)) @@ -440,7 +440,7 @@ namespace Hazel.Udp lock (PingLock) { - this.AveragePingMs = Math.Max(50, this.AveragePingMs * .7f + rt * .3f); + this._pingMs = Math.Max(50, this._pingMs * .7f + rt * .3f); } } } diff --git a/Hazel/Udp/UdpConnection.cs b/Hazel/Udp/UdpConnection.cs index c2de019..e28118a 100644 --- a/Hazel/Udp/UdpConnection.cs +++ b/Hazel/Udp/UdpConnection.cs @@ -9,6 +9,8 @@ namespace Hazel.Udp /// public abstract partial class UdpConnection : NetworkConnection { + public override float AveragePingMs => this._pingMs; + private const int SioUdpConnectionReset = -1744830452; public static readonly byte[] EmptyDisconnectBytes = new byte[] { (byte)UdpSendOption.Disconnect }; @@ -35,7 +37,6 @@ namespace Hazel.Udp } catch { } - try { const int SIO_UDP_CONNRESET = -1744830452; -- 2.39.5