예제 #1
0
        private void ForwardMessageToAllUsers(string message)
        {
            lock (usersLocker)
            {
                string[] messageArray = message.Split('-');

                if (messageArray.Length == 3)
                {
                    bool verifiedMessage = false;

                    for (int i = 0; i < this.users.Count; i++)
                    {
                        if (this.users[i].Username == messageArray[0] && this.users[i].SessionKey == messageArray[2])
                        {
                            verifiedMessage = true;
                            break;
                        }
                    }

                    if (verifiedMessage == true)
                    {
                        for (int i = 0; i < this.users.Count; i++)
                        {
                            this.users[i].NetworkWatcher.Send(ProtocolCreator.NewMessage(messageArray[0] + ": " + messageArray[1]));
                        }
                    }
                }
            }
        }
예제 #2
0
        private void IsAliveWorker()
        {
            while (this.isReading == true)
            {
                this.Send(ProtocolCreator.IsAlive());

                Thread.Sleep(2000);
            }
        }
예제 #3
0
        private void AddUser(string username, NetworkWatcher networkWatcher)
        {
            lock (usersLocker)
            {
                if (CheckIfLegalUsername(username) == true)
                {
                    // Sends session key to client
                    Protocol protocol = ProtocolCreator.SessionKey();
                    this.CreateNewUser(username, Encoding.UTF8.GetString(protocol.Content), networkWatcher);

                    networkWatcher.Send(protocol);
                }
            }
        }
예제 #4
0
        public void ForwardMessagesToAllClients()
        {
            while (true)
            {
                if (client.Connected == true)
                {
                    NetworkStream clientStream = client.GetStream();

                    if (clientStream.DataAvailable == false)
                    {
                        Thread.Sleep(100);
                        continue;
                    }

                    string messageProtocol = NetworkManager.ReadMessage(client, 302);

                    char[] mesageProtocolArray = messageProtocol.ToCharArray();

                    if (messageProtocol[0] == 'C' && messageProtocol[1] == 'H' && messageProtocol[2] == 'A' && messageProtocol[3] == 'T' && messageProtocol[4] == 'M' && messageProtocol[5] == 'E')
                    {
                        string messageString = string.Empty;

                        for (int i = 6; i < mesageProtocolArray.Length; i++)
                        {
                            messageString = messageString + mesageProtocolArray[i];
                        }

                        string[] messageProtocolContent = messageString.Split('-');

                        Protocol message = ProtocolCreator.PublishMessage(messageProtocolContent[0], GetUserGroup(messageProtocolContent[0]), messageProtocolContent[1]);

                        foreach (User user in userAccountManager.OnlineUser)
                        {
                            NetworkManager.SendMessage(message, user.Client);
                        }
                    }

                    Thread.Sleep(100);
                }
                else
                {
                    break;
                }
            }
        }
예제 #5
0
        public void VerifyUserLogin(string username, string password, TcpClient client, string userPath)
        {
            string path = userPath + @"\" + username + ".txt";

            if (File.Exists(path) == false)
            {
                Protocol loginInvalid = ProtocolCreator.LoginInvalid();

                NetworkManager.SendMessage(loginInvalid, client);
            }
            else if (File.Exists(path) == true)
            {
                if (VerifyUserData(username, password, path) == true)
                {
                    Protocol loginOK = ProtocolCreator.LoginOk();
                    NetworkManager.SendMessage(loginOK, client);

                    string protocolString = loginOK.ToString();

                    char[] protocolStringArray = protocolString.ToCharArray();

                    string sessionKey = string.Empty;

                    if (protocolStringArray[0] == 'C' && protocolStringArray[1] == 'H' && protocolStringArray[2] == 'A' && protocolStringArray[3] == 'T' && protocolStringArray[4] == 'L' && protocolStringArray[5] == 'O')
                    {
                        for (int i = 6; i < protocolStringArray.Length; i++)
                        {
                            sessionKey = sessionKey + protocolStringArray[i];
                        }

                        this.OnlineUser.Add(new User(username, client, sessionKey));

                        Console.WriteLine("{0} ({1}) has logged in!", username, ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString());

                        MessageManager messageManager = new MessageManager(this, client);
                        messageManager.ForwardMessagesToAllClients();
                    }
                }
                else
                {
                    Protocol loginInvalid = ProtocolCreator.LoginInvalid();
                    NetworkManager.SendMessage(loginInvalid, client);
                }
            }
        }
