Beispiel #1
0
        void Listening()
        {
            while (true)
            {
                User user = new User();
                user.socket = server.Accept();
                try {
                    user.username = MyNetwork.Read(user.socket);
                } catch {
                    ShowMsg("A new connection lost when sending username. " + "(" + user.socket.RemoteEndPoint.ToString() + ")");
                    continue;
                }
                if (bannedIp.Contains(user.IP))   //Banned IP
                {
                    SendToUser(user, "/refuse_banned");
                    ShowMsg("A new connection was refused because its IP has been banned. " + "(" + user.username + ", " + user.socket.RemoteEndPoint.ToString() + ")");
                    continue;
                }
                if (users.ContainsKey(user.username))   //Duplicate Name
                {
                    SendToUser(user, "/refuse_duplicate");
                    ShowMsg("A new connection was refused because of its duplicated username. " + "(" + user.username + ", " + user.socket.RemoteEndPoint.ToString() + ")");
                    continue;
                }
                users.Add(user.username, user);
                ShowMsg("A new connection builded. " + "(" + user.username + ", " + user.socket.RemoteEndPoint.ToString() + ")");

                Broadcast(user.username + " entered the chatroom.");

                Thread read = new Thread(delegate() { ReadAndBroadcast(user); });
                read.IsBackground = true;
                read.Start();
            }
        }
Beispiel #2
0
 void ReadAndBroadcast(User user)
 {
     try {
         while (true)
         {
             string text = MyNetwork.Read(user.socket);
             Broadcast(user.username + ": " + text);
         }
     } catch {
         Offline(user);
     }
 }
Beispiel #3
0
 void ReadAndAct()
 {
     while (true)
     {
         string text;
         try {
             text = MyNetwork.Read(client);
         } catch {
             ConnectionLost();
             return;
         }
         if (text[0] == '/')
         {
             OptCmdFromServer(text);
         }
         else
         {
             ShowMsg(text);
         }
     }
 }