private void Call() { string targetLogin = connection[0]; if (String.IsNullOrEmpty(targetLogin)) { connection.SendMessage(CommandSet.Error, ErrorMessageID.UnknownError); return; } bool isFriend; lock (ThreadSync.Lock) { isFriend = IsFriend(targetLogin); } if (!isFriend) { connection.SendMessage(CommandSet.Error, ErrorMessageID.NotInContacts); return; } bool isOnline; lock (ThreadSync.Lock) { isOnline = UserCollection.IsOnline(targetLogin); } if (!isOnline) { connection.SendMessage(CommandSet.Call, ErrorMessageID.UserOffline); return; } User targetUser; try { targetUser = UserCollection.GetUserByLogin(targetLogin); } catch { connection.SendMessage(CommandSet.Error, ErrorMessageID.UnknownError); return; } if (targetUser.IsBusy) { connection.SendMessage(CommandSet.Call, ErrorMessageID.UserBusy); return; } connection.SendMessage(CommandSet.Call, targetUser.GetIPString(), targetUser.Firstport, targetUser.SecondPort); }
public void SendContacts() { UpdateContactsList(); string[] contacts = new string[ContactsList.Count]; for (int i = 0; i < contacts.Length; i++) { Contact contact = ContactsList[i]; string code = "0"; if (UserCollection.IsOnline(contact.Login)) { code = "1"; } contacts[i] = contact.ToString() + ";" + code; } connection.SendMessage(CommandSet.Contacts, String.Join(";", contacts)); }
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(); } }