/// <summary>
 /// Raises when a client logs out.
 /// </summary>
 /// <param name="content">Content that describes what client has logged out and for what reason.</param>
 protected override void OnLoggedOut(LogoutContent content)
 {
     if (content.ClientName == Name)
     {
         Status = ClientStatus.Disconnected;
     }
 }
Beispiel #2
0
        /// <summary>
        /// Raises when a client logs out.
        /// </summary>
        /// <param name="content">Content that describes what client has logged out and for what reason.</param>
        protected override void OnLoggedOut(LogoutContent content)
        {
            clientNames.Remove(content.ClientName);
            Pusher.Push(content);

            if (content.Reason == LogoutReason.Kicked && content.ClientName == Name)
            {
                ResetClient();
            }
        }
Beispiel #3
0
        /// <summary>
        /// Sends a logout message to the server. There is no need to call <see cref="ClientBase.Dispose()"/>
        /// right after calling this method.
        /// </summary>
        private void Logout(LogoutReason reason)
        {
            if (Status == ClientStatus.LoggedIn)
            {
                var     args    = new LogoutContent(Name, reason);
                Package package = new Package(BaseCommand.Logout, args.Serialize());
                SendPackageAsyncBase(package);
            }

            if (Status != ClientStatus.Disconnected && Socket.Connected)
            {
                Socket.Shutdown(SocketShutdown.Both);
            }

            ResetClient();
        }
Beispiel #4
0
        /// <summary>
        /// Looks for, and removes timed out clients.
        /// </summary>
        private async void CheckForTimedOut()
        {
            for (int i = 0; i < clients.Count; i++)
            {
                // 500 ms wait time.
                bool isInactive = await clients[i].IsInactiveTaskAsync(PollWait);

                if (isInactive && i < clients.Count)
                {
                    var args = new LogoutContent(clients[i].Name, LogoutReason.TimedOut);
                    UiContext.Default.Invoke(OnClientLoggedOut, args);
                    clients[i].Dispose();
                    clients.RemoveAt(i);
                    i--;
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Boots the specified client from the server.
        /// </summary>
        /// <exception cref="ArgumentException">Client could not be found.</exception>
        public void KickClient(string name, string reason = "")
        {
            var client = clients.Find(c => c.Name == name);

            if (client == null)
            {
                throw new ArgumentException($@"Client ""{name}"" not found.", nameof(name));
            }

            var content = new LogoutContent(name, LogoutReason.Kicked, reason);
            var package = new Package((int)BaseCommand.Logout, content);

            OnClientLoggedOut(content);
            BroadcastPackage(package);
            clientNames.Remove(client.Name);
            client.Socket.Shutdown(SocketShutdown.Both);
            client.Dispose();
            clients.Remove(client);
        }
Beispiel #6
0
        /// <summary>
        /// Raises when a client received a completed <see cref="Package"/>.
        /// Not calling the base implementation of this methods will cause the client
        /// to malfunction.
        /// </summary>
        /// <param name="package">The package that has been received.</param>
        protected virtual void OnPackageReceived(Package package)
        {
            switch ((BaseCommand)package.Command)
            {
            case BaseCommand.Logout:
                var content = LogoutContent.Deserialize(package.Content);
                OnLoggedOut(content);
                break;

            case BaseCommand.ClientNames:
                var c = ClientNamesContent.Deserialize(package.Content);
                OnNamesReceived(c);
                break;

            case BaseCommand.Login:
                var loginContent = LoginContent.Deserialize(package.Content);
                OnLoggedIn(loginContent);
                break;
            }
        }
Beispiel #7
0
        private void OnPackageReceivedCore(ServerSideClient client, Package e)
        {
            switch ((BaseCommand)e.Command)
            {
            case BaseCommand.ClientNames: SendClientNames(client); break;

            case BaseCommand.Logout:
                clients.Remove(client);
                BroadcastPackageAsync(e, client);
                clientNames.Remove(client.Name);
                client.Dispose();
                OnClientLoggedOut(LogoutContent.Deserialize(e.Content));
                break;

            case BaseCommand.Login: ProcessLoginPackage(client, e); break;

            case BaseCommand.Sync:
                Synchronize(client);
                break;
            }

            OnPackageReceived(client, e);
        }
Beispiel #8
0
 /// <summary>
 /// Raised when a client logs out.
 /// </summary>
 /// <param name="content">Describes the reason why the client logged out.</param>
 protected virtual void OnClientLoggedOut(LogoutContent content)
 {
     Pusher.Push(content);
 }
Beispiel #9
0
 /// <summary>
 /// Raises when a client logs out.
 /// </summary>
 /// <param name="content">Content that describes what client has logged out and for what reason.</param>
 protected virtual void OnLoggedOut(LogoutContent content)
 {
 }