/// <summary> /// Called when a connection request is made. /// </summary> /// <param name="packetHeader">The packet header.</param> /// <param name="connection">The connection.</param> /// <param name="credentials">The credentials.</param> private void HandleLoginRequested(PacketHeader packetHeader, Connection connection, ClientCredentials credentials) { lock (this.AuthenticatedClients) { // If the client is already connected, remove it from the AuthenticatedClients dictionnary if (this.IsClientAuthenticated(connection)) { this.AuthenticatedClients.RemoveClient(connection); } // If the client's account is not already used and if credentials are correct bool loginAllowed = !this.IsUsernameAlreadyConnected(credentials.Username) && this.CheckCredentials(credentials); if (loginAllowed) { // Add the client to the list this.AuthenticatedClients.AddClient(connection, credentials.Username); // Add a message to logs //this.Logs += string.Format("{0} is now connected !\n", this.AuthenticatedClients[connection]); this.Logs += string.Format("{0} is now connected !\n", this.AuthenticatedClients.GetUsername(connection)); } // Send a reply to the client connection.SendObject <bool>(PACKET_TYPE_LOGIN_REPLY, loginAllowed); } this.NotifyObservers(); }
/// <summary> /// Called when a profile modification request is made. /// </summary> /// <param name="packetHeader">The packet header.</param> /// <param name="connection">The connection.</param> /// <param name="newClientCredentials">The new client's credentials.</param> private void HandleModifyProfileRequested(PacketHeader packetHeader, Connection connection, ClientCredentials newClientCredentials) { if (this.IsClientAuthenticated(connection)) { // Modify client's credentials bool clientCredentialsModified = this.Database.ModifyClientCredentials(this.AuthenticatedClients.GetUsername(connection), newClientCredentials); // Send a reply to the client connection.SendObject <bool>(PACKET_TYPE_MODIFYPROFILE_REPLY, clientCredentialsModified); // Eventually disconnect the client if (clientCredentialsModified) { connection.CloseConnection(false); // The client have to reconnect } this.NotifyObservers(); } }
/// <summary> /// Called when a registration request is made. /// </summary> /// <param name="packetHeader">The packet header.</param> /// <param name="connection">The connection.</param> /// <param name="credentials">The credentials.</param> private void HandleRegistrationRequested(PacketHeader packetHeader, Connection connection, ClientCredentials credentials) { // Try to register the client bool registrationAccomplished = this.Database.RegisterClient(credentials); // Send a reply to the client connection.SendObject <bool>(PACKET_TYPE_REGISTRATION_REPLY, registrationAccomplished); this.NotifyObservers(); }