public void ClientDisconnectTest() { using (ThreadLimitedUdpConnectionListener listener = new ThreadLimitedUdpConnectionListener(2, new IPEndPoint(IPAddress.Any, 4296), new NullLogger())) using (UdpConnection connection = new UdpClientConnection(new IPEndPoint(IPAddress.Loopback, 4296))) { 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(); mutex.WaitOne(); connection.Disconnect("Testing"); mutex2.WaitOne(); } }
/// <summary> /// Helper that disconnects and reconnects to the lobby. Used after a game and /// when we have gathered all info we needed from spawning ourselves. Will not /// invoke the disconnect handler, unless the connecting fails. /// </summary> private async Task DisconnectAndReconnect() { _connection.RemoveDisconnectListeners(); _connection.Disconnect("Bye bye!"); try { await Connect(_address, _lobbyName, _port); } catch { OnDisconnect?.Invoke(); } }
/// <summary> /// When invoked, does a final termination of the client. This will emit the /// disconnect event. /// </summary> public void DisconnectAndExit() { _connection?.Disconnect("Bye bye!"); }