]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
Ensure Unity clients send a Disconnect packet when disposed
authorMatthew Endsley <mendsley@gmail.com>
Sun, 28 Feb 2021 23:48:25 +0000 (15:48 -0800)
committerMatthew Endsley <mendsley@gmail.com>
Tue, 2 Mar 2021 21:54:17 +0000 (13:54 -0800)
Hazel.UnitTests/TestHelper.cs
Hazel.UnitTests/UdpConnectionTests.cs
Hazel.UnitTests/UnityUdpConnectionTests.cs
Hazel/Udp/UnityUdpClientConnection.cs

index 39071c13cb98640091c258a0ef43646b5fd8e15c..082854e080ebc2f8d31620a3256627d61212f644 100644 (file)
@@ -1,4 +1,4 @@
-using System;
+using System;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 
 using Hazel;
@@ -270,6 +270,43 @@ namespace Hazel.UnitTests
             mutex2.WaitOne();
         }
 
+        /// <summary>
+        ///     Ensures a client sends a disconnect packet to the server on Dispose.
+        /// </summary>
+        /// <param name="listener">The listener to test.</param>
+        /// <param name="connection">The connection to test.</param>
+        internal static void RunClientDisconnectOnDisposeTest(NetworkConnectionListener listener, Connection connection)
+        {
+            ManualResetEvent mutex = new ManualResetEvent(false);
+            ManualResetEvent mutex2 = new ManualResetEvent(false);
+
+            listener.NewConnection += delegate (NewConnectionEventArgs args)
+            {
+                args.Connection.Disconnected += delegate (object sender2, DisconnectedEventArgs args2)
+                {
+                    mutex2.Set();
+                };
+
+                mutex.Set();
+            };
+
+            listener.Start();
+
+            connection.Connect();
+
+            if (!mutex.WaitOne(TimeSpan.FromSeconds(1)))
+            {
+                Assert.Fail("Timeout waiting for client connection");
+            }
+
+            connection.Dispose();
+
+            if (!mutex2.WaitOne(TimeSpan.FromSeconds(1)))
+            {
+                Assert.Fail("Timeout waiting for client disconnect packet");
+            }
+        }
+
         /// <summary>
         ///     Builds new data of increaseing value bytes.
         /// </summary>
index 2421268f1ed99ed876f6464848ef299dd2e4a4c7..1b3fb276e5294c135066ed0c0f469ac81861773c 100644 (file)
@@ -444,6 +444,18 @@ namespace Hazel.UnitTests
             }
         }
 
+        /// <summary>
+        ///     Test that a disconnect is sent when the client is disposed.
+        /// </summary>
+        public void ClientDisconnectOnDisposeTest()
+        {
+            using (UdpConnectionListener listener = new UdpConnectionListener(new IPEndPoint(IPAddress.Any, 4296)))
+            using (UdpConnection connection = new UdpClientConnection(new IPEndPoint(IPAddress.Loopback, 4296)))
+            {
+                TestHelper.RunClientDisconnectOnDisposeTest(listener, connection);
+            }
+        }
+
         /// <summary>
         ///     Tests disconnection from the server.
         /// </summary>
index 4f989c569ea3406c4adb5e38c1ad44d27f3f15ae..83f62fcddcb0cd4de27c3ed863bd97da9561354c 100644 (file)
@@ -414,6 +414,18 @@ namespace Hazel.UnitTests
             }
         }
 
+        /// <summary>
+        ///     Test that a disconnect is sent when the client is disposed.
+        /// </summary>
+        public void ClientDisconnectOnDisposeTest()
+        {
+            using (UdpConnectionListener listener = new UdpConnectionListener(new IPEndPoint(IPAddress.Any, 4296)))
+            using (UdpConnection connection = new UnityUdpClientConnection(new IPEndPoint(IPAddress.Loopback, 4296)))
+            {
+                TestHelper.RunClientDisconnectOnDisposeTest(listener, connection);
+            }
+        }
+
         /// <summary>
         ///     Tests disconnection from the server.
         /// </summary>
index 8b501802c184c1f71dbae76b793d81c46cb54063..25eb4db92eb0fb51daabf1318dd769fd942271ec 100644 (file)
@@ -14,6 +14,7 @@ namespace Hazel.Udp
     public class UnityUdpClientConnection : UdpConnection
     {
         private Socket socket;
+        private bool sendSynchronously = false;
 
         public UnityUdpClientConnection(IPEndPoint remoteEndPoint, IPMode ipMode = IPMode.IPv4)
             : base()
@@ -49,14 +50,26 @@ namespace Hazel.Udp
         {
             try
             {
-                socket.BeginSendTo(
-                    bytes,
-                    0,
-                    length,
-                    SocketFlags.None,
-                    EndPoint,
-                    HandleSendTo,
-                    null);
+                if (this.sendSynchronously)
+                {
+                    socket.SendTo(
+                        bytes,
+                        0,
+                        length,
+                        SocketFlags.None,
+                        EndPoint);
+                }
+                else
+                {
+                    socket.BeginSendTo(
+                        bytes,
+                        0,
+                        length,
+                        SocketFlags.None,
+                        EndPoint,
+                        HandleSendTo,
+                        null);
+                }
             }
             catch (NullReferenceException) { }
             catch (ObjectDisposedException)
@@ -246,6 +259,8 @@ namespace Hazel.Udp
         /// <inheritdoc />
         protected override void Dispose(bool disposing)
         {
+            this.sendSynchronously = true;
+
             if (disposing)
             {
                 SendDisconnect();