Example #1
0
        /// <summary>
        /// Logs out the given <see cref="Client"/>.
        /// <remarks>Does nothing if the <see cref="Client"/> is not logged in.</remarks>
        /// </summary>
        /// <param name="client">The <see cref="Client"/> that should be logged out.</param>
        public void LogoutClient(Client.Client client)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }

            //Check if the client is logged in and if that is the case remove it from the dictionary of logged in clients.
            Account account = null;

            lock (_Lock)
            {
                if (_SessionByClient.TryGetValue(client, out Session session))
                {
                    _SessionByClient.Remove(client);
                    account = session.Account;
                    _ClientIdLoggedIn[session.ClientId] = false;
                    session.Invalidate();
                }
            }

            //If the Client ist still connected we have to send the messages that are required to bring the client back to the state of being able to log in again.
            if (client.IsConnected)
            {
                using (var message = _PacketWriterPool.GetScriptMessageStream(ScriptMessages.LogoutAcknowledged))
                {
                    client.SendScriptMessage(message, NetPriority.High, NetReliability.ReliableOrdered);
                }
            }

            //Invoke the logout event.
            if (account != null)
            {
                _Dispatcher.RunOrEnqueue(() =>
                {
                    var eventArgs = new LogoutEventArgs(client, account);
                    account.OnLoggedOut(eventArgs);
                    ClientLoggedOut?.Invoke(this, eventArgs);
                });
            }
        }