public SendMessage(Message toSend, User chatUser, String thisUser, ChatFrm owner) { this.timeoutCounter = 0; this.toSend = toSend; this.owner = owner; this.chatUser = chatUser; this.thisUser = thisUser; SocketSend(); MACKTimer = new Timer(); MACKTimer.Interval = 10; MACKTimer.Elapsed += new ElapsedEventHandler(MACKTimer_Tick); MACKTimer.Enabled = true; MACKTimer.Start(); }
private void threadSafeActivateChat(ChatFrm thisChat) { Control sync = this.ActiveControl; if(sync != null && sync.InvokeRequired){ sync.Invoke((Action) delegate { threadSafeActivateChat(thisChat); }); return; } if(!thisChat.Visible) this.Show(); thisChat.Activate(); }
/*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(); } } }
private void lbUsers_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Return) { int selectedIndex = lbUsers.SelectedIndex; if (selectedIndex != -1) { String selectedUsername = lbUsers.Items[selectedIndex].ToString(); foreach (User user in UserList) { if (user.username == selectedUsername) { if (user.chatExists()) { if (user.chatHidden()) { user.getChat().Show(); } user.getChat().Activate(); } else { ChatFrm newChat = new ChatFrm(user, username, this); user.openChat(ref newChat); newChat.Show(); } } } } e.Handled = true; // Memory handle } }
private void lbUsers_DoubleClick(object sender, MouseEventArgs e) { Point p = Cursor.Position; p = lbUsers.PointToClient(p); int selectedIndex = lbUsers.IndexFromPoint(p); if (selectedIndex != -1) { // Check if a chat window is already open String selectedUsername = lbUsers.Items[selectedIndex].ToString(); foreach (User user in UserList) { if (user.username == selectedUsername) { if (user.chatExists()) { if (user.chatHidden()) { user.getChat().Show(); } user.getChat().Activate(); } else { ChatFrm newChat = new ChatFrm(user, username, this); user.openChat(ref newChat); newChat.Show(); } } } } }
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(); } } } }
public void openChat(ref ChatFrm chatWindow) { if (this.chatWindow != null && this.chatWindow.Visible) { this.chatWindow.Activate(); chatWindow = null; } else { this.chatWindow = chatWindow; //chatWindow.Show(); } }