Ejemplo n.º 1
0
 private void pMToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (!pmForms.ContainsKey(lstUsers.SelectedItems[0].Text) && lstUsers.SelectedItems[0].Text != GlobalProperties.Client.Username)
     {
         string userName = lstUsers.SelectedItems[0].Text;
         PMForm pmForm   = new PMForm(lstUsers.SelectedItems[0].Text, client);
         pmForms.Add(lstUsers.SelectedItems[0].Text, pmForm);
         pmForm.Show();
         pmForm.FormClosing += (SENDER, E) =>
         {
             pmForms.Remove(userName);
         };
     }
 }
Ejemplo n.º 2
0
        void client_Received(object sender, xDataReceivedEventArgs e)
        {
            ChatClient   client = sender as ChatClient;
            PacketReader r      = new PacketReader(e.Data, false);

            switch (e.Header)
            {
            case ChatClient.Header.USERNAME:
                online++;
                Invoke((MethodInvoker) delegate
                {
                    lstUsers.Columns[0].Text = "Online (" + online.ToString() + ")";
                    string _username         = r.ReadString();
                    lstUsers.Items.Add(_username);
                    if (blocked.Contains(_username))
                    {
                        lstUsers.Items[lstUsers.Items.Count - 1].ForeColor = Color.Red;
                    }
                    lstUsers.Sort();

                    WriteToChat(_username + ": Connected", Color.Green);

                    if (connectToolStripMenuItem.Checked)
                    {
                        GlobalProperties.SoundPlayer.Stream = Properties.Resources.beep_1;
                        GlobalProperties.SoundPlayer.Play();
                    }
                });
                break;

            case ChatClient.Header.USERS:
                List <string> usernames = new List <string>();
                int           len       = r.ReadInt32();
                for (int i = 0; i < len; i++)
                {
                    usernames.Add(r.ReadString());
                }
                usernames.Sort();
                Invoke((MethodInvoker) delegate
                {
                    lstUsers.Items.Clear();
                });
                foreach (string name in usernames)
                {
                    Invoke((MethodInvoker) delegate
                    {
                        lstUsers.Items.Add(name);
                        if (blocked.Contains(name))
                        {
                            lstUsers.Items[lstUsers.Items.Count - 1].ForeColor = Color.Red;
                        }
                    });
                }
                online = usernames.Count;
                Invoke((MethodInvoker) delegate
                {
                    lstUsers.Columns[0].Text = "Online (" + online.ToString() + ")";
                });
                break;

            case ChatClient.Header.MESSAGE:
                string username = r.ReadString();
                string message  = r.ReadString();
                int    bLen     = r.ReadInt32();
                for (int i = 0; i < bLen; i++)
                {
                    if (GlobalProperties.Client.Username == r.ReadString())
                    {
                        r.Close();
                        return;
                    }
                }
                if (blocked.Contains(username))
                {
                    break;
                }
                if (soundOnSentReceivedToolStripMenuItem.Checked)
                {
                    GlobalProperties.SoundPlayer.Stream = Properties.Resources.IM08;
                    GlobalProperties.SoundPlayer.Play();
                }
                StringBuilder sb = new StringBuilder();
                sb.Append(username + ": " + message);
                Invoke((MethodInvoker) delegate
                {
                    WriteToChat(sb.ToString(), Color.Black);
                });
                break;

            case ChatClient.Header.LEFT:
                string leftName = r.ReadString();
                online--;
                for (int i = 0; i < lstUsers.Items.Count; i++)
                {
                    Invoke((MethodInvoker) delegate
                    {
                        if (lstUsers.Items[i].Text == leftName)
                        {
                            lstUsers.Items.RemoveAt(i);
                            lstUsers.Columns[0].Text = "Online (" + online.ToString() + ")";
                            r.Close();
                            WriteToChat(leftName + ": Disconnected", Color.Red);

                            if (disconnectToolStripMenuItem.Checked)
                            {
                                GlobalProperties.SoundPlayer.Stream = Properties.Resources.boing_2;
                                GlobalProperties.SoundPlayer.Play();
                            }


                            return;
                        }
                    });
                }
                break;

            case ChatClient.Header.PM:
                string user = r.ReadString();
                if (blocked.Contains(user))
                {
                    break;
                }
                GlobalProperties.SoundPlayer.Stream = Properties.Resources.IM08;
                GlobalProperties.SoundPlayer.Play();
                string msg = r.ReadString();
                Invoke((MethodInvoker) delegate
                {
                    if (pmForms.ContainsKey(user))
                    {
                        if (!pmForms[user].IsDisposed)
                        {
                            pmForms[user].WriteToChat(msg, Color.Black);
                        }
                    }
                    else
                    {
                        PMForm newPM = new PMForm(user, client);
                        pmForms.Add(user, newPM);
                        newPM.Show();
                        newPM.WriteToChat(msg, Color.Black);
                        newPM.FormClosed += (s, E) =>
                        {
                            pmForms.Remove(user);
                        };
                    }
                });
                break;
            }
            r.Close();
        }
