Exemplo n.º 1
0
        //接收线程
        private void ReceiveThread()
        {
            while (receiveThreadIsToRun)
            {
                if (udpReceiver != null)
                {
                    IPEndPoint remoteHost = new IPEndPoint(IPAddress.Any, 0);
                    try
                    {
                        byte[] Data = udpReceiver.Receive(ref remoteHost);
                        //MessageBox.Show(Encoding.Unicode.GetString(Data) + "\r\n" + remoteHost.Address.ToString() + "\r\n" + remoteHost.Port.ToString());
                        var mStream = new MemoryStream(Data);
                        var serializer = new DataContractJsonSerializer(typeof(Message));
                        Message ReadMessage = (Message)serializer.ReadObject(mStream);
                        byte[] bytes;
                        int i = 0;
                        bool BreakFlag = false;
                        switch (ReadMessage.ID)
                        {
                            //用户登入
                            case MessageID.UserCheckIn:
                                //MessageBox.Show(ReceiverIP + "|" + Hoster.ip);
                                if (ReadMessage.user.ip != Hoster.ip)
                                {
                                    bytes = Encoding.Unicode.GetBytes(MakeUserPacket(MessageID.UserBeIn));
                                    udpClient.Send(bytes, bytes.Length, ReadMessage.user.ip, PortSend);
                                }
                                else
                                {
                                    ReadMessage.user.name += "(自己)";
                                }
                                for (i = 0; i < userlist.Count; i++)
                                {
                                    if (userlist[i].ip == ReadMessage.user.ip)
                                    {
                                        break;
                                    }
                                }
                                if (i == userlist.Count)
                                {
                                    userlist.Add(ReadMessage.user);
                                    if (miniwindow != null)
                                    {
                                        miniwindow.userwindow.UpdateUser();
                                    }
                                    User_list.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new NextPrimeDelegate(UpdateUserList));
                                }
                                break;
                            //用户退出
                            case MessageID.UserCheckOut:
                                for (i = 0; i < userlist.Count; i++)
                                {
                                    if (userlist[i].ip == ReadMessage.user.ip)
                                    {
                                        userlist.RemoveAt(i);
                                    }
                                }
                                if (miniwindow != null)
                                {
                                    miniwindow.userwindow.UpdateUser();
                                }
                                User_list.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new NextPrimeDelegate(UpdateUserList));

                                //关闭已经打开的对话框
                                for (i = 0; i < talklist.Count; i++)
                                {
                                    if (talklist[i].TalkUser.ip == ReadMessage.user.ip)
                                    {
                                        talklist[i].Dispatcher.Invoke(new Action(() =>
                                        {
                                            //MessageBox.Show("用户已退出,窗口自动关闭");
                                            talklist[i].Close();
                                        }));
                                        talklist.RemoveAt(i);
                                    }
                                }
                                break;
                            //用户发来文本信息
                            case MessageID.UserSendMessage:
                                PlayMessageSound();
                                for (i = 0; i < talklist.Count; i++)
                                {
                                    this.talklist[i].Dispatcher.Invoke(new Action(() =>
                                    {
                                        if (talklist[i].Title == ReadMessage.user.ip)
                                        {
                                            talklist[i].ShowMessage(ReadMessage.content, ReadMessage.font);
                                            BreakFlag = true;
                                        }
                                    }));
                                    if (BreakFlag == true)
                                    {
                                        break;
                                    }
                                }
                                //如果没有现有对话框的话
                                if (i == talklist.Count)
                                {
                                    GetMsgList.Add(ReadMessage);
                            
                                    if (GetMsgList.Count == 1)
                                    {
                                        //闪动窗口托盘线程
                                        Thread FlashIconThread = new Thread(new ThreadStart(FlashIcon));
                                        FlashIconThread.Start();
                                    }
                                    notifyIcon.BalloonTipText = ReadMessage.user.name + "发来消息";
                                    notifyIcon.ShowBalloonTip(1000);

                                    if (this.Visibility == Visibility.Hidden && miniwindow != null)
                                    {
                                        miniwindow.FlashImage();
                                    }
                                    //MessageBox.Show(ReadMessage.user.name + "发来信息");
                                }
                                break;
                            //用户回复在线
                            case MessageID.UserBeIn:
                                userlist.Add(ReadMessage.user);
                                User_list.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new NextPrimeDelegate(UpdateUserList));
                                break;
                            //对方发送文件过来
                            case MessageID.UserSendFile:
                                PlayMessageSound();
                                for (i = 0; i < talklist.Count; i++)
                                {
                                    this.talklist[i].Dispatcher.Invoke(new Action(() =>
                                    {
                                        if (talklist[i].Title == ReadMessage.user.ip)
                                        {
                                            talklist[i].AddRecvFile(ReadMessage.file, ReadMessage.filesize);
                                            BreakFlag = true;
                                        }
                                    }));
                                    if (BreakFlag == true)
                                    {
                                        break;
                                    }
                                }
                                //如果没有现有对话框的话
                                if (i == talklist.Count)
                                {
                                    //!!!!!!!!!!!!!!!!!!!!!!!!!!多线程对待,会导致资源堵塞
                                    //Thread CreateTalkWindowToFileThread = new Thread(new ParameterizedThreadStart(CreateTalkWindiwToFile)); //创建进程
                                    //CreateTalkWindowToFileThread.SetApartmentState(ApartmentState.STA);
                                    //CreateTalkWindowToFileThread.Start(ReadMessage); 
                                    //MessageBox.Show(ReadMessage.file+"|"+ReadMessage.filesize);
                                    //保存文件信息,提示用户有文件发入
                                    GetMsgList.Add(ReadMessage);

                                    if (GetMsgList.Count == 1)
                                    {
                                        //闪动窗口托盘线程
                                        Thread FlashIconThread = new Thread(new ThreadStart(FlashIcon));
                                        FlashIconThread.Start();
                                    }
                                    notifyIcon.BalloonTipText = ReadMessage.user.name + "发来文件:" + ReadMessage.file;
                                    notifyIcon.ShowBalloonTip(1000);

                                    if (this.Visibility == Visibility.Hidden && miniwindow != null)
                                    {
                                        miniwindow.FlashImage();
                                    }
                                }
                                break;
                            //对方开始接收文件,本地开始发送
                            case MessageID.UserRecvFile:
                                for (i = 0; i < talklist.Count; i++)
                                {
                                    this.talklist[i].Dispatcher.Invoke(new Action(() =>
                                    {
                                        if (talklist[i].Title == ReadMessage.user.ip)
                                        {
                                            talklist[i].listen(ReadMessage.fileid);
                                            BreakFlag = true;
                                        }
                                    }));
                                    if (BreakFlag == true)
                                    {
                                        break;
                                    }
                                }
                                break;
                            //拒绝接收文件
                            case MessageID.UserRefuseFile:
                                for (i = 0; i < talklist.Count; i++)
                                {
                                    this.talklist[i].Dispatcher.Invoke(new Action(() =>
                                    {
                                        if (talklist[i].Title == ReadMessage.user.ip)
                                        {
                                            talklist[i].CloseSendFile(ReadMessage.fileid);
                                            BreakFlag = true;
                                        }
                                    }));
                                    if (BreakFlag == true)
                                    {
                                        break;
                                    }
                                }
                                break;
                            //回复关闭文件信息
                            case MessageID.UserCancelFile:
                                for (i = 0; i < talklist.Count; i++)
                                {
                                    this.talklist[i].Dispatcher.Invoke(new Action(() =>
                                    {
                                        if (talklist[i].Title == ReadMessage.user.ip)
                                        {
                                            talklist[i].CloseSendFile();
                                            BreakFlag = true;
                                        }
                                    }));
                                    if (BreakFlag == true)
                                    {
                                        break;
                                    }
                                }
                                break;
                            //请求获得共享文件
                            case MessageID.UserGetSharedFiles:
                                SharedFileTo(ReadMessage.user.ip, MySharedFiles);
                                break;
                            //获得共享文件
                            case MessageID.UserSharedFiles:
                                for (i = 0; i < talklist.Count; i++)
                                {
                                    this.talklist[i].Dispatcher.Invoke(new Action(() =>
                                    {
                                        if (talklist[i].Title == ReadMessage.user.ip)
                                        {
                                            talklist[i].GetSharedFiles(ReadMessage.sharedfiles,ReadMessage.sharedpassword);
                                            BreakFlag = true;
                                        }
                                    }));
                                    if (BreakFlag == true)
                                    {
                                        break;
                                    }
                                }
                                //如果没有现有对话框的话,显示在主框中
                                if (i == talklist.Count)
                                {
                               
                                }
                                break;
                            case MessageID.UserGetSharedFile:
                                Thread t = new Thread(new ParameterizedThreadStart(listen)); //创建进程
                                t.Start(ReadMessage);                                        //开始进程
                                break;
                            case MessageID.UserGetAllFiles:
                                AllFileTo(ReadMessage.user.ip, MySharedFiles);
                                break;
                            case MessageID.UserSharedALLFiles:
                                if (AllFiles != null)
                                {
                                    this.Down_list.Dispatcher.Invoke(new Action(() =>
                                    {
                                        if (ReadMessage.sharedfiles == null)
                                        {
                                            MessageBox.Show("12");
                                        }
                                        for (i = 0; i < ReadMessage.sharedfiles.Count; i++)
                                        {
                                            SharedFiles file = new SharedFiles();
                                            file.ip = ReadMessage.sharedfiles[i].ip;
                                            file.username = ReadMessage.sharedfiles[i].username;
                                            file.name = ReadMessage.sharedfiles[i].name;
                                            file.SharedWay = ReadMessage.sharedfiles[i].SharedWay;
                                            file.size = ReadMessage.sharedfiles[i].size;
                                            file.filepath = ReadMessage.sharedfiles[i].filepath;
                                            file.image = GetFileImage(file.name);
                                            AllFiles.Add(file);

                                        }
                                        Down_list.ItemsSource = null;
                                        Down_list.ItemsSource = AllFiles;
                                    }));
                                }
                                break;
                            default:
                                break;
                        }
                     }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }
        }
