private async void SendAddToGroupToServer()
 {
     try
     {
         foreach (var item in pnGroup.Controls)
         {
             if (item.GetType() == typeof(ucGroupToAdd))
             {
                 if ((item as ucGroupToAdd).isAdd)
                 {
                     listAdd.Add((item as ucGroupToAdd).group);
                 }
             }
         }
         foreach (var item in listAdd)
         {
             byte[]       tempbuff = Encoding.UTF8.GetBytes("GPENDING%" + selectedUser.Id + "%" + item.ID + "%" + item.Name + "%" + item.admin.Id);
             SmallPackage package  = new SmallPackage(0, 1024, "M", tempbuff, "0");
             FrmMain.server.GetStream().WriteAsync(package.Packing(), 0, package.Packing().Length);
         }
     }
     catch
     {
         MessageBox.Show("Please check the connection again or the server could not be found!", "Error Connected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }
Beispiel #2
0
        public async void ChangeAvatar()
        {
            try
            {
                Guid         id             = Guid.NewGuid();
                byte[]       tempBuff       = Encoding.UTF8.GetBytes("SAVEAVATAR%" + fi.Length + "%" + fi.Name + "%" + fi.Extension + "%" + id.ToString());
                SmallPackage packageReceive = new SmallPackage(0, 1024, "M", tempBuff, "0");
                FrmMain.server.GetStream().WriteAsync(packageReceive.Packing(), 0, packageReceive.Packing().Length);

                /// Send file
                byte[] data = File.ReadAllBytes(fi.FullName);

                await FrmMain.client.SendFileToServer(data, "A", id.ToString());

                byte[] tempfile = File.ReadAllBytes(fi.FullName);
                id = Guid.NewGuid();
                File.WriteAllBytes(@"./cache/avatar/" + id.ToString() + fi.Extension, tempfile);
                FrmMain.me.AvatarPath = @"./cache/avatar/" + id.ToString() + fi.Extension;
                this.parent.LoadUser();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Please check the connection again or the server could not be found!", "Error Connected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        public async Task SendFile()
        {
            try
            {
                if (this.panelListFile.Controls.Count > 0)
                {
                    foreach (var item in files)
                    {
                        AddFileToListChat(FrmMain.me, "-1", item.Name);
                        //Gửi
                        byte[] data = File.ReadAllBytes(item.FullName);
                        Guid   id   = Guid.NewGuid();

                        byte[] tempbuff = Encoding.UTF8.GetBytes("STARTSENDFILE%" + group.ID + "%" + data.Length.ToString() + "%" + item.Name + "%" + item.Extension + "%"
                                                                 + id.ToString() + "%" + "Public");
                        SmallPackage Smallpackage = new SmallPackage(0, 1024, "M", tempbuff, "0");
                        await FrmMain.server.GetStream().WriteAsync(Smallpackage.Packing(), 0, Smallpackage.Packing().Length);

                        FrmMain.client.SendFileToServer(data, "F", id.ToString());;
                    }
                    this.files.Clear();
                    this.panelListFile.Controls.Clear();
                    this.panelListFile.Visible = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Please check the connection again or the server could not be found!", "Error Connected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Beispiel #4
0
 private void txtCreate_KeyDown(object sender, KeyEventArgs e)
 {
     if (e.KeyCode == Keys.Enter)
     {
         try
         {
             if (string.IsNullOrEmpty(txtCreate.Text))
             {
                 MessageBox.Show("Please enter group name!", "Error create group", MessageBoxButtons.OK, MessageBoxIcon.Warning);
             }
             else
             {
                 if (txtCreate.Text.Length > 32)
                 {
                     MessageBox.Show("Name is shorter than 32", "Name is not invalid", MessageBoxButtons.OK);
                     return;
                 }
                 else
                 {
                     byte[]       buff         = Encoding.UTF8.GetBytes(string.Format("CREATEGR%{0}%{1}", txtCreate.Text, FrmMain.me.Id));
                     SmallPackage smallPackage = new SmallPackage(0, 1024, "M", buff, "0");
                     FrmMain.server.GetStream().WriteAsync(smallPackage.Packing(), 0, smallPackage.Packing().Length);
                     this.txtCreate.Text = "";
                 }
             }
         }
         catch (Exception ex)
         {
             MessageBox.Show("Please check the connection again or the server could not be found!", "Error Connected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
         }
     }
 }
Beispiel #5
0
 private async void LoadDataUser()
 {
     byte[]       buff           = new byte[1024];
     byte[]       tempbuff       = Encoding.UTF8.GetBytes("LOADUSERDATA%");
     SmallPackage packageReceive = new SmallPackage(0, 1024, "M", tempbuff, "0");
     await server.GetStream().WriteAsync(packageReceive.Packing(), 0, packageReceive.Packing().Length);
 }
Beispiel #6
0
        public async Task SendFileToServer(byte[] package, string Style, string IDpackage)
        {
            int byteSent        = 0;
            int byteLeft        = package.Length;
            int nextPackageSize = 964;

            SmallPackage packageSend;

            while (byteLeft > 0)
            {
                byte[] tempBuff = new byte[964];
                if (byteLeft >= 964)
                {
                    Buffer.BlockCopy(package, byteSent, tempBuff, 0, 964);
                }
                else
                {
                    Buffer.BlockCopy(package, byteSent, tempBuff, 0, byteLeft);
                }

                packageSend = new SmallPackage(byteSent, package.Length, Style, tempBuff, IDpackage.ToString());

                mClient.GetStream().WriteAsync(packageSend.Packing(), 0, packageSend.Packing().Length);
                byteSent += nextPackageSize;
                byteLeft -= nextPackageSize;
            }
        }
Beispiel #7
0
        private void txtCreate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtCreate.Text))

                {
                    MessageBox.Show("Please enter contact name!", "Error create contact", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    try
                    {
                        byte[]       tempBuff     = Encoding.UTF8.GetBytes(string.Format("CREATECB%{0}", this.txtCreate.Text));
                        SmallPackage smallPackage = new SmallPackage(0, 1024, "M", tempBuff, "Server");
                        FrmMain.server.GetStream().WriteAsync(smallPackage.Packing(), 0, smallPackage.Packing().Length);
                        txtCreate.Clear();
                    }
                    catch
                    {
                        MessageBox.Show("Please check the connection again or the server could not be found!", "Error Connected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }
        private async void SendAddToContact()
        {
            try
            {
                foreach (var item in pnUser.Controls)
                {
                    if (item.GetType() == typeof(ucADD))
                    {
                        if ((item as ucADD).isAdd)
                        {
                            listUser.Add((item as ucADD).user);
                        }
                    }
                }
                foreach (var item in listUser)
                {
                    byte[] tempbuff = Encoding.UTF8.GetBytes("ADDCONTACT%" + item.Id + "%" +
                                                             selectedContactBook.contactBook.ID);
                    SmallPackage package = new SmallPackage(0, 1024, "M", tempbuff, "0");
                    FrmMain.server.GetStream().WriteAsync(package.Packing(), 0, package.Packing().Length);

                    selectedContactBook._AddUser(item);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Please check the connection again or the server could not be found!", "Error Connected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Beispiel #9
0
 public async void SendAddFriendToServer()
 {
     try
     {
         byte[]       tempbuff = Encoding.UTF8.GetBytes("PENDING%" + FrmMain.me.Id + "%" + user.Id);
         SmallPackage package  = new SmallPackage(0, 1024, "M", tempbuff, "0");
         FrmMain.server.GetStream().WriteAsync(package.Packing(), 0, package.Packing().Length);
     }
     catch (Exception ex)
     {
         MessageBox.Show("Please check the connection again or the server could not be found!", "Error Connected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }
 private void pictureBoxdeletemember_Click(object sender, EventArgs e)
 {
     try
     {
         byte[]       buffer       = Encoding.UTF8.GetBytes("KICKMEMBER%" + group.ID + "%" + user.Id);
         SmallPackage smallPackage = new SmallPackage(0, 1024, "M", buffer, "Server");
         FrmMain.server.GetStream().WriteAsync(smallPackage.Packing(), 0, smallPackage.Packing().Length);
         group.RemoveMember(user.Id);
         this.Dispose();
     }
     catch (Exception ex)
     {
         MessageBox.Show("Please check the connection again or the server could not be found!", "Error Connected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }
Beispiel #11
0
 private void radioButton2_CheckedChanged(object sender, EventArgs e)
 {
     try
     {
         FrmMain.theme.Black();
         parent.ChangeTheme();
         byte[]       tempbuff       = Encoding.UTF8.GetBytes("THEME%" + (FrmMain.theme.IsWhite == true ? "White" : "Black"));
         SmallPackage packageReceive = new SmallPackage(1024, tempbuff.Length, "M", tempbuff, "0");
         FrmMain.server.GetStream().WriteAsync(packageReceive.Packing(), 0, packageReceive.Packing().Length);
     }
     catch (Exception ex)
     {
         MessageBox.Show("Please check the connection again or the server could not be found!", "Error Connected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }
 private async void SendAcceptGroupInviteToServer()
 {
     try
     {
         // goi len server gui thanh vien ve
         byte[] tempbuff = Encoding.UTF8.GetBytes("GROUPACCEPT%" + uiParent.group.ID + "%" + uiParent.group.Name + "%"
                                                  + FrmMain.me.Id + "%" + FrmMain.me.Name);
         SmallPackage package = new SmallPackage(0, 1024, "M", tempbuff, "0");
         FrmMain.server.GetStream().WriteAsync(package.Packing(), 0, package.Packing().Length);
         pnParent.Controls.Remove(this);
     }
     catch (Exception ex)
     {
         MessageBox.Show("Please check the connection again or the server could not be found!", "Error Connected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }
Beispiel #13
0
        private async void button1_Click(object sender, EventArgs e)
        {
            if (kiemtradulieu() == false)
            {
                MessageBox.Show("Vui long nhap du lieu day du!");
            }
            if (kiemtramatkhau() == false)
            {
                MessageBox.Show("Mat khau khong trung khop!");
                txtNhaplaimatkhau.PasswordChar = '\0';
                txtNhaplaimatkhau.Text         = "Nhap lai mat khau";
            }
            else
            {
                try
                {
                    UserManager.UserVerification userVerification = new UserManager.UserVerification();
                    string pass = userVerification.GetSHA256(txtMatkhau.Text);

                    byte[]       tempbuff = Encoding.UTF8.GetBytes("SIGNUP%" + this.txtTendangnhap.Text + "%" + pass);
                    SmallPackage package  = new SmallPackage(0, 1024, "M", tempbuff, "0");
                    await FrmLogin.server.GetStream().WriteAsync(package.Packing(), 0, package.Packing().Length);

                    byte[] buffReceive = new byte[1024];
                    await FrmLogin.server.GetStream().ReadAsync(buffReceive, 0, buffReceive.Length);

                    SmallPackage packageReceive = new SmallPackage();
                    packageReceive.DividePackage(buffReceive);

                    string[] data = (Encoding.UTF8.GetString(packageReceive.Data).Trim('\0', '\t', '\n')).Split('%');
                    if (data[0].Trim('\0', '\r', '\n') == "SIGNUPOKE")
                    {
                        MessageBox.Show("SIGN UP successfully");
                        this.login.Show();
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Tai khoan da ton tai");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Please check the connection again or the server could not be found!", "Error Connected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }
Beispiel #14
0
        private void CheckPasswordFromServer()
        {
            try
            {
                UserVerification verification = new UserVerification();

                byte[] tempBuff = Encoding.UTF8.GetBytes(string.Format("CHECKPASS%{0}%{1}%{2}",
                                                                       FrmMain.me.Id,
                                                                       verification.GetSHA256(txtOldPassword.Text),
                                                                       verification.GetSHA256(txtNewPassword.Text)));
                SmallPackage smallPackage = new SmallPackage(0, 1024, "M", tempBuff, "Server");
                FrmMain.server.GetStream().WriteAsync(smallPackage.Packing(), 0, smallPackage.Packing().Length);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Please check the connection again or the server could not be found!", "Error Connected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Beispiel #15
0
        private async void SendVoiceToServer()
        {
            try
            {
                if (voice.GetWave() == null)
                {
                    return;
                }
                voice.DisposeAll();

                byte[] data = File.ReadAllBytes(voice.Path);
                Guid   id   = Guid.NewGuid();
                if (user != null)
                {
                    byte[] tempBuff = Encoding.UTF8.GetBytes(string.Format("STARTSENDVOICE%{0}%{1}%{2}%{3}",
                                                                           user.Id,
                                                                           data.Length,
                                                                           id.ToString(), "Private"));
                    SmallPackage smallPackage = new SmallPackage(0, 1024, "M", tempBuff, "0");
                    FrmMain.server.GetStream().WriteAsync(smallPackage.Packing(), 0, smallPackage.Packing().Length);
                    FrmMain.client.SendFileToServer(data, "V", id.ToString());

                    userForm.AddVoiceMessage(FrmMain.me, voice.Path);
                }
                else
                {
                    byte[] tempBuff = Encoding.UTF8.GetBytes(string.Format("STARTSENDVOICE%{0}%{1}%{2}%{3}",
                                                                           group.ID,
                                                                           data.Length,
                                                                           id.ToString(), "Public"));
                    SmallPackage smallPackage = new SmallPackage(0, 1024, "M", tempBuff, "0");
                    FrmMain.server.GetStream().WriteAsync(smallPackage.Packing(), 0, smallPackage.Packing().Length);
                    FrmMain.client.SendFileToServer(data, "V", id.ToString());

                    groupForm.AddVoiceMessage(FrmMain.me, voice.Path);
                }
                // Process when record sent
                voice.Path = voice.GetNextPath();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Please check the connection again or the server could not be found!", "Error Connected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Beispiel #16
0
        private void pictureDelete_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dialogResult = MessageBox.Show("Are you sure remove this contact!", "Remove Contact", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    byte[]       buff         = Encoding.UTF8.GetBytes("DELETECONTACT%" + this._ID);
                    SmallPackage smallPackage = new SmallPackage(0, 1024, "M", buff, "Server");
                    FrmMain.server.GetStream().WriteAsync(smallPackage.Packing(), 0, smallPackage.Packing().Length);

                    _RemoveThis();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Please check the connection again or the server could not be found!", "Error Connected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        public async void SendRequest(string account, string pass, string action)
        {
            try
            {
                UserManager.UserVerification userVerification = new UserManager.UserVerification();
                pass = userVerification.GetSHA256(pass);
                byte[]       tempBuff    = Encoding.UTF8.GetBytes(action + "%" + account + "%" + pass);
                SmallPackage package     = new SmallPackage(1024, 1024, "M", tempBuff, "0");
                byte[]       buffMessage = package.Packing();
                await server.GetStream().WriteAsync(buffMessage, 0, buffMessage.Length);

                byte[] buffReceive = new byte[1024];
                await server.GetStream().ReadAsync(buffReceive, 0, buffReceive.Length);

                SmallPackage packageReceive = new SmallPackage();
                packageReceive.DividePackage(buffReceive);

                string[] data = (Encoding.UTF8.GetString(packageReceive.Data).Trim('\0', '\t', '\n')).Split('%');
                if (data[0] == "LOGINOKE")
                {
                    User    user     = new User(data[1], account, true, @"./images/avatarDefault/avatarDefault.png");
                    FrmMain mainform = new FrmMain(this, user, client, server, data[2]);
                    mainform.Show();
                    this.Hide();
                    label2.Visible = false;
                }
                else if (data[0] == "ERRORLOGINED")
                {
                    MessageBox.Show("Account was logined in other computer", "Login error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    label2.Text    = "Tai khoan hoac mat khau khong chinh xac";
                    label2.Visible = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Please check the connection again or the server could not be found!", "Error Connected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.lnConnect.Text = "No connection";
            }
        }
        private void label3_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult rs = MessageBox.Show("Are you sure Out this Group", "Remove Group", MessageBoxButtons.YesNo);
                if (rs == DialogResult.Yes)
                {
                    byte[]       buff         = Encoding.UTF8.GetBytes("OUTGR%" + group.ID + "%" + (group.admin == FrmMain.me ? "true" : "false") + "%" + FrmMain.me.Id);
                    SmallPackage smallPackage = new SmallPackage(0, 1024, "M", buff, "0");
                    FrmMain.server.GetStream().WriteAsync(smallPackage.Packing(), 0, smallPackage.Packing().Length);

                    frmMain.GroupUI.Dispose();
                    mainForm.GroupUIs.Remove(frmMain.GroupUI);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Please check the connection again or the server could not be found!", "Error Connected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Beispiel #19
0
 private async void pictureBox2_Click(object sender, EventArgs e)
 {
     try
     {
         if (ucParent != null)
         {
             fileId = ucParent.ID;
         }
         byte[]       tempbuff = Encoding.UTF8.GetBytes("SENDFILE%" + fileId + "%" + fileName);
         SmallPackage package  = new SmallPackage(0, 1024, "M", tempbuff, "0");
         FrmMain.server.GetStream().WriteAsync(package.Packing(), 0, package.Packing().Length);
         PictureBox temp = sender as PictureBox;
         //temp.Image = Image.FromFile(@"..\..\images\check.png");
         //temp.Enabled = false;
     }
     catch (Exception ex)
     {
         MessageBox.Show("Please check the connection again or the server could not be found!", "Error Connected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }
Beispiel #20
0
 private void label3_Click(object sender, EventArgs e)
 {
     try
     {
         var temp = MessageBox.Show("Remove Friend", "Remove Friend", MessageBoxButtons.YesNo);
         if (temp == DialogResult.Yes)
         {
             byte[]       tempbuff = Encoding.UTF8.GetBytes("REMOVEFRIEND%" + FrmMain.me.Id + "%" + user.Id);
             SmallPackage package  = new SmallPackage(0, 1024, "M", tempbuff, "0");
             FrmMain.server.GetStream().WriteAsync(package.Packing(), 0, package.Packing().Length);
         }
         FrmMain.frmFriend.RemoveFriend(frmMain.userUI);
         frmMain.userUI.DisableRemove();
         frmMain.userUI.EnableADD();
         this.user.IsFriend = false;
     }
     catch (Exception ex)
     {
         MessageBox.Show("Please check the connection again or the server could not be found!", "Error Connected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }
        public async Task SendMessage()
        {
            try
            {
                if (TextBoxEnterChat.Text.Trim() != "")
                {
                    byte[] tempbuff = Encoding.UTF8.GetBytes("GSEND%" + group.ID + "%" +
                                                             FrmMain.me.Id + "%" +
                                                             this.TextBoxEnterChat.Text);
                    SmallPackage package = new SmallPackage(0, 1024, "M", tempbuff, "0");
                    FrmMain.server.GetStream().WriteAsync(package.Packing(), 0, package.Packing().Length);

                    this.AddItemToListChat(FrmMain.me, "-1", this.TextBoxEnterChat.Text);
                    this.GroupUI.AddMessageIntoInteract(FrmMain.me.Name, TextBoxEnterChat.Text);
                    TextBoxEnterChat.Text = string.Empty;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Please check the connection again or the server could not be found!", "Error Connected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        private async void lbSave_Click(object sender, EventArgs e)
        {
            DialogResult ds = MessageBox.Show("Are you sure change this message", "Edit Message", MessageBoxButtons.YesNo);

            if (ds == DialogResult.Yes)
            {
                try
                {
                    byte[]       tempbuff = Encoding.UTF8.GetBytes("EDITMESSAGE%" + pnparent.ID + "%" + pnparent.IDParent + "%" + textBox1.Text);
                    SmallPackage package  = new SmallPackage(0, 1024, "M", tempbuff, "0");
                    FrmMain.server.GetStream().WriteAsync(package.Packing(), 0, package.Packing().Length);
                    ucmessshow.Visible = true;
                    ucmessshow.SetText(textBox1.Text);
                    pnparent._RemoveEditControls(this);
                    pnparent.isTurnOnEdit = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Please check the connection again or the server could not be found!", "Error Connected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }
Beispiel #23
0
 public async Task SendMessage()
 {
     try
     {
         if (TextBoxEnterChat.Text != "")
         {
             // Gửi tin nhắn qua server
             byte[]       tempbuff = Encoding.UTF8.GetBytes("SEND%" + FrmMain.me.Id + "%" + user.Id + "%" + this.TextBoxEnterChat.Text);
             SmallPackage package  = new SmallPackage(0, 1024, "M", tempbuff, "0");
             FrmMain.server.GetStream().WriteAsync(package.Packing(), 0, package.Packing().Length);
             // tạo một panel chat
             this.AddItemInToListChat(FrmMain.me, "-1", this.TextBoxEnterChat.Text);
             this.userUI.AddMessageIntoInteract(FrmMain.me.Name, TextBoxEnterChat.Text);
             //clear textbox nhập chat
             TextBoxEnterChat.Text = "";
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("Please check the connection again or the server could not be found!", "Error Connected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }
        private void picDelete_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult temp = MessageBox.Show("Are you sure delete this message", "Delete Message", MessageBoxButtons.OKCancel);
                if (temp == DialogResult.OK)
                {
                    if (this.User.Id != FrmMain.me.Id)
                    {
                        this.Visible = false;
                    }
                    else if (isFile)
                    {
                        byte[]       tempbuff = Encoding.UTF8.GetBytes("DELETEMESSAGE%" + this.ID + "%" + IDParent);
                        SmallPackage package  = new SmallPackage(0, 1024, "M", tempbuff, "0");
                        FrmMain.server.GetStream().WriteAsync(package.Packing(), 0, package.Packing().Length);
                        this.ucfileshow.DeleteMessage();
                        this.DisableEdit();
                        this.DisableDelete();
                    }
                    else
                    {
                        byte[]       tempbuff = Encoding.UTF8.GetBytes("DELETEMESSAGE%" + this.ID + "%" + IDParent);
                        SmallPackage package  = new SmallPackage(0, 1024, "M", tempbuff, "0");
                        FrmMain.server.GetStream().WriteAsync(package.Packing(), 0, package.Packing().Length);
                        this.ucmessshow.DeleteMessage();
                        this.DisableEdit();
                        this.DisableDelete();
                    }

                    acceptFocus = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Please check the connection again or the server could not be found!", "Error Connected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Beispiel #25
0
        private async Task AwaitReadData()
        {
            try
            {
                byte[]         tempBuff;
                SmallPackage   package;
                List <Package> listAwaitPackage = new List <Package>();
                while (true)
                {
                    // Nhận Gói Tin
                    byte[] buff    = new byte[1024];
                    int    nReturn = await server.GetStream().ReadAsync(buff, 0, buff.Length);

                    package = new SmallPackage();
                    package.DividePackage(buff);

                    if (package.Style == "M")
                    {
                        string[] data = (System.Text.Encoding.UTF8.GetString(package.Data).Trim('\0', '\t', '\r', '\n')).Split('%');
                        // Xử lí gói tin

                        string action = data[0];
                        // Tìm hành động
                        if (action == "LOADUSERDATA")
                        {
                            for (int i = 1; i < data.Length; i++)
                            {
                                if (data[i] == "")
                                {
                                    break;
                                }

                                string[] arr  = data[i].Split(' ');
                                string   path = @"./images/avatarDefault/avatarDefault.png";
                                if (arr[1] != me.Name)
                                {
                                    listUser.Add(new User(arr[0], arr[1], bool.Parse(arr[2]), path));
                                    UserUIs.Add(new UserUI(listUser[listUser.Count - 1], this));
                                }
                            }
                            serverUsersForm.LoadListAllUser();
                        }
                        else if (action == "LOADCONTACTBOOK")
                        {
                            for (int j = 1; j < data.Length; j++)
                            {
                                string[]    arr            = data[j].Split('•');
                                ContactBook newContactBook = new ContactBook(arr[0], arr[1]);
                                for (int i = 2; i < arr.Length; i++)
                                {
                                    foreach (var item3 in UserUIs)
                                    {
                                        if (item3.user.Id == arr[i])
                                        {
                                            newContactBook._AddMember(item3.user);
                                            break;
                                        }
                                    }
                                }
                                frmContactBook._AddContactBook(newContactBook);
                            }
                            time = true;
                        }
                        else if (action == "MESSAGE")                         // MESSAGE +id tin nhan+ tin nhắn + Id người gửi
                        {
                            string temp    = Encoding.UTF8.GetString(package.Data).Trim('\0', '\t', '\n');
                            string Message = "";
                            int    count   = 0;
                            for (int j = 0; j < temp.Length; j++)
                            {
                                if (count >= 3)
                                {
                                    Message = Message + temp[j];
                                }
                                else
                                if (temp[j] == '%')
                                {
                                    count++;
                                }
                            }
                            for (int i = 0; i < UserUIs.Count; i++)
                            {
                                if (UserUIs[i].GetId() == data[2])
                                {
                                    UserUIs[i].AddMessage(data[1], Message);
                                    UserUIs[i].BringToTop();
                                    break;
                                }
                            }
                        }
                        else if (action == "ADDUSER")
                        {
                            string path     = @"./images/avatarDefault/avatarDefault.png";
                            User   tempUser = (new User(data[1], data[2], false, path));
                            UserUI temp     = new UserUI(tempUser, this);
                            listUser.Add(tempUser);
                            UserUIs.Add(temp);
                            serverUsersForm._AddNewUser(temp);
                        }
                        else if (action == "ONLINE")
                        {
                            for (int i = 0; i < UserUIs.Count; i++)
                            {
                                if (UserUIs[i].GetId() == data[1])
                                {
                                    UserUIs[i].SetStatus(true);
                                    UserUIs[i].ChangeStatusOnline();
                                    break;
                                }
                            }
                        }
                        else if (action == "OFFLINE")
                        {
                            for (int i = 0; i < UserUIs.Count; i++)
                            {
                                if (UserUIs[i].GetId() == data[1])
                                {
                                    UserUIs[i].SetStatus(false);
                                    UserUIs[i].ChangeStatusOffline();
                                    break;
                                }
                            }
                        }
                        else if (action == "FILE")
                        {
                            Package awaitPackage = new Package(data[1], me.Id, 0, int.Parse(data[2]),
                                                               "F", data[3], data[4], data[5], false);
                            listAwaitPackage.Add(awaitPackage);
                        }
                        else if (action == "TEMPFILE")                         //("TEMPFILE%" + Createid.ToString() + "%" + client.id_ + "%" + infoByte.Name);
                        {
                            string tempFileId     = data[1];
                            string tempidNguoiGui = data[2];
                            string tempFileName   = data[3];
                            if (data[4] == "Private")
                            {
                                for (int i = 0; i < FrmMain.UserUIs.Count; i++)
                                {
                                    if (UserUIs[i].GetId() == tempidNguoiGui)
                                    {
                                        UserUIs[i].AddPanelFile(tempFileId, tempFileName);
                                        UserUIs[i].BringToTop();
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                for (int i = 0; i < GroupUIs.Count; i++)
                                {
                                    if (GroupUIs[i].group.ID == data[5])
                                    {
                                        foreach (var item in listUser)
                                        {
                                            if (item.Id == tempidNguoiGui)
                                            {
                                                GroupUIs[i].AddPanelFile(item, tempFileId, tempFileName);
                                                GroupUIs[i].BringToTop();
                                                break;
                                            }
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                        else if (action == "CHECKPASS")
                        {
                            if (data[1] == "YES")
                            {
                                settingForm.RespondToChangePasswordMessage(true);
                            }
                            else
                            {
                                settingForm.RespondToChangePasswordMessage(false);
                            }
                        }
                        else if (action == "AVATAR")
                        {
                            Package awaitPackage = new Package("0", me.Id, 0, int.Parse(data[2]),
                                                               "A", data[1], data[3], data[4], false);
                            listAwaitPackage.Add(awaitPackage);
                        }
                        else if (action == "PENDING")
                        {
                            foreach (var item in UserUIs)
                            {
                                if (item.user.Id == data[1])
                                {
                                    serverUsersForm.AddPending(item);
                                    serverUsersForm.EnablePointPending();
                                    picNotification.Visible = true;
                                    break;
                                }
                            }
                        }
                        else if (action == "FRIEND")
                        {
                            for (int i = 1; i < data.Length; i++)
                            {
                                if (data[i] == "")
                                {
                                    break;
                                }
                                foreach (var item in UserUIs)
                                {
                                    if (item.user.Id == data[i])
                                    {
                                        item.user.IsFriend = true;
                                        item.EnableRemove();
                                        item.DisableADD();
                                    }
                                }
                            }
                        }
                        else if (action == "ACCEPTFRIEND")
                        {
                            foreach (var item in UserUIs)
                            {
                                if (item.user.Id == data[1])
                                {
                                    AddUserIntoFrmFriend(item);
                                    item.user.IsFriend = true;
                                    item.DisableADD();
                                    item.EnableRemove();
                                    break;
                                }
                            }
                        }
                        else if (action == "REMOVEFRIEND")
                        {
                            foreach (var item in UserUIs)
                            {
                                if (item.user.Id == data[1])
                                {
                                    frmFriend.RemoveFriend(item);
                                    item.user.IsFriend = false;
                                    item.EnableADD();
                                    item.DisableRemove();
                                    break;
                                }
                            }
                        }
                        else if (action == "EDITMESSAGE")
                        {
                            foreach (var item in UserUIs)
                            {
                                if (item.user.Id == data[2])
                                {
                                    item.EditMessage(data[1], data[3]);
                                    break;
                                }
                            }
                        }
                        else if (action == "EDITGROUPMESSAGE")
                        {
                            foreach (var item in GroupUIs)
                            {
                                if (item.group.ID == data[2])
                                {
                                    item.EditMessage(data[1], data[3]);
                                    break;
                                }
                            }
                        }
                        else if (action == "DELETEMESSAGE")
                        {
                            foreach (var item in UserUIs)
                            {
                                if (item.user.Id == data[2])
                                {
                                    item.DeleteMessage(data[1]);
                                    break;
                                }
                            }
                        }
                        else if (action == "DELETEGROUPMESSAGE")
                        {
                            foreach (var item in GroupUIs)
                            {
                                if (item.group.ID == data[2])
                                {
                                    item.DeleteMessage(data[1]);
                                    break;
                                }
                            }
                        }
                        else if (action == "IDMESS")
                        {
                            listMessAwaitID[0].ID = data[1];
                            listMessAwaitID.Remove(listMessAwaitID[0]);
                        }
                        else if (action == "IDFILE")
                        {
                            listFileAwaitID[0].ID = data[1];
                            listFileAwaitID.Remove(listFileAwaitID[0]);
                        }
                        else if (action == "LOADGROUPDATA")
                        {
                            for (int i = 1; i < data.Length; i++)
                            {
                                if (data[i] == "")
                                {
                                    break;
                                }

                                string[] arr  = data[i].Split('•');
                                string   path = @"./images/avatarDefault/groupDefault.png";
                                listGroup.Add(new Group(arr[0], arr[1], path));
                                listGroup[listGroup.Count - 1].AddMember(me);
                                for (int j = 3; j < arr.Length; j += 2)
                                {
                                    foreach (var item in listUser)
                                    {
                                        if (arr[j] == item.Id)
                                        {
                                            listGroup[listGroup.Count - 1].AddMember(item);
                                            if (item.Id == arr[2])
                                            {
                                                listGroup[listGroup.Count - 1].admin = item;
                                            }
                                            break;
                                        }
                                    }
                                }
                                if (arr[2] == me.Id)
                                {
                                    listGroup[listGroup.Count - 1].admin = me;
                                }
                                GroupUIs.Add(new GroupUI(listGroup[listGroup.Count - 1], this));
                            }
                        }
                        else if (action == "GPENDING")
                        {
                            GroupUI temp = new GroupUI(new Group(data[1], data[2]), this);
                            serverUsersForm.AddGroupPending(temp);
                            serverUsersForm.EnablePointPending();
                            picNotification.Visible = true;
                        }
                        else if (action == "GROUPDATA")
                        {
                            string[] arr   = data[1].Split('•');
                            Group    group = new Group(arr[0], arr[1]);
                            string   admin = arr[2];

                            for (int j = 3; j < arr.Length; j += 2)
                            {
                                foreach (var item in listUser)
                                {
                                    if (arr[j] == item.Id)
                                    {
                                        group.AddMember(item);
                                        if (admin == item.Id)
                                        {
                                            group.admin = item;
                                        }
                                        break;
                                    }
                                }
                            }
                            group.AddMember(me);
                            if (arr[2] == me.Id)
                            {
                                group.admin = me;
                            }
                            listGroup.Add(group);
                            GroupUIs.Add(new GroupUI(group, this));
                        }
                        else if (action == "NEWMEMBER")
                        {
                            foreach (var item in GroupUIs)
                            {
                                if (item.group.ID == data[1])
                                {
                                    foreach (var item2 in listUser)
                                    {
                                        if (data[2] == item2.Id)
                                        {
                                            item.group.AddMember(item2);
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        else if (action == "GSEND")
                        {
                            string temp    = Encoding.UTF8.GetString(package.Data).Trim('\0', '\t', '\n');
                            string Message = "";
                            int    count   = 0;
                            for (int j = 0; j < temp.Length; j++)
                            {
                                if (count >= 4)
                                {
                                    Message = Message + temp[j];
                                }
                                else
                                if (temp[j] == '%')
                                {
                                    count++;
                                }
                            }
                            for (int i = 0; i < GroupUIs.Count; i++)
                            {
                                if (GroupUIs[i].group.ID == data[1])
                                {
                                    foreach (User user in listUser)
                                    {
                                        if (user.Id == data[2])
                                        {
                                            GroupUIs[i].AddMessage(user, data[3], Message);
                                            GroupUIs[i].BringToTop();
                                            break;
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                        else if (action == "OUTGR")
                        {
                            string IDGr     = data[1];
                            string IDmember = data[2];
                            foreach (var item in this.GroupUIs)
                            {
                                if (item.group.ID == IDGr)
                                {
                                    if (IDmember == me.Id)
                                    {
                                        GroupUIs.Remove(item);
                                        listGroup.Remove(item.group);
                                        item.Dispose();
                                    }
                                    else
                                    {
                                        item.group.RemoveMember(IDmember);
                                    }
                                    break;
                                }
                            }
                        }
                        else if (action == "CREATEGRERROR")
                        {
                            MessageBox.Show("Create Group Fail! Please check your name group or connection", "Create Error", MessageBoxButtons.OK);
                        }
                        else if (action == "CREATEGROKE")
                        {
                            MessageBox.Show("Create Group successfully!", "Create Group", MessageBoxButtons.OK);
                        }
                        else if (action == "CHANGEHOST")
                        {
                            string IDgr    = data[1];
                            string newHost = data[2];

                            foreach (var item in GroupUIs)
                            {
                                if (item.group.ID == data[1])
                                {
                                    if (me.Id == data[2])
                                    {
                                        item.group.admin = me;
                                    }
                                    else
                                    {
                                        item.group.ChangeHost(newHost);
                                    }
                                    break;
                                }
                            }
                        }
                        else if (action == "CREATECBERRORNAME")
                        {
                            MessageBox.Show("Contact name has exists!", "Erorr Create Contact", MessageBoxButtons.OK);
                        }
                        else if (action == "CREATECBSUCCES")
                        {
                            MessageBox.Show("Create contact successfully!", "Create Contact", MessageBoxButtons.OK);
                        }
                        else if (action == "NEWCB")
                        {
                            ContactBook newContactBook = new ContactBook(data[1], data[2]);
                            frmContactBook._AddContactBook(newContactBook);
                        }
                        else if (action == "VOICE")
                        {
                            Package awaitPackage;
                            if (data[2][0] != 'G')
                            {
                                awaitPackage = new Package(data[1], FrmMain.me.Id, 0, int.Parse(data[2]), "V", "NULL",
                                                           ".wav", data[3], true);
                                listAwaitPackage.Add(awaitPackage);
                            }
                            else
                            {
                                awaitPackage = new Package(data[1], data[2], 0, int.Parse(data[3]), "V", "NULL",
                                                           ".wav", data[4], false);
                                listAwaitPackage.Add(awaitPackage);
                            }
                        }
                        else if (action == "REMOVEUSER")
                        {
                            User temp = new User();
                            foreach (var item in listUser)
                            {
                                if (item.Id == data[1])
                                {
                                    temp = item;
                                    break;
                                }
                            }
                            foreach (var item in GroupUIs)
                            {
                                if (item.group.MemberInGroup(temp))
                                {
                                    item.group.RemoveMember(temp.Id);
                                }
                            }
                            foreach (var item in UserUIs)
                            {
                                if (item.user.Id == temp.Id)
                                {
                                    UserUIs.Remove(item);
                                    item.Dispose();
                                    break;
                                }
                            }
                            frmContactBook._RemoveUser(data[1]);
                            listUser.Remove(temp);
                        }
                    }
                    else if (package.Style == "F")
                    {
                        foreach (var item in listAwaitPackage)
                        {
                            if (package.ID == item.IDpackage)
                            {
                                if (item.Ack + package.Data.Length > item.Length)
                                {
                                    byte[] tempBuffer = new byte[item.Length - item.Ack];
                                    Buffer.BlockCopy(package.Data, 0, tempBuffer, 0, item.Length - item.Ack);
                                    package.Data = new byte[item.Length - item.Ack];
                                    tempBuffer.CopyTo(package.Data, 0);
                                }
                                package.Data.CopyTo(item.Data, item.Ack);
                                item.Ack = item.Ack + package.Data.Length;
                                if (item.Ack == item.Length)
                                {
                                    _FileDialog fd = new _FileDialog();
                                    fd.SaveFile(item.Data, item.FileName);
                                }
                            }
                        }
                    }
                    else if (package.Style == "A")
                    {
                        foreach (var item in listAwaitPackage)
                        {
                            if (package.ID == item.IDpackage)
                            {
                                if (item.Ack + package.Data.Length > item.Length)
                                {
                                    byte[] tempBuffer = new byte[item.Length - item.Ack];
                                    Buffer.BlockCopy(package.Data, 0, tempBuffer, 0, item.Length - item.Ack);
                                    package.Data = new byte[item.Length - item.Ack];
                                    tempBuffer.CopyTo(package.Data, 0);
                                }
                                package.Data.CopyTo(item.Data, item.Ack);
                                item.Ack = item.Ack + package.Data.Length;
                                if (item.Ack == item.Length)
                                {
                                    string path = @"./cache/avatar/" + item.FileName + item.Extension;
                                    try
                                    {
                                        File.WriteAllBytes(path, item.Data);
                                    }
                                    catch (Exception ex)
                                    {
                                    }
                                    foreach (var item2 in UserUIs)
                                    {
                                        if (item2.user.Id == item.FileName)
                                        {
                                            item2.SetAvatar(path);

                                            break;
                                        }
                                        if (item.FileName == me.Id)
                                        {
                                            this.SetAvatar(path);
                                            this.LoadUser();
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else if (package.Style == "V")
                    {
                        foreach (var item in listAwaitPackage)
                        {
                            if (package.ID == item.IDpackage)
                            {
                                if (item.Ack + package.Data.Length > item.Length)
                                {
                                    byte[] tempBuffer = new byte[item.Length - item.Ack];
                                    Buffer.BlockCopy(package.Data, 0, tempBuffer, 0, item.Length - item.Ack);
                                    package.Data = new byte[item.Length - item.Ack];
                                    tempBuffer.CopyTo(package.Data, 0);
                                }
                                package.Data.CopyTo(item.Data, item.Ack);
                                item.Ack = item.Ack + package.Data.Length;
                                if (item.Ack == item.Length)
                                {
                                    if (item.isPrivate)
                                    {
                                        foreach (var userUI in UserUIs)
                                        {
                                            if (userUI.user.Id == item.IDsend)
                                            {
                                                string path = string.Format(@"./voice/{0}/", item.IDsend);
                                                if (!Directory.Exists(path))
                                                {
                                                    Directory.CreateDirectory(path);
                                                }
                                                path += string.Format("{0}.wav", GetIDForIncomingVoice(path));
                                                File.WriteAllBytes(path, item.Data);
                                                userUI.userForm.AddVoiceMessage(userUI.user, path);
                                                userUI.BringToTop();
                                                break;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        foreach (var groupUI in GroupUIs)
                                        {
                                            if ("G" + groupUI.group.ID == item.IDreceive)
                                            {
                                                string path = string.Format(@"./voice/{0}/", item.IDreceive);
                                                if (!Directory.Exists(path))
                                                {
                                                    Directory.CreateDirectory(path);
                                                }
                                                path += string.Format("{0}.wav", GetIDForIncomingVoice(path));
                                                File.WriteAllBytes(path, item.Data);
                                                foreach (var userUI in UserUIs)
                                                {
                                                    if (userUI.user.Id == item.IDsend)
                                                    {
                                                        groupUI.groupForm.AddVoiceMessage(userUI.user, path);
                                                        groupUI.BringToTop();
                                                        break;
                                                    }
                                                }
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Server disconnected!", "Error Connected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                acceptClose = false;
                this.Close();
            }
        }
Beispiel #26
0
 private async void LoadGroupData()
 {
     byte[]       tempbuff       = Encoding.UTF8.GetBytes("LOADGROUPDATA%" + me.Id);
     SmallPackage packageReceive = new SmallPackage(0, 1024, "M", tempbuff, "0");
     await server.GetStream().WriteAsync(packageReceive.Packing(), 0, packageReceive.Packing().Length);
 }
Beispiel #27
0
 private async void LoadContactBook()
 {
     byte[]       tempbuff       = Encoding.UTF8.GetBytes("LOADCONTACTBOOK%");
     SmallPackage packageReceive = new SmallPackage(0, 1024, "M", tempbuff, "0");
     await server.GetStream().WriteAsync(packageReceive.Packing(), 0, packageReceive.Packing().Length);
 }