From: Forest Date: Thu, 20 Jun 2019 21:38:47 +0000 (-0700) Subject: Fix and test disconnect message feature X-Git-Tag: 1.0.0~40 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=38c8747a67ab01b4baf01bb90ca1d7fe2ac985ec;p=rhonda%2Fimpostor.hazel.git Fix and test disconnect message feature --- diff --git a/Hazel.UnitTests/UdpConnectionTests.cs b/Hazel.UnitTests/UdpConnectionTests.cs index aa154ce..f7a95c0 100644 --- a/Hazel.UnitTests/UdpConnectionTests.cs +++ b/Hazel.UnitTests/UdpConnectionTests.cs @@ -391,5 +391,41 @@ namespace Hazel.UnitTests TestHelper.RunServerDisconnectTest(listener, connection); } } + + /// + /// Tests disconnection from the server. + /// + [TestMethod] + public void ServerExtraDataDisconnectTest() + { + using (UdpConnectionListener listener = new UdpConnectionListener(new IPEndPoint(IPAddress.Any, 4296))) + using (UdpConnection connection = new UdpClientConnection(new IPEndPoint(IPAddress.Loopback, 4296))) + { + MessageReader received = null; + ManualResetEvent mutex = new ManualResetEvent(false); + + connection.Disconnected += delegate (object sender, DisconnectedEventArgs args) + { + received = args.Message; + mutex.Set(); + }; + + listener.NewConnection += delegate (NewConnectionEventArgs args) + { + MessageWriter writer = MessageWriter.Get(SendOption.None); + writer.Write("Goodbye"); + args.Connection.Disconnect("Testing", writer); + }; + + listener.Start(); + + connection.Connect(); + + mutex.WaitOne(); + + Assert.IsNotNull(received); + Assert.AreEqual("Goodbye", received.ReadString()); + } + } } } diff --git a/Hazel/DisconnectedEventArgs.cs b/Hazel/DisconnectedEventArgs.cs index 3d87d66..bbecf81 100644 --- a/Hazel/DisconnectedEventArgs.cs +++ b/Hazel/DisconnectedEventArgs.cs @@ -31,9 +31,10 @@ namespace Hazel public readonly MessageReader Message; - public DisconnectedEventArgs(string reason, MessageReader reader) + public DisconnectedEventArgs(string reason, MessageReader message) { - + this.Reason = reason; + this.Message = message; } } } diff --git a/Hazel/NetworkConnection.cs b/Hazel/NetworkConnection.cs index ba81822..f670d11 100644 --- a/Hazel/NetworkConnection.cs +++ b/Hazel/NetworkConnection.cs @@ -40,11 +40,26 @@ namespace Hazel /// protected abstract bool SendDisconnect(MessageWriter writer); - /// /// Called when the socket has been disconnected at the remote host. /// - /// The exception if one was the cause. + protected void DisconnectRemote(string reason, MessageReader reader) + { + if (this.SendDisconnect(null)) + { + try + { + InvokeDisconnected(reason, reader); + } + catch { } + } + + this.Dispose(); + } + + /// + /// Called when the socket has been disconnected locally. + /// public override void Disconnect(string reason, MessageWriter writer = null, bool fireEvent = true) { if (this.SendDisconnect(writer) && fireEvent) diff --git a/Hazel/Udp/UdpConnection.cs b/Hazel/Udp/UdpConnection.cs index 8889a1f..5cc56eb 100644 --- a/Hazel/Udp/UdpConnection.cs +++ b/Hazel/Udp/UdpConnection.cs @@ -121,7 +121,9 @@ namespace Hazel.Udp break; case (byte)UdpSendOption.Disconnect: - Disconnect("The remote sent a disconnect request"); + message.Offset = 1; + message.Position = 0; + DisconnectRemote("The remote sent a disconnect request", message); message.Recycle(); break;