From: Forest Date: Sun, 14 Oct 2018 19:45:47 +0000 (-0700) Subject: Fix some null refs and other weird bugs X-Git-Tag: 1.0.0~78 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=3037ea0720afbf12b52da7ce2f54bfd5963c1977;p=rhonda%2Fimpostor.hazel.git Fix some null refs and other weird bugs --- diff --git a/Hazel.UnitTests/MessageReaderTests.cs b/Hazel.UnitTests/MessageReaderTests.cs index 56894e2..fbcd9ec 100644 --- a/Hazel.UnitTests/MessageReaderTests.cs +++ b/Hazel.UnitTests/MessageReaderTests.cs @@ -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"; diff --git a/Hazel.UnitTests/UdpConnectionTests.cs b/Hazel.UnitTests/UdpConnectionTests.cs index 4ac8045..3476653 100644 --- a/Hazel.UnitTests/UdpConnectionTests.cs +++ b/Hazel.UnitTests/UdpConnectionTests.cs @@ -114,6 +114,35 @@ namespace Hazel.UnitTests connection.Connect(); } } + + /// + /// Tests dual mode connectivity. + /// + [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); + } + } + } /// /// 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(); } diff --git a/Hazel/Connection.cs b/Hazel/Connection.cs index 05f4513..74e3a22 100644 --- a/Hazel/Connection.cs +++ b/Hazel/Connection.cs @@ -50,6 +50,8 @@ namespace Hazel /// public event EventHandler DataReceived; + public int TestLagMs = -1; + public event Action 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 handler = DataReceived; - if (handler != null) - handler(this, args); + if (handler != null) handler.Invoke(this, args); } /// @@ -268,8 +269,7 @@ namespace Hazel //Make a copy to avoid race condition between null check and invocation EventHandler handler = Disconnected; - if (handler != null) - handler(this, args); + if (handler != null) handler.Invoke(this, args); } /// diff --git a/Hazel/NetworkConnection.cs b/Hazel/NetworkConnection.cs index 21f2f45..01d13c0 100644 --- a/Hazel/NetworkConnection.cs +++ b/Hazel/NetworkConnection.cs @@ -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); + } } } } diff --git a/Hazel/Udp/UdpClientConnection.cs b/Hazel/Udp/UdpClientConnection.cs index 32fa7c3..0fab271 100644 --- a/Hazel/Udp/UdpClientConnection.cs +++ b/Hazel/Udp/UdpClientConnection.cs @@ -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); + } } /// @@ -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(); } diff --git a/Hazel/Udp/UdpConnection.Reliable.cs b/Hazel/Udp/UdpConnection.Reliable.cs index f262f7d..afd2a21 100644 --- a/Hazel/Udp/UdpConnection.Reliable.cs +++ b/Hazel/Udp/UdpConnection.Reliable.cs @@ -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) { } } } } diff --git a/Hazel/Udp/UdpServerConnection.cs b/Hazel/Udp/UdpServerConnection.cs index 85b6e22..2bf3435 100644 --- a/Hazel/Udp/UdpServerConnection.cs +++ b/Hazel/Udp/UdpServerConnection.cs @@ -108,7 +108,11 @@ namespace Hazel.Udp //Invoke event outide lock if need be if (invoke) { - InvokeDisconnected(e); + try + { + InvokeDisconnected(e); + } + catch { } Dispose(); }