Esempio n. 1
0
 private void sendButton_Click(object sender, EventArgs e)
 {
     if (chatTabControlEx.SelectedIndex >= 0)
     {
         TabPage     tp   = chatTabControlEx.SelectedTab;
         string      name = tp.Name;
         RichTextBox rtb  = ((RichTextBox)tp.Controls[name + "rtb"]);
         string      buf  = msgRichTextBox.Text;
         string      msg  = MessengerMsg.FormString(MsgType.SEND, name, buf);
         user.Write(msg);
         rtb.AppendText(String.Format("<{0}> {1} \n", "You", buf));
         msgRichTextBox.Clear();
     }
 }
Esempio n. 2
0
 void StartResiving()
 {
     try
     {
         MessengerMsg msg;
         while (client.Connected)
         {
             msg = MessengerMsg.ParseMsg(br.ReadString());
             if (msg.msgType == MsgType.AV_USERS)
             {
                 string[] users           = msg.message.Split(MessengerMsg.EOU);
                 var      usersWithStatus =
                     from u in users
                     let i = u.IndexOf(MessengerMsg.EOC)
                             select new UsersEventInfo(
                         Convert.ToByte(u.Substring(0, i)),
                         u.Substring(i + MessengerMsg.EOC.Length));
                 OnUsersOnline(new UsersEventArgs(usersWithStatus.ToList()));
             }
             else if (msg.msgType == MsgType.USER_CHANGED_STATUS)
             {
                 if (msg.reciever != name)
                 {
                     OnUserChangedStatus(new UserStatusEventArgs(msg.reciever, true,
                                                                 Convert.ToByte(msg.message)));
                 }
             }
             else if (msg.msgType == MsgType.USER_OFFLINE)
             {
                 if (msg.reciever != name)
                 {
                     OnUserChangedStatus(new UserStatusEventArgs(msg.reciever, false,
                                                                 MsgType.USER_OFFLINE));
                 }
             }
             else if (msg.msgType == MsgType.SEND)
             {
                 OnMessageRecieved(new MessageRecievedEventArgs(msg.reciever, msg.message));
             }
         }
     }
     catch
     {
         CloseConnection();
     }
 }
Esempio n. 3
0
 public void Login()
 {
     if (IsConnected)
     {
         Write(MessengerMsg.FormString(MsgType.LOGIN, name, pass));
         MessengerMsg m = MessengerMsg.ParseMsg(br.ReadString());
         if (m.msgType == MsgType.OK)
         {
             login = true;
             OnLogin(new LoginEventArgs(true, "Logged-in succesfully"));
         }
         else if (m.msgType == MsgType.NO)
         {
             login = false;
             OnLogin(new LoginEventArgs(false, m.message));
         }
     }
 }
Esempio n. 4
0
        public bool ConnectCallback(IAsyncResult ar)
        {
            allDone.Set();
            TcpClient t = (TcpClient)ar.AsyncState;

            t.EndConnect(ar);
            netStream = t.GetStream();
            br        = new BinaryReader(netStream, Encoding.UTF8);
            bw        = new BinaryWriter(netStream, Encoding.UTF8);
            string       msg = br.ReadString();
            MessengerMsg m   = MessengerMsg.ParseMsg(msg);

            if (m.msgType == MsgType.HELLO)
            {
                Write(MessengerMsg.ServiceMsg(MsgType.HELLO).ToString());
                conn = true;
                OnConnected(new ConnectedEventArgs("Connected to the Server"));
                return(true);
            }
            return(false);
        }
Esempio n. 5
0
 public void Register()
 {
     if (IsConnected)
     {
         Write(MessengerMsg.FormString(MsgType.REGISTER, name, pass));
         MessengerMsg m = MessengerMsg.ParseMsg(br.ReadString());
         if (m.msgType == MsgType.OK)
         {
             OnRegister(new RegisterEventArgs(m.msgType, "Registered succesfully"));
         }
         else if (m.msgType == MsgType.NAME_TOO_LONG)
         {
             OnRegister(new RegisterEventArgs(m.msgType, "Name must be less than 40 symbols"));
         }
         else if (m.msgType == MsgType.PASSWORD_TOO_SMALL)
         {
             OnRegister(new RegisterEventArgs(m.msgType, "Password must be more than 4 symbols"));
         }
         else if (m.msgType == MsgType.PASSWORD_TOO_LONG)
         {
             OnRegister(new RegisterEventArgs(m.msgType, "Password must be less than 20 symbols"));
         }
         else if (m.msgType == MsgType.INVALID_NAME)
         {
             OnRegister(new RegisterEventArgs(m.msgType, "Name must not contein \"< > / ; :\" symbols"));
         }
         else if (m.msgType == MsgType.NAME_EXISTS)
         {
             OnRegister(new RegisterEventArgs(m.msgType, "User with this name already exists"));
         }
         else
         {
             OnRegister(new RegisterEventArgs(m.msgType, "Can't register"));
         }
     }
 }
Esempio n. 6
0
 public void ChageStatus(UserStatus s)
 {
     status = s;
     this.Write(MessengerMsg.FormString(MsgType.CHANGE_STATUS, ((byte)status).ToString(), String.Empty));
 }
Esempio n. 7
0
 public void GetUsersOnline()
 {
     this.Write(MessengerMsg.FormString(MsgType.AV_USERS, String.Empty, String.Empty));
 }