예제 #6
0
        private void RemoveUser(string username, string sessionkey)
        {
            lock (usersLocker)
            {
                // Verifys session key of the logout request
                bool legitSessionKey = false;

                for (int i = 0; i < this.users.Count; i++)
                {
                    if (this.users[i].Username == username && this.users[i].SessionKey == sessionkey)
                    {
                        legitSessionKey = true;
                        break;
                    }
                }

                if (legitSessionKey == true)
                {
                    for (int i = 0; i < this.users.Count; i++)
                    {
                        if (this.users[i].Username == username)
                        {
                            this.AddLineToLog(username + " (" + ((IPEndPoint)this.users[i].NetworkWatcher.Client.Client.RemoteEndPoint).Address.ToString() + ")" + " logged out!");
                            this.users[i].NetworkWatcher.Stop();
                            this.users.Remove(this.users[i]);
                            break;
                        }
                    }
                }

                // Sends all users the user who logged out
                for (int i = 0; i < this.users.Count; i++)
                {
                    this.users[i].NetworkWatcher.Send(ProtocolCreator.RemoveUser(username));
                    this.users[i].NetworkWatcher.Send(ProtocolCreator.NewMessage("SERVER: " + username + " logged out!"));
                }
            }
        }
예제 #7
0
        private void FinishUserLogin(string username, string sessionkey)
        {
            lock (usersLocker)
            {
                NetworkWatcher networkWatcher = null;

                // Gets the networkWatcher of the new user
                for (int i = 0; i < this.users.Count; i++)
                {
                    if (this.users[i].Username == username && this.users[i].SessionKey == sessionkey)
                    {
                        networkWatcher = this.users[i].NetworkWatcher;
                    }
                }

                if (networkWatcher != null)
                {
                    // Sends the new user all online users
                    for (int i = 0; i < this.users.Count; i++)
                    {
                        if (this.users[i].Username != username)
                        {
                            networkWatcher.Send(ProtocolCreator.AddUser(this.users[i].Username));
                        }
                    }

                    // Sends all users the user who logged in
                    for (int i = 0; i < this.users.Count; i++)
                    {
                        this.users[i].NetworkWatcher.Send(ProtocolCreator.AddUser(username));
                        this.users[i].NetworkWatcher.Send(ProtocolCreator.NewMessage("SERVER: " + username + " logged in!"));
                    }

                    this.AddLineToLog(username + " (" + ((IPEndPoint)networkWatcher.Client.Client.RemoteEndPoint).Address.ToString() + ")" + " logged in!");
                }
            }
        }
예제 #8
0
        public static void CreateNewUser(string username, string password, TcpClient client, string userPath)
        {
            string path = userPath + @"\" + username + ".txt";

            if (File.Exists(path) == true)
            {
                Protocol registrationInvalid = ProtocolCreator.RegistrationInvalid();

                NetworkManager.SendMessage(registrationInvalid, client);
            }
            else
            {
                if (CheckIfLegalUserData(username, password) == false)
                {
                    Protocol registrationInvalid = ProtocolCreator.RegistrationInvalid();

                    NetworkManager.SendMessage(registrationInvalid, client);
                }
                else
                {
                    string[] userFile = new string[3];

                    userFile[0] = "Username: "******"Password: "******"User Group: User";

                    File.WriteAllLines(path, userFile);

                    Protocol registrationOk = ProtocolCreator.RegistrationOk();

                    NetworkManager.SendMessage(registrationOk, client);

                    Console.WriteLine("{0} ({1}) has registered!", username, ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString());
                }
            }
        }