Ejemplo n.º 1
0
        //hàm chính trong tiểu trình nhận thông điệp
        public void bw_receive_DoWork(object sender, DoWorkEventArgs e)
        {
            DataTable dt_friend=null;
            while (socket.Connected)
            {
                try
                {
                    //lưu thong tin tài khoản đăng kí
                    string accountTemp;
                    string usernameTemp;
                    string passwordTemp;
                    string emailTemp;
                    //end
                    byte[] buffer = new byte[4];
                    byte[] data;//lưu byte của tin nhắn dạng chữ
                    byte[] dataPicture = new byte[10000];
                    int lengh;
                    stream.Read(buffer, 0, 4);
                    CommandType_ cmt;
                    cmt = (CommandType_)BitConverter.ToInt32(buffer, 0);
                    //if client vừa kết nối muốn đăng nhập
                    if (cmt == CommandType_.Login)
                    {
                        buffer = new byte[4];
                        stream.Read(buffer, 0, 4);
                        lengh = BitConverter.ToInt32(buffer, 0);

                        data = new byte[lengh];
                        stream.Read(data, 0, lengh);
                        accountTemp = Encoding.ASCII.GetString(data);
                        stream.Read(buffer, 0, 4);
                        lengh = BitConverter.ToInt32(buffer, 0);
                        data = new byte[lengh];
                        stream.Read(data, 0, lengh);
                        passwordTemp = Encoding.ASCII.GetString(data);
                        //kiểm tra xem trong database có đúng tài khoản mật khẩu hay ko
                        DataTable dt = new DataTable();
                        dt = Khachhang_BUS.Dangnhap(accountTemp, passwordTemp);
                        if (dt.Rows.Count > 0)
                        {
                            //nếu đúng thì lấy username của tài khoản vừa đang nhâp
                            this.userName = dt.Rows[0][1].ToString();
                            //kiểm tra client này đã có kết nối chưa
                            if (IsOnline(this.userName) == null)
                            {
                                //đăng nhập thành công và gởi danh sách bạn
                                Command cmd = new Command(CommandType_.LoginSuccess, dt);
                                SendCommand(cmd);
                                checkDangNhapSuccess = true;

                                //nếu chưa kết nối5
                                Online(this.userName);
                                ////lưu client vào List  client
                                Form1.listClient.Add(this);
                            }
                            else
                            {
                                //tài khoản này đã được đăng nhập bởi người khác
                                Command cmd = new Command(CommandType_.NameExists);
                                this.SendCommand(cmd);
                            }
                        }
                        else
                        {
                            //đăng nhập thất bại.tài khoản hoặc mật khẩu không đúng
                            Command cmd = new Command(CommandType_.Failure);
                            this.SendCommand(cmd);
                        }
                    }
                    //đăng kí 1 tài khoản mới
                    if (cmt == CommandType_.Register)
                    {
                        //đọc username
                        buffer = new byte[4];
                        stream.Read(buffer, 0, 4);
                        lengh = BitConverter.ToInt32(buffer, 0);
                        data = new byte[lengh];
                        stream.Read(data, 0, lengh);
                        usernameTemp = Encoding.ASCII.GetString(data);
                        //đọc account
                        stream.Read(buffer, 0, 4);
                        lengh = BitConverter.ToInt32(buffer, 0);
                        data = new byte[lengh];
                        stream.Read(data, 0, lengh);
                        accountTemp = Encoding.ASCII.GetString(data);
                        //đọc password
                        stream.Read(buffer, 0, 4);
                        lengh = BitConverter.ToInt32(buffer, 0);
                        data = new byte[lengh];
                        stream.Read(data, 0, lengh);
                        passwordTemp = Encoding.ASCII.GetString(data);
                        //đọc email
                        stream.Read(buffer, 0, 4);
                        lengh = BitConverter.ToInt32(buffer, 0);
                        data = new byte[lengh];
                        stream.Read(data, 0, lengh);
                        emailTemp = Encoding.ASCII.GetString(data);
                        //kiểm tra tên người dùng và tài khoản có tồn tại chưa
                        DataTable da = new DataTable();
                        da = Khachhang_BUS.Kiemtra_Dangki(accountTemp, usernameTemp);
                        if (da.Rows.Count > 0)
                        {
                            Command cmd = new Command(CommandType_.RegisterFailure);
                            this.SendCommand(cmd);
                        }
                        else
                        {
                            Image _imageTemp = (Image)Properties.Resources.ResourceManager.GetObject("_default");
                            MemoryStream ms = new MemoryStream();
                            ImageConverter imageConvert = new ImageConverter();
                            data = new byte[10000];
                            data = (byte[])imageConvert.ConvertTo(_imageTemp, typeof(byte[]));
                            //data = ms.ToArray();
                            KhachHang_DTO kh_dto = new KhachHang_DTO(accountTemp, passwordTemp, usernameTemp, data, emailTemp);
                            Khachhang_BUS.Add_kh(kh_dto);

                            Command cmd = new Command(CommandType_.RegisterSuccess);
                            this.SendCommand(cmd);
                            _form1.AddShowClient(usernameTemp);
                        }
                    }
                    if (cmt == CommandType_.LoadMessage)
                    {

                        buffer = new byte[4];
                        stream.Read(buffer, 0, 4);
                        lengh = BitConverter.ToInt32(buffer, 0);
                        data = new byte[lengh];
                        stream.Read(data, 0, lengh);
                        usernameTemp = Encoding.ASCII.GetString(data);

                        buffer = new byte[4];
                        stream.Read(buffer, 0, 4);
                        int count = BitConverter.ToInt32(buffer, 0);

                        DataTable dt = Khachhang_BUS.Load_message(count, this.userName,usernameTemp);
                        Command cmd=new Command(CommandType_.LoadMessage,dt);
                        this.SendCommand(cmd);
                    }
                    //nếu thông điệp nhận được là 1 tin nhắn trao đổi với server
                    if (cmt == CommandType_.Message)
                    {
                        //đọc nội dung tin nhắn
                        string cmdMetaData = "";
                        buffer = new byte[4];
                        if( stream.Read(buffer, 0, 4) == 0)
                        {
                            // nó read = 0 co' nghia~ la`ben kia da ngat ket noi
                            int a = 5;
                            //cái này là server
                            //ko thấy lỗi đâu
                            //bên client đọc
                        }

                        int lenght = BitConverter.ToInt32(buffer, 0);
                        byte[] metaBuffer = new byte[lenght];
                        stream.Read(metaBuffer, 0, lenght);
                        cmdMetaData = Encoding.ASCII.GetString(metaBuffer);
                        //đọc font
                        stream.Read(buffer, 0, 4);
                        lenght = BitConverter.ToInt32(buffer, 0);
                        metaBuffer = new byte[lenght];
                        stream.Read(metaBuffer, 0, lenght);
                        //convert byte sang font
                        MemoryStream s = new MemoryStream(metaBuffer);
                        BinaryFormatter bf = new BinaryFormatter();
                        Font temp = new Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular);
                        temp = (Font)bf.Deserialize(s);
                        //gọi form chat thông qua delegate
                        _form1.Invoke(new Action(delegate()
                        {
                            // nếu chưa tồn tại hoặc đã BỊ tắt thÌ khởi tạo lại
                            if (chat == null || chat.IsDisposed)
                            {
                                chat = new Chat(this);
                                chat.Show();
                            }
                        }));
                        //gởi yin nhắn đã nhận được cho form chat vừa khởi tạo để hiển thị lên form
                        chat.Receive(cmdMetaData, temp);
                    }
                    //tin nhắn trao đổi giữa những người trong danh sách bạn
                    if (cmt == CommandType_.MessageFriend)
                    {
                        buffer = new byte[4];
                        stream.Read(buffer, 0, 4);
                        lengh = BitConverter.ToInt32(buffer, 0);
                        data = new byte[lengh];
                        stream.Read(data, 0, lengh);
                        usernameTemp = Encoding.ASCII.GetString(data);

                        //đọc nội dung tin nhắn
                        string cmdMetaData = "";
                        buffer = new byte[4];
                        stream.Read(buffer, 0, 4);
                        int lenght = BitConverter.ToInt32(buffer, 0);
                        byte[] metaBuffer = new byte[lenght];
                        stream.Read(metaBuffer, 0, lenght);
                        byte[] _content = metaBuffer;
                        cmdMetaData = Encoding.ASCII.GetString(metaBuffer);
                        //đọc font
                        stream.Read(buffer, 0, 4);
                        lenght = BitConverter.ToInt32(buffer, 0);
                        metaBuffer = new byte[lenght];
                        stream.Read(metaBuffer, 0, lenght);
                        byte[] _font = metaBuffer;
                        //convert byte sang font
                        string _time=DateTime.Now.ToString();
                        Khachhang_BUS.Save_message(this.userName, usernameTemp, _content, metaBuffer,_time );

                        MemoryStream s = new MemoryStream(metaBuffer);
                        BinaryFormatter bf = new BinaryFormatter();
                        Font temp = new Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular);
                        temp = (Font)bf.Deserialize(s);
                        Command cmd = new Command(CommandType_.MessageFriend, this.userName, cmdMetaData, temp);
                        ClientManager clm = null;
                        for (int i = 0; i < Form1.listClient.Count; i++)
                        {
                            if (Form1.listClient[i].userName == usernameTemp)
                            {
                                clm = Form1.listClient[i];
                                break;
                            }
                        }
                        if (clm != null)
                        {
                            clm.SendCommand(cmd);
                        }
                    }

                    if (cmt == CommandType_.ListFriend)
                    {
                       dt_friend = Khachhang_BUS.Loadds_FRIEND(this.userName);
                       Command cmd = new Command(CommandType_.ListFriend, dt_friend);
                       this.SendCommand(cmd);
                    }
                    if(cmt==CommandType_.FindFriend)
                    {
                        buffer = new byte[4];
                        stream.Read(buffer, 0, 4);
                        lengh = BitConverter.ToInt32(buffer, 0);
                        data = new byte[lengh];
                        stream.Read(data, 0, lengh);
                        usernameTemp = Encoding.ASCII.GetString(data);
                       DataTable dt= Khachhang_BUS.FindFriend(usernameTemp);
                       //có tồn tại username này trong danh sách --trả thông tin của username này về cho client
                        if(dt.Rows.Count>0)
                        {
                            Command cmd = new Command(CommandType_.Found, dt.Rows[0][1].ToString(),dt.Rows[0][4].ToString(),(byte[])dt.Rows[0][5]);
                            this.SendCommand(cmd);
                        }
                        else
                        {
                            Command cmd = new Command(CommandType_.NotFound);
                            this.SendCommand(cmd);
                        }
                    }
                    if (cmt == CommandType_.LoadNotice)
                    {
                        DataTable dt = Khachhang_BUS.LoadNotice(this.userName);

                        Command cmd = new Command(CommandType_.LoadNotice, dt);
                        this.SendCommand(cmd);

                    }
                    if (cmt == CommandType_.AddNotice)
                    {

                        stream.Read(buffer, 0, 4);
                        int lenght = BitConverter.ToInt32(buffer, 0);
                        data = new byte[lenght];
                        stream.Read(data, 0, lenght);
                        string _userPrimary = Encoding.ASCII.GetString(data);

                        stream.Read(buffer, 0, 4);
                        lenght = BitConverter.ToInt32(buffer, 0);
                        data = new byte[lenght];
                        stream.Read(data, 0, lenght);
                        string _userReference = Encoding.ASCII.GetString(data);

                        stream.Read(buffer, 0, 4);
                        lenght = BitConverter.ToInt32(buffer, 0);
                        data = new byte[lenght];
                        stream.Read(data, 0, lenght);
                        string _type = Encoding.ASCII.GetString(data);
                        string _time= DateTime.Now.ToString();
                        NOTICE_DTO notice = new NOTICE_DTO(_userPrimary, _userReference, _type, "", _time);
                        DataTable dt= Khachhang_BUS.AddNotice(notice);
                        if (int.Parse(dt.Rows[0][0].ToString()) == 0)
                        {
                            Command cmd = new Command(CommandType_.AddFriendFailure);
                            this.SendCommand(cmd);
                        }
                        if(int.Parse( dt.Rows[0][0].ToString())==1)
                        {
                            Command cmd = new Command(CommandType_.AddNoticeFailure);
                            this.SendCommand(cmd);
                        }
                        if (int.Parse(dt.Rows[0][0].ToString()) == 2)
                        {
                            Command cmd = new Command(CommandType_.AddNoticeSuccess);
                            this.SendCommand(cmd);
                        }
                    }
                    if (cmt == CommandType_.ChangeInformation)
                    {
                        stream.Read(buffer, 0, 4);
                        int lenght = BitConverter.ToInt32(buffer, 0);
                        data = new byte[lenght];
                        stream.Read(data, 0, lenght);
                        string _email = Encoding.ASCII.GetString(data);

                        //đọc avatar
                        Image image;
                        stream.Read(buffer, 0, 4);
                        lenght = BitConverter.ToInt32(buffer, 0);
                        dataPicture = new byte[lenght];
                        ReadBigData(stream, lenght, ref dataPicture);

                        //status
                        stream.Read(buffer, 0, 4);
                        lenght = BitConverter.ToInt32(buffer, 0);
                        data = new byte[lenght];
                        stream.Read(data, 0, lenght);
                        //string _status = Encoding.ASCII.GetString(data);
                        Khachhang_BUS.Change_information(this.userName, _email, dataPicture, data);
                    }
                    if (cmt == CommandType_.AddFriend)
                    {

                        stream.Read(buffer, 0, 4);
                        int lenght = BitConverter.ToInt32(buffer, 0);
                        data = new byte[lenght];
                        stream.Read(data, 0, lenght);
                        string _userPrimary = Encoding.ASCII.GetString(data);

                        stream.Read(buffer, 0, 4);
                        lenght = BitConverter.ToInt32(buffer, 0);
                        data = new byte[lenght];
                        stream.Read(data, 0, lenght);
                        string _userReference = Encoding.ASCII.GetString(data);
                        string _time = DateTime.Now.ToString();
                        NOTICE_DTO notice = new NOTICE_DTO(_userReference, _userPrimary, "2", "", _time);
                        Khachhang_BUS.AddNotice(notice);
                        Khachhang_BUS.Add_Friend(_userPrimary, _userReference);
                        Khachhang_BUS.DeleteNotice(_userPrimary, _userReference);
                        Command cmd = new Command(CommandType_.DeleteNoticeSuccess);
                        this.SendCommand(cmd);
                        ClientManager cmTemp;
                        //gởi trạng thái của friend
                        if ((cmTemp = IsOnline(_userReference)) != null)
                        {
                            Command _cmd = new Command(CommandType_.ListFriend, Khachhang_BUS.Loadds_FRIEND(_userReference));
                            cmTemp.SendCommand(_cmd);
                        }
                    }
                    if (cmt == CommandType_.DeleteNotice)
                    {
                        stream.Read(buffer, 0, 4);
                        int lenght = BitConverter.ToInt32(buffer, 0);
                        data = new byte[lenght];
                        stream.Read(data, 0, lenght);
                        string _userPrimary = Encoding.ASCII.GetString(data);

                        stream.Read(buffer, 0, 4);
                        lenght = BitConverter.ToInt32(buffer, 0);
                        data = new byte[lenght];
                        stream.Read(data, 0, lenght);
                        string _userReference = Encoding.ASCII.GetString(data);
                        Khachhang_BUS.DeleteNotice(_userPrimary, _userReference);
                        Command cmd = new Command(CommandType_.DeleteNoticeSuccess);
                        this.SendCommand(cmd);
                    }
                }
                catch
                {
                    //kết nối bên client bị đóng
                    if (checkDangNhapSuccess)
                    {
                        Offline(this);
                        if ((dt_friend = Khachhang_BUS.Loadds_FRIEND(this.userName)) != null)
                        {
                            int _CountTemp;//lưu số lượng friend của user
                            _CountTemp = dt_friend.Rows.Count;
                            for (int i = 0; i < _CountTemp; i++)
                            {
                                ClientManager cmTemp;
                                //gởi trạng thái của friend
                                if ((cmTemp = IsOnline(dt_friend.Rows[i][0].ToString())) != null)
                                {
                                    Command _cmd = new Command(CommandType_.Offline, this.userName);
                                    cmTemp.SendCommand(_cmd);
                                }
                            }
                        }
                    }
                    socket.Close();
                }
            }
            //đóng kết nồi
            //Form1.listClient[1]._userName = "******";
        }
