Beispiel #1
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();
     }
 }
Beispiel #2
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));
         }
     }
 }
Beispiel #3
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);
        }
Beispiel #4
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"));
         }
     }
 }