TestHelper.RunServerDisconnectTest(listener, connection);
}
}
+
+ /// <summary>
+ /// Tests disconnection from the server.
+ /// </summary>
+ [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());
+ }
+ }
}
}
public readonly MessageReader Message;
- public DisconnectedEventArgs(string reason, MessageReader reader)
+ public DisconnectedEventArgs(string reason, MessageReader message)
{
-
+ this.Reason = reason;
+ this.Message = message;
}
}
}
/// </summary>
protected abstract bool SendDisconnect(MessageWriter writer);
-
/// <summary>
/// Called when the socket has been disconnected at the remote host.
/// </summary>
- /// <param name="e">The exception if one was the cause.</param>
+ protected void DisconnectRemote(string reason, MessageReader reader)
+ {
+ if (this.SendDisconnect(null))
+ {
+ try
+ {
+ InvokeDisconnected(reason, reader);
+ }
+ catch { }
+ }
+
+ this.Dispose();
+ }
+
+ /// <summary>
+ /// Called when the socket has been disconnected locally.
+ /// </summary>
public override void Disconnect(string reason, MessageWriter writer = null, bool fireEvent = true)
{
if (this.SendDisconnect(writer) && fireEvent)
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;