Exemplo n.º 1
0
        /*private void createChat(User chatuser, String username){
          if(mainControl != null && mainControl.InvokeRequired){
        mainControl.Invoke(createChat)*/
        private void messageListen()
        {
            while (true)
              {
            msgClient.Client.ReceiveTimeout = 100;
            try
            {
              byte[] rcv = msgClient.Receive(ref msgRCVEP);

            char[] rcvCStr = Encoding.ASCII.GetChars(rcv, 0, rcv.Length);
            String rcvStr = new String(rcvCStr);
            String uName = rcvStr.Substring(4, 32).Trim('\0');
            bool isEncrypted = false;
            if(rcvStr.Substring(41, 1)[0] != '\0')
             isEncrypted = (Int32.Parse(rcvStr.Substring(41, 1)) == 1);

            if (rcvStr.StartsWith("MESG"))
            {
              IPAddress rcvIP = msgRCVEP.Address;
              User chatUser = checkAndAddUser(uName, rcvIP);
              ChatFrm thisChat = null;
              bool chatOpen = false;

              int sequenceID = Int32.Parse(rcvStr.Substring(36, 5));
              char[] mackMSGStr = Message.setupPacket("MACK", username,
                                           sequenceID.ToString(), '0', "");
              IPEndPoint sendTo = new IPEndPoint(rcvIP, StaticVars.msgPort);
              UdpClient sendMACK = new UdpClient();
              sendMACK.Send(Encoding.ASCII.GetBytes(mackMSGStr),
                            mackMSGStr.Length, sendTo);

              if(chatUser.chatExists() && !chatUser.chatHidden()){
                thisChat = chatUser.getChat();
                threadSafeActivateChat(thisChat);
                chatOpen = true;
              }

              if(!chatOpen){
                thisChat = new ChatFrm(chatUser, username, this);
                newChats.Add(thisChat);
                chatUser.openChat(ref thisChat);

              }
              if (isEncrypted && !chatUser.isPasswordProtected())
              {
                GetPassword window = new GetPassword(chatUser.username);
                while (window.ShowDialog() != DialogResult.OK) ;
                chatUser.setPassword(window.password);
              }

              String messageStr = rcvStr.Substring(42, 140).Trim('\0');
              Message receivedMessage = new Message(messageStr,
                                                    thisChat.chatUser);

              if (!chatUser.receivedSequences.Contains(sequenceID))
              {
                chatUser.receivedSequences.Add(sequenceID);
                chatUser.receivedMessages.Add(receivedMessage);
              }

            }

            else if (rcvStr.StartsWith("MACK"))
            {
              foreach (User user in UserList)
              {
                if (user.username == uName)
                {
                  int sequenceID = Int32.Parse(rcvStr.Substring(36, 5));
                  SendMessage temp = null;
                  user.touchAwaitingMACK(false, ref temp, sequenceID);
                  break;
                }
              }
            }

            // Manage memory
            rcvCStr = null;
            rcvStr = null;
            uName = null;
              rcv = null;
            }

            // Catch the exception for when the receive times out
            // If the thread needs to be killed, exit the thread
            catch (SocketException)
            {
              if (rcvThreadKill)
            Application.ExitThread();
            }
              }
        }
Exemplo n.º 2
0
        private void cmUser_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            int selectedIndex = lbUsers.SelectedIndex;
              String selectedUsername = lbUsers.Items[selectedIndex].ToString();
              GetPassword window = new GetPassword(selectedUsername);
              if(window.ShowDialog() != DialogResult.OK)
            return;

              foreach (User user in UserList)
              {
            if (user.username == selectedUsername)
            {
              if (user.chatExists())
              {
            if (user.chatHidden())
            {
              user.setPassword(window.password);
              user.getChat().Show();
            }
            user.getChat().Activate();
              }
              else
              {
            ChatFrm newChat = new ChatFrm(user, username, this);
            user.setPassword(window.password);
            user.openChat(ref newChat);
            newChat.Show();
              }
            }
              }
        }