Ejemplo n.º 3
0
 private void pMToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (!pmForms.ContainsKey(lstUsers.SelectedItems[0].Text) && lstUsers.SelectedItems[0].Text != GlobalProperties.Client.Username)
     {
         string userName = lstUsers.SelectedItems[0].Text;
         PMForm pmForm = new PMForm(lstUsers.SelectedItems[0].Text, client);
         pmForms.Add(lstUsers.SelectedItems[0].Text, pmForm);
         pmForm.Show();
         pmForm.FormClosing += (SENDER, E) =>
         {
             pmForms.Remove(userName);
         };
     }
 }
Ejemplo n.º 4
0
        void client_Received(object sender, xDataReceivedEventArgs e)
        {
            ChatClient client = sender as ChatClient;
            PacketReader r = new PacketReader(e.Data, false);
            switch (e.Header)
            {
                case ChatClient.Header.USERNAME:
                    online++;
                    Invoke((MethodInvoker)delegate
                    {
                        lstUsers.Columns[0].Text = "Online (" + online.ToString() + ")";
                        string _username = r.ReadString();
                        lstUsers.Items.Add(_username);
                        if (blocked.Contains(_username))
                            lstUsers.Items[lstUsers.Items.Count - 1].ForeColor = Color.Red;
                        lstUsers.Sort();

                        WriteToChat(_username + ": Connected", Color.Green);

                        if (connectToolStripMenuItem.Checked)
                        {
                            GlobalProperties.SoundPlayer.Stream = Properties.Resources.beep_1;
                            GlobalProperties.SoundPlayer.Play();
                        }
                    });
                    break;

                case ChatClient.Header.USERS:
                    List<string> usernames = new List<string>();
                    int len = r.ReadInt32();
                    for (int i = 0; i < len; i++)
                    {
                        usernames.Add(r.ReadString());
                    }
                    usernames.Sort();
                    Invoke((MethodInvoker)delegate
                    {
                        lstUsers.Items.Clear();
                    });
                    foreach (string name in usernames)
                    {
                        Invoke((MethodInvoker)delegate
                        {
                            lstUsers.Items.Add(name);
                            if (blocked.Contains(name))
                                lstUsers.Items[lstUsers.Items.Count - 1].ForeColor = Color.Red;
                        });
                    }
                    online = usernames.Count;
                    Invoke((MethodInvoker)delegate
                    {
                        lstUsers.Columns[0].Text = "Online (" + online.ToString() + ")";
                    });
                    break;

                case ChatClient.Header.MESSAGE:
                    string username = r.ReadString();
                    string message = r.ReadString();
                    int bLen = r.ReadInt32();
                    for (int i = 0; i < bLen; i++)
                    {
                        if (GlobalProperties.Client.Username == r.ReadString())
                        {
                            r.Close();
                            return;
                        }
                    }
                    if (blocked.Contains(username))
                        break;
                    if (soundOnSentReceivedToolStripMenuItem.Checked)
                    {
                        GlobalProperties.SoundPlayer.Stream = Properties.Resources.IM08;
                        GlobalProperties.SoundPlayer.Play();
                    }
                    StringBuilder sb = new StringBuilder();
                    sb.Append(username + ": " + message);
                    Invoke((MethodInvoker)delegate
                    {
                        WriteToChat(sb.ToString(), Color.Black);
                    });
                    break;

                case ChatClient.Header.LEFT:
                    string leftName = r.ReadString();
                    online--;
                    for (int i = 0; i < lstUsers.Items.Count; i++)
                    {
                        Invoke((MethodInvoker)delegate
                        {
                            if (lstUsers.Items[i].Text == leftName)
                            {
                                lstUsers.Items.RemoveAt(i);
                                lstUsers.Columns[0].Text = "Online (" + online.ToString() + ")";
                                r.Close();
                                WriteToChat(leftName + ": Disconnected", Color.Red);

                                if (disconnectToolStripMenuItem.Checked)
                                {
                                    GlobalProperties.SoundPlayer.Stream = Properties.Resources.boing_2;
                                    GlobalProperties.SoundPlayer.Play();
                                }

                                return;
                            }
                        });
                    }
                    break;

                case ChatClient.Header.PM:
                    string user = r.ReadString();
                    if (blocked.Contains(user))
                        break;
                    GlobalProperties.SoundPlayer.Stream = Properties.Resources.IM08;
                    GlobalProperties.SoundPlayer.Play();
                    string msg = r.ReadString();
                    Invoke((MethodInvoker)delegate
                    {
                        if (pmForms.ContainsKey(user))
                        {
                            if (!pmForms[user].IsDisposed)
                                pmForms[user].WriteToChat(msg, Color.Black);
                        }
                        else
                        {
                            PMForm newPM = new PMForm(user, client);
                            pmForms.Add(user, newPM);
                            newPM.Show();
                            newPM.WriteToChat(msg, Color.Black);
                            newPM.FormClosed += (s, E) =>
                            {
                                pmForms.Remove(user);
                            };
                        }
                    });
                    break;
            }
            r.Close();
        }