private void Invite() { string targetLogin = connection[0]; if (String.IsNullOrEmpty(targetLogin) || targetLogin == Login) { connection.SendMessage(CommandSet.Error, ErrorMessageID.UnknownError); return; } string message = connection[1]; if (message.Trim().Length == 0) // pozbywamy się pustych wiadomości { message = String.Empty; } string result; lock (ThreadSync.Lock) { result = Database.AddInvitation(this, targetLogin, message); } if (result != ErrorMessageID.OK) { connection.SendMessage(CommandSet.Error, result); return; } try { User targetUser = UserCollection.GetUserByLogin(targetLogin); targetUser.NewInvitation(this, message); } catch { Debug.WriteLine("Użytkownik " + targetLogin + " nie jest online"); } connection.SendMessage(CommandSet.OK); }
private void AcceptInvite() { string inviterLogin = connection[0]; lock (ThreadSync.Lock) { if (!Database.RemoveInvitation(inviterLogin, Login)) { return; } Database.AddContact(inviterLogin, Login); Database.AddContact(Login, inviterLogin); } try { User targetUser = UserCollection.GetUserByLogin(inviterLogin); targetUser.InvitationAccepted(this); } catch { // jeśli użytkownik nie jest online to nie robimy nic } SendContacts(); }
private void AcceptSession(Thread connectionThread, TcpClient userConnection) { System.Timers.Timer timeout = new System.Timers.Timer(5000); // timer ustawiony na 5 sekund, tyle czasu ma klient na przesłanie danych timeout.Elapsed += delegate { onConnectionTimeoutEvent(connectionThread, userConnection); }; timeout.Start(); Connection connection = new Connection(userConnection); connection.ReceiveMessage(); if (connection.Command == CommandSet.Login || connection.Command == CommandSet.Register) { if (UserCollection.IsOnline(connection[0])) { connection.SendMessage(CommandSet.AuthFail, ErrorMessageID.UserAlreadyLoggedIn); connection.Disconnect(); return; } bool isNewUser = false; string login = connection[0].ToLower(); string password = connection[1]; string name = null; string firstPort = null, secondPort = null; if (connection.Command == CommandSet.Login) { firstPort = connection[2]; secondPort = connection[3]; } else if (connection.Command == CommandSet.Register) { firstPort = connection[3]; secondPort = connection[4]; isNewUser = true; name = connection[2]; } User user; NetworkCredential userCredential; try { userCredential = new NetworkCredential(login, password); } catch // jeśli dane podane przez klienta mają nieprawidłowy format { userConnection.Close(); return; } timeout.Stop(); string errorMessageID = ErrorMessageID.UnknownError; //ewentualna odpowiedz bazy danych w przypadku błędu bool isValidUser; lock (ThreadSync.Lock) { isValidUser = Database.TryGetUser(out user, userCredential, ref errorMessageID, isNewUser, name); // sprawdzamy czy użytkownik istnieje w bazie danych oraz czy podał prawidłowe hasło } if (!isValidUser) { connection.SendMessage(CommandSet.AuthFail, errorMessageID); connection.Disconnect(); return; } else { Console.WriteLine("Zalogowano użytkownika: " + login); connection.SendMessage(CommandSet.AuthSuccess); // przesyłamy referencje do danych które nie są znane bazie danych user.Firstport = firstPort; user.SecondPort = secondPort; user.SetConnection(connection); user.UpdateContactsList(); UserCollection.Add(user); // odnotowujemy że dany użytkownik stał się online user.DoWork(connectionThread); // dalsza obsługa klienta } } else { connection.Disconnect(); } }