Exemplo n.º 2
0
 public void GetSharedFiles(List<SharedFilesTo> files,string password)
 {
     SharedFilesList.Clear();
     for (int i = 0; i < files.Count; i++)
     {
         SharedFiles file = new SharedFiles();
         file.name = files[i].name;
         file.SharedWay = files[i].SharedWay;
         file.size = files[i].size;
         file.filepath = files[i].filepath;
         file.image = GetFileImage(file.name);
         SharedFilesList.Add(file);
     }
     SharedPassword = password;
     ShowSharedFiles();
 }
Exemplo n.º 3
0
 //获得本地共享文件内容
 public List<SharedFiles> GetMyShare(string Path)
 {
     List<SharedFiles> AllFiles = new List<SharedFiles>();
     string[] strDirectorys = Directory.GetDirectories(Path);
     string[] strFiles = Directory.GetFiles(Path);
     foreach (string strDirectory in strDirectorys)
     {
         string[] Files = Directory.GetFiles(strDirectory);
         foreach (string strFile in Files)
         {
             SharedFiles file = new SharedFiles();
             file.name = strFile.Substring(strFile.LastIndexOf('\\') + 1);
             //file.image = new BitmapImage(new Uri("/Images/directory.png", UriKind.Relative)); ;
             //file.size = "文件夹";
             file.image = GetFileImage(file.name);
             file.size = FomatFileSize(GetFileSize(strFile));
             AllFiles.Add(file);
         }
     }
     foreach (string strFile in strFiles)
     {
         SharedFiles file = new SharedFiles();
         file.name = strFile.Substring(strFile.LastIndexOf('\\') + 1);
         file.image = GetFileImage(file.name);
         file.size = FomatFileSize(GetFileSize(strFile));
         AllFiles.Add(file);
     }
     //for (int i = 0; i < AllFiles.Count; i++)
     //{
     //    MessageBox.Show(AllFiles[i]);
     //}
     return AllFiles;
 }
