]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
Fix some null refs and other weird bugs
authorForest <chocozilla@gmail.com>
Sun, 14 Oct 2018 19:45:47 +0000 (12:45 -0700)
committerForest <chocozilla@gmail.com>
Sun, 14 Oct 2018 19:45:47 +0000 (12:45 -0700)
Hazel.UnitTests/MessageReaderTests.cs
Hazel.UnitTests/UdpConnectionTests.cs
Hazel/Connection.cs
Hazel/NetworkConnection.cs
Hazel/Udp/UdpClientConnection.cs
Hazel/Udp/UdpConnection.Reliable.cs
Hazel/Udp/UdpServerConnection.cs

index 56894e2d3763d320697b8fd2731c148e0766f745..fbcd9ec2e5b702019364ae44ead9449418aa4fa7 100644 (file)
@@ -128,7 +128,7 @@ namespace Hazel.UnitTests
             Assert.IsTrue(MessageWriter.IsLittleEndian());
         }
 
-        [TestMethod]
+        // [TestMethod]
         public void Test()
         {
             string dataStr = "4 0 5 32 0 0 0 6 0 1 5";
index 4ac8045c6a8fecd12f5f80e4af6ead41d8e43ecd..3476653b326028dd8a0abcefb03167548dfff593 100644 (file)
@@ -114,6 +114,35 @@ namespace Hazel.UnitTests
                 connection.Connect();
             }
         }
+        
+        /// <summary>
+        ///     Tests dual mode connectivity.
+        /// </summary>
+        [TestMethod]
+        public void MixedConnectionTest()
+        {
+            using (UdpConnectionListener listener2 = new UdpConnectionListener(new NetworkEndPoint(IPAddress.IPv6Any, 4296, IPMode.IPv6)))
+            {
+                listener2.Start();
+
+                listener2.NewConnection += (sender, evt) =>
+                {
+                    Console.WriteLine("v6 connection: " + ((NetworkConnection)evt.Connection).GetIP4Address());
+                };
+
+                using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint("127.0.0.1", 4296, IPMode.IPv4)))
+                {
+                    connection.Connect();
+                    Assert.AreEqual(ConnectionState.Connected, connection.State);
+                }
+
+                using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint(IPAddress.IPv6Loopback, 4296, IPMode.IPv6)))
+                {
+                    connection.Connect();
+                    Assert.AreEqual(ConnectionState.Connected, connection.State);
+                }
+            }
+        }
 
         /// <summary>
         ///     Tests dual mode connectivity.
@@ -125,7 +154,7 @@ namespace Hazel.UnitTests
             {
                 listener.Start();
 
-                using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint(IPAddress.IPv6Loopback, 4296, IPMode.IPv6)))
+                using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint("127.0.0.1", 4296, IPMode.IPv6)))
                 {
                     connection.Connect();
                 }
index 05f451303783731557fb13b6b3b1ce09f60c6c3b..74e3a225f4203bd5226350a8f1a8785b26274980 100644 (file)
@@ -50,6 +50,8 @@ namespace Hazel
         /// </example>
         public event EventHandler<DataReceivedEventArgs> DataReceived;
 
+        public int TestLagMs = -1;
+
         public event Action<byte[], int> DataSentRaw;
         protected void InvokeDataSentRaw(byte[] data, int length)
         {
@@ -248,8 +250,7 @@ namespace Hazel
 
             //Make a copy to avoid race condition between null check and invocation
             EventHandler<DataReceivedEventArgs> handler = DataReceived;
-            if (handler != null)
-                handler(this, args);
+            if (handler != null) handler.Invoke(this, args);
         }
 
         /// <summary>
@@ -268,8 +269,7 @@ namespace Hazel
 
             //Make a copy to avoid race condition between null check and invocation
             EventHandler<DisconnectedEventArgs> handler = Disconnected;
