Exemple #1
0
        static private void GetContacts(BinaryReader reader)
        {
            byte[] bytes = new byte[User.TcpCLient.ReceiveBufferSize];

            string xml = reader.ReadString();

            DataSet dst = new DataSet();

            dst.ReadXml(new StringReader(xml));
            AsyncContactMethod ContactAdd = OnContactAdd;

            foreach (DataRow row in dst.Tables[0].Rows)
            {
                int     id       = int.Parse(row[0].ToString());
                string  fullname = row[1].ToString().Replace("  ", "");
                string  email    = row[2].ToString().Replace(" ", "");
                string  phone    = row[3].ToString().Replace("  ", "");
                int     status   = int.Parse(row[4].ToString());
                string  position = row[5].ToString().Replace("  ", "");
                int     chat     = int.Parse(row[6].ToString());
                int     count    = int.Parse(row[7].ToString());
                Contact c        = new Contact(id, fullname, email, phone, status, position, chat, count);
                ContactListView.BeginInvoke(ContactAdd, c);
            }
        }
Exemple #2
0
        static void ReceiveMessage()
        {
            User.GetListFriend();
            BinaryReader        reader = new BinaryReader(User.TcpCLient.GetStream());
            AsyncContactStatus  ContactStatusChanged = OnContactStatusChanged;
            AsyncContactMethod  ContactAdd           = OnContactAdd;
            AsyncContactMethod  ContactRemove        = OnContactRemove;
            AsyncContactGet     ContactGet           = GetContacts;
            AsyncGetChat        GetChat             = SetChatLog;
            AsyncChatMessStatus asyncChatMessStatus = ChatMessStatus;
            AsyncReceiveMessage asyncReceiveMessage = ReceiveMessage;

            while (true)
            {
                try
                {
                    int code = reader.ReadInt32();
                    switch (code)
                    {
                    case 3:
                    {
                        ContactGet(reader);
                        break;
                    }

                    case 4:
                    {
                        int id     = reader.ReadInt32();
                        int status = reader.ReadInt32();
                        foreach (Contact c in Contacts)
                        {
                            if (c.ID == id)
                            {
                                ContactListView.BeginInvoke(ContactStatusChanged, new object[] { c, status });
                                break;
                            }
                        }
                        break;
                    }

                    case 5:
                    {
                        int id     = reader.ReadInt32();
                        int status = reader.ReadInt32();
                        foreach (Contact c in Contacts)
                        {
                            if (c.ID == id)
                            {
                                ContactListView.BeginInvoke(ContactRemove, c);
                                break;
                            }
                        }
                        break;
                    }

                    case 6:
                    {
                        string  xml = reader.ReadString();
                        DataSet dst = new DataSet();
                        dst.ReadXml(new StringReader(xml));
                        DataSet chatDataSet = new DataSet();
                        chatDataSet.Tables.Add();
                        chatDataSet.Tables[0].Columns.Add();
                        chatDataSet.Tables[0].Columns.Add();
                        chatDataSet.Tables[0].Columns.Add();



                        if (dst.Tables.Count >= 1)
                        {
                            string date = "";
                            foreach (DataRow row in dst.Tables[0].Rows)
                            {
                                if (date != row[2].ToString())
                                {
                                    chatDataSet.Tables[0].Rows.Add(new string[] { "", "", row[2].ToString().Split('T')[0] });
                                }

                                date = row[2].ToString();
                                string time = row[3].ToString().Replace("PT", "").Replace("H", ":").Replace("M", "");
                                time = time.Remove(time.IndexOf(':') + 3);

                                string message = row[4].ToString();
                                string status  = int.Parse(row[5].ToString()) == 1 ? "Прочитано" : "Не прочитано";
                                string sender  = "";
                                if (int.Parse(row[1].ToString()) == SelectContact.ID)
                                {
                                    sender = SelectContact.Name + ":";
                                }
                                else
                                {
                                    sender = "Вы:";
                                }
                                sender += "\n" + status;

                                chatDataSet.Tables[0].Rows.Add(new string[] { time, sender, message });
                            }
                        }
                        ChatGrid.BeginInvoke(GetChat, chatDataSet);
                        break;
                    }

                    case 7:
                    {
                        int id = reader.ReadInt32();
                        if (SelectContact != null)
                        {
                            if (SelectContact.ID == id)
                            {
                                for (int i = 0; i < ChatGrid.Rows.Count; i++)
                                {
                                    if (ChatGrid.Rows[i].Cells[1].Value.ToString().Contains("Вы"))
                                    {
                                        ChatGrid.BeginInvoke(asyncChatMessStatus, i);
                                    }
                                }
                            }
                        }
                        break;
                    }

                    case 8:
                    {
                        int    id      = reader.ReadInt32();
                        string date    = reader.ReadString();
                        string time    = reader.ReadString();
                        string message = reader.ReadString();

                        if (SelectContact != null)
                        {
                            if (SelectContact.ID == id)
                            {
                                if (date != DateTime.Now.ToString("dd-MM-yyyy"))
                                {
                                    ChatGrid.BeginInvoke(asyncReceiveMessage, new object[] { "", date });
                                }

                                ChatGrid.BeginInvoke(asyncReceiveMessage, new object[] { time, message });
                            }
                            else
                            {
                                foreach (Contact c in Contacts)
                                {
                                    if (c.ID == id)
                                    {
                                        c.CountMessage++;
                                        ContactListView.BeginInvoke(ContactStatusChanged, new object[] { c, c.Status?1:0 });
                                        break;
                                    }
                                }
                            }
                        }
                        break;
                    }
                    }
                }
                catch (SocketException ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                catch (IOException ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    //break;
                }
            }
        }
Exemple #3
0
        // получение сообщений
        static void ReceiveMessage()
        {
            User.GetListFriend();
            BinaryReader       reader = new BinaryReader(User.TcpCLient.GetStream());
            AsyncContactStatus ContactStatusChanged = OnContactStatusChanged;
            AsyncContactMethod ContactAdd           = OnContactAdd;
            AsyncContactMethod ContactRemove        = OnContactRemove;
            AsyncContactGet    ContactGet           = GetContacts;

            while (true)
            {
                try
                {
                    if (logout)
                    {
                        break;
                    }

                    int code = reader.ReadInt32();
                    switch (code)
                    {
                    case 3:
                    {
                        Application.Current.Dispatcher.Invoke(ContactGet, reader);
                        break;
                    }

                    case 4:
                    {
                        int id     = reader.ReadInt32();
                        int status = reader.ReadInt32();
                        foreach (Contact c in Contacts)
                        {
                            if (c.ID == id)
                            {
                                Application.Current.Dispatcher.Invoke(ContactStatusChanged, new object[] { c, status });
                                break;
                            }
                        }
                        break;
                    }

                    case 5:
                    {
                        int id     = reader.ReadInt32();
                        int status = reader.ReadInt32();
                        foreach (Contact c in Contacts)
                        {
                            if (c.ID == id)
                            {
                                Application.Current.Dispatcher.Invoke(ContactRemove, c);
                                break;
                            }
                        }
                        break;
                    }
                    }
                }
                catch (SocketException ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                catch (IOException ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                catch
                {
                    break;
                }
            }
        }