private void RegisterNewUser(IPEndPoint sender, byte[] message, out byte[] send) { try { // Create a database access object to register new user with database. using (TheNoise_DatabaseControl.DataAccessLayer databaseAccess = new TheNoise_DatabaseControl.DataAccessLayer(databaseAddress.ToString(), databaseName, databaseUsername, databasePassword, databaseUseIntegratedSecurity)) { // Deserialize the login request. LoginData credentials = (LoginData)ObjectSerialization.Deserialize(message, typeof(LoginData)); // Attempt to register with the database. UserAddResult result; if (!debugging) { result = databaseAccess.addUser(credentials); } else { result = UserAddResult.Success; } ObjectSerialization.Serialize(result, out send); GeneralEvent.Invoke(this, new GeneralEventArgs(null, sender, "Issued a request for registration and the result was: " + result.ToString())); } } catch (Exception ex) { GeneralEvent.Invoke(this, new GeneralEventArgs(null, sender, "Tried to register a new user, but something failed: " + ex.Message)); ObjectSerialization.Serialize(UserAuthenticationResult.UnknownResult, out send); } }
private void AuthenticateClient(IPEndPoint sender, byte[] message, out byte[] send) { try { UserAuthenticationResult result; // Create a database access object to validate this user against. using (TheNoise_DatabaseControl.DataAccessLayer databaseAccess = new TheNoise_DatabaseControl.DataAccessLayer(databaseAddress.ToString(), databaseName, databaseUsername, databasePassword, databaseUseIntegratedSecurity)) { // Deserialize the auth request. LoginData credentials = (LoginData)ObjectSerialization.Deserialize(message, typeof(LoginData)); // Attempt to validate this user. if (!debugging) { result = databaseAccess.validateUser(credentials); } else { // Debug mode, simply accept the auth request. result = UserAuthenticationResult.Success; } if (result == UserAuthenticationResult.Success) { // Add the user to the authenticated list. authenticatedConnections.Add(sender, new TheNoiseClientHandler(credentials.username, audioPath + credentials.username + "\\", sender)); usernameLookup.Add(credentials.username, sender); authenticatedConnections[sender].AudioPacketReady += TheNoiseServer_audioPacketReady; authenticatedConnections[sender].TrackListUpdated += TheNoiseServer_trackListUpdated; // Fire event that a user is authenticated. ClientAuthenticated.Invoke(this, new ClientAuthEventArgs(sender, credentials.username)); } else { GeneralEvent.Invoke(this, new GeneralEventArgs(null, sender, "Tried to authenticate, but the database gave us: " + result.ToString())); } } // Generate response for client. ObjectSerialization.Serialize(result, out send); } catch (Exception ex) { GeneralEvent.Invoke(this, new GeneralEventArgs(null, sender, "Tried to authenticate, but something failed: " + ex.Message)); ObjectSerialization.Serialize(UserAuthenticationResult.UnknownResult, out send); } }