-            if (handler != null)
-                handler(this, args);
+            if (handler != null) handler.Invoke(this, args);
         }
 
         /// <summary>
index 21f2f45b3568b1136b6579100bb0e8cd1439d6cf..01d13c0b1d37efe1ef3721f80bef508aca4dcbba 100644 (file)
@@ -29,7 +29,15 @@ namespace Hazel
 
         public long GetIP4Address()
         {
-            return ((IPEndPoint)this.RemoteEndPoint).Address.Address;
+            if (IPMode == IPMode.IPv4)
+            {
+                return ((IPEndPoint)this.RemoteEndPoint).Address.Address;
+            }
+            else
+            {
+                var bytes = ((IPEndPoint)this.RemoteEndPoint).Address.GetAddressBytes();
+                return BitConverter.ToInt64(bytes, bytes.Length - 8);
+            }
         }
     }
 }
index 32fa7c3b7e827b4c51b3938f3a1506c710138564..0fab2717fc27fb382a385c700cdc76cf944245f8 100644 (file)
@@ -107,6 +107,11 @@ namespace Hazel.Udp
                 HandleDisconnect(he);
                 throw he;
             }
+            catch (ArgumentOutOfRangeException e)
+            {
+                HazelException he = new HazelException("Something wonk with the buffer: " + bytes.Length, e);
+                HandleDisconnect(he);
+            }
         }
 
         /// <inheritdoc />
@@ -265,6 +270,11 @@ namespace Hazel.Udp
                 return;
             }
 
+            if (this.TestLagMs > 0)
+            {
+                Thread.Sleep(this.TestLagMs);
+            }
+
             HandleReceive(bytes);
         }
 
@@ -286,7 +296,11 @@ namespace Hazel.Udp
             //Invoke event outide lock if need be
             if (invoke)
             {
-                InvokeDisconnected(e);
+                try
+                {
+                    InvokeDisconnected(e);
+                }
+                catch { }
 
                 Dispose();
             }
index f262f7de03885865cb2494818f006fef51169beb..afd2a219f436e32a0ef82d9d897d8b52d22edbab 100644 (file)
@@ -219,7 +219,7 @@ namespace Hazel.Udp
 
                         Trace.WriteLine("Resend.");
                     },
-                    resendTimeout > 0 ? resendTimeout : (AveragePingMs != 0 ? (int)AveragePingMs * 4 : 200),
+                    resendTimeout > 0 ? resendTimeout : (int)Math.Max(40, Math.Min(AveragePingMs * 4, 750)),
                     ackCallback
                 );
 
@@ -385,7 +385,7 @@ namespace Hazel.Udp
                     packet.Stopwatch.Stop();
                     lock (PingLock)
                     {
-                        this.AveragePingMs = this.AveragePingMs * .7f + (float)packet.Stopwatch.Elapsed.TotalMilliseconds * .3f;
+                        this.AveragePingMs = Math.Max(10, this.AveragePingMs * .7f + (float)packet.Stopwatch.Elapsed.TotalMilliseconds * .3f);
                     }
 
                     packet.Recycle();
@@ -413,7 +413,11 @@ namespace Hazel.Udp
 
             // Always reply with acknowledgement in order to stop the sender repeatedly sending it
             // TODO: group acks together
-            WriteBytesToConnection(bytes, bytes.Length);
+            try
+            {
+                WriteBytesToConnection(bytes, bytes.Length);
+            }
+            catch (InvalidOperationException) { }
         }
     }
 }
index 85b6e22b67c3a4e36c1d4b5cbdaaa43993b4798e..2bf343569279230773b61e08f33ff4550214181b 100644 (file)
@@ -108,7 +108,11 @@ namespace Hazel.Udp
             //Invoke event outide lock if need be
             if (invoke)
             {
-                InvokeDisconnected(e);
+                try
+                {
+                    InvokeDisconnected(e);
+                }
+                catch { }
 
                 Dispose();
             }