]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
A bit of testing and pushing abstractions around. Readers and Writers now clear their...
authorForest <forest@innersloth.com>
Thu, 5 Nov 2020 07:00:13 +0000 (23:00 -0800)
committerForest <forest@innersloth.com>
Thu, 5 Nov 2020 07:01:13 +0000 (23:01 -0800)
Hazel.UnitTests/MessageReaderTests.cs
Hazel/FewerThreads/ThreadLimitedUdpConnectionListener.cs
Hazel/Hazel.csproj
Hazel/MessageReader.cs
Hazel/MessageWriter.cs
Hazel/NetworkConnection.cs
Hazel/Udp/UdpClientConnection.cs
Hazel/Udp/UdpConnection.Reliable.cs
Hazel/Udp/UdpConnection.cs

index ee3b42730510db5a06ca08840d35e361597cb8fe..ef4c20838f55239b2e5fee45b349f7741031aa21 100644 (file)
@@ -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();
index 9e227fbc6cc2852f1070beda3a6a9da978fe61a0..3d59106045aaab3e95fddfe22e4a39929ad14603 100644 (file)
@@ -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 });
         }
 
         /// <summary>
@@ -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()
index 6ca0b0a10cbf41a98fe785be9268c591710c6dcd..f6b10e6b4d382eab36b17ef69289883b4fd56732 100644 (file)
@@ -31,7 +31,7 @@
     <DebugType>portable</DebugType>
     <Optimize>true</Optimize>
     <OutputPath>bin\Release\</OutputPath>
-    <DefineConstants>TRACE;HAZEL_BAG</DefineConstants>
+    <DefineConstants>TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
     <DocumentationFile>
index 43ed4f09d2edc17093f5989675784dd36776cd5a..39ed20cf0caa546c8a88d54c9ff490e234baca76 100644 (file)
@@ -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);
 
index 0c94643a7c349e4f33538a86b7833869375a7563..0cabb66c7386ebc06f5a94a82336480b3077810e 100644 (file)
@@ -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;
index a76bb9c13c3c1c6eed6a8ac6a329bb9773db0eba..58915b73c31713e3dbece4864d68e8bc21f90255 100644 (file)
@@ -37,6 +37,8 @@ namespace Hazel
         /// </remarks>
         public EndPoint RemoteEndPoint { get; protected set; }
 
+        public virtual float AveragePingMs { get; }
+
         public long GetIP4Address()
         {
             if (IPMode == IPMode.IPv4)
index 4500fe56bbb01baa2d9a3b08444c7f4884d4754a..bb947bca4c133926d6a7ed4fa333fd4e9d2b4f07 100644 (file)
@@ -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)
+            {
+
+            }
         }
 
         /// <summary>
index 01ab48820963cb6cba7d2bcd524272d1955a1687..8fc3d7edb24fb6a4b0c1aafe1cc50144eb3d4fb9 100644 (file)
@@ -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.
         /// </remarks>
-        public float AveragePingMs = 500;
+        private float _pingMs = 500;
 
         /// <summary>
         ///     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);
                 }
             }
         }
index c2de019b8bf9c5c7575282979bb41c3210cb0f16..e28118a38526e541c4707fd3cb0441008f184e97 100644 (file)
@@ -9,6 +9,8 @@ namespace Hazel.Udp
     /// <inheritdoc />
     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;