Ejemplo n.º 2
0
 public static DataTable Add_notice(NOTICE_DTO notice)
 {
     SqlConnection con = SQLConnectionData.KetnoiCSDL();
     SqlCommand cmd = new SqlCommand("add_notice", con);
     cmd.CommandType = CommandType.StoredProcedure;
     cmd.Parameters.Add("@userPrimary", SqlDbType.NChar, 30);
     cmd.Parameters.Add("@userReferences", SqlDbType.NChar, 30);
     cmd.Parameters.Add("@type", SqlDbType.NChar, 1);
     cmd.Parameters.Add("@content", SqlDbType.NChar, 255);
     cmd.Parameters.Add("@Time", SqlDbType.SmallDateTime);
     cmd.Parameters["@userPrimary"].Value = notice.UserPrimary;
     cmd.Parameters["@userReferences"].Value = notice.UserReference;
     cmd.Parameters["@type"].Value = notice.Type;
     cmd.Parameters["@content"].Value = notice.Content;
     cmd.Parameters["@Time"].Value = notice.Time;
     SqlDataAdapter da = new SqlDataAdapter(cmd);
     DataTable dt = new DataTable();
     da.Fill(dt);
     return dt;
 }
Ejemplo n.º 3
0
 public static DataTable AddNotice(NOTICE_DTO notice)
 {
     return NOTICE.Add_notice(notice);
 }