Exemplo n.º 4
0
 public void DownloadFile(SharedFiles sharedfile)
 {
     if (sharedfile.SharedWay == DownloadWay.NeedPassword)
     {
         EnterPassword Dlg = new EnterPassword();
         Dlg.ShowDialog();
         if (Dlg.Is_GetPassword)
         {
             if (Dlg.password.Text == SharedPassword)
             {
                 Parent.GetSharedFile(TalkUser.ip, sharedfile.filepath);
                 SharedFile sf = new SharedFile();
                 sf.SavePath = Parent.DownedDir + "\\" + sharedfile.name;
                 sf.size = sharedfile.size;
                 Thread g = new Thread(new ParameterizedThreadStart(GetSharedFile)); //创建进程
                 g.Start(sf);                                   //开始进程
             }
             else
             {
                 MessageBox.Show("您输入的口令有误");
             }
         }
     }
     else
     {
         Parent.GetSharedFile(TalkUser.ip, sharedfile.filepath);
         SharedFile sf = new SharedFile();
         sf.SavePath = Parent.DownedDir + "\\" + sharedfile.name;
         sf.size = sharedfile.size;
         Thread g = new Thread(new ParameterizedThreadStart(GetSharedFile)); //创建进程
         g.Start(sf);                                   //开始进程
     }
 }