Esempio n. 1
0
        private void RemoveUserList(UserInfo user)
        {
            int i = 0;

            while (i < userList.Items.Count)
            {
                if ((userList.Items[i] as ListBoxItem).Content.ToString() == user.IpAddress)
                    break;
                i++;
            }

            userList.Items.RemoveAt(i);
        }
Esempio n. 2
0
        private int fileSpeed = 60 * 1024;//buffer lebngth

        public MainWindow()
        {
            try
            {
                InitializeComponent();
                this.fileScrollViewer.Background = Brushes.LightGray;
                this.fileScrollViewer.BorderBrush = Brushes.Gray;
            }
            catch (Exception ex)
            {
                AppLog.SysLog(ex.ToString());
            }

            try
            {
                localInfo = new UserInfo
                {
                    IpAddress = NetworkUtil.GetLocalIp(),
                    Name = this.textBox1.Text
                };

                richTextBox1.KeyDown += richTextBox1_KeyDown;

                this.userList.SelectionChanged += new SelectionChangedEventHandler(new EventHandler((object obj, EventArgs e) =>
                {
                    if (fileCanvas.Children.Count > 0)
                    {
                        MessageBox.Show("有尚未完成的文件传输");
                        return;
                    }
                    if (userList.SelectedItem != null)
                        selectInfo = new UserInfo { IpAddress = (userList.SelectedItem as ListBoxItem).Content.ToString() };
                }));

                fileReceive = new Queue();
                fileSend = new Queue();
                running = true;
                broadcast = new UdpServer(IPAddress.Parse(NetworkUtil.GetNetBroad()), IPAddress.Any, port);
                Online = new MessageInfo { From = localInfo, MessageType = MessageInfo.Type.Sign, MessageSign = MessageInfo.Sign.Online };

                broadcast.OnReceiveComplete += new UdpServer.ReceiveComplete((s) =>
                {
                    MemoryStream ms = new MemoryStream(s);
                    MessageInfo msg = NetBird.Util.Serializable.Deserialize<MessageInfo>(ms);
                    ms.Close();
                    if (msg.From != null)
                        switch (msg.MessageType)
                        {
                            case MessageInfo.Type.Sign:
                                switch (msg.MessageSign)
                                {
                                    case MessageInfo.Sign.Online: this.userList.Dispatcher.Invoke(new Action(() =>
                                    {
                                        //AddUserList(msg.From); //测试时允许添加本机IP到UserList
                                        if (msg.From.IpAddress == localInfo.IpAddress)
                                            return;
                                        AddUserList(msg.From);
                                        MessageInfo rspInfo = new MessageInfo { From = localInfo, MessageSign = MessageInfo.Sign.ResponseOnline, MessageType = MessageInfo.Type.Sign };
                                        broadcast.SendMessage(Serializable.SerializableToBytes(rspInfo), IPAddress.Parse(msg.From.IpAddress), port);
                                    }));
                                        break;
                                    case MessageInfo.Sign.Offline:

                                        this.userList.Dispatcher.Invoke(new Action(() =>
                                        {
                                            if (localInfo.IpAddress != msg.From.IpAddress)
                                                RemoveUserList(msg.From);
                                        }));
                                        break;
                                    case MessageInfo.Sign.FileRequest:
                                        if (msg.Attachment != null)
                                            fileReceive.Enqueue(msg);
                                        break;
                                    case MessageInfo.Sign.FileAccept:
                                        if (msg.Attachment != null)
                                            fileSend.Enqueue(msg);
                                        break;
                                    case MessageInfo.Sign.ResponseOnline:
                                        this.userList.Dispatcher.Invoke(new Action(() =>
                                        {
                                           AddUserList(msg.From);
                                        }));
                                        break;
                                }
                                break;
                            case MessageInfo.Type.Common:
                                CommonMessage(string.Format("{0}:{1}", msg.From.IpAddress, msg.MessageBody));
                                break;
                        }
                });

                broadcast.OnSendComplete += new UdpServer.SendComplete(() => { });

                broadcast.SendMessage(NetBird.Util.Serializable.SerializableToBytes(Online));
                broadcast.StartListen();

                //udpClient = new UdpClient(port);
                //listen = new UdpClient(port);
                //tcpClient = new TcpClient();

                fileThread = new Thread(new ThreadStart(() =>
                {
                    fileRequestListen = new UdpServer(IPAddress.Parse(localInfo.IpAddress), IPAddress.Any, filePort);
                    fileRequestListen.StartListen();
                    fileRequestListen.OnSendComplete += new UdpServer.SendComplete(() => { });
                    fileRequestListen.OnReceiveComplete += new UdpServer.ReceiveComplete((byte[] content) =>
                    {
                        MemoryStream ms = new MemoryStream(content);
                        FileSendInfo fsInfo = Serializable.Deserialize<FileSendInfo>(ms);
                        ms.Close();
                        switch (fsInfo.Type)
                        {
                            case FileSendInfo.InfoType.Send:
                                this.fileRCanvas.Dispatcher.Invoke(new Action(() =>
                                {
                                    try
                                    {
                                        var rlb = GetFileLabelById(fsInfo.Id);
                                        if (rlb != null)
                                        {
                                            FileStream rfs = File.OpenWrite(rlb.FilePath);
                                            rfs.Position = fsInfo.Seek;
                                            rfs.Write(fsInfo.Content, 0, fsInfo.Content.Length);
                                            int rseek = (int)rfs.Length;
                                            rfs.Close();
                                            //double ss = (double)rseek / (double)rlb.FileLength;
                                            rlb.Progress.Value = ((double)rseek / (double)rlb.FileLength) * 100;
                                            FileSendInfo rnfsInfo = new FileSendInfo { Id = fsInfo.Id, Seek = rseek, Type = FileSendInfo.InfoType.Back, IpAddress = localInfo.IpAddress };
                                            fileRequestListen.SendMessage(Serializable.SerializableToBytes(rnfsInfo), IPAddress.Parse(fsInfo.IpAddress), filePort);
                                        }
                                    }
                                    catch (Exception ex0)
                                    {
                                        throw ex0;
                                    }
                                }));
                                break;
                            case FileSendInfo.InfoType.Back:
                                this.fileCanvas.Dispatcher.Invoke(new Action(() =>
                                {
                                    try
                                    {
                                        var lb = fileCanvas.Children[fsInfo.Id] as Controls.FileLabel;
                                        int seek = fsInfo.Seek;
                                        FileSendInfo nfsInfo = null;
                                        FileStream fs = File.OpenRead(lb.FilePath);
                                        //文件发送完信号
                                        if (fs.Length - 1 < seek)
                                        {
                                            nfsInfo = new FileSendInfo { Id = fsInfo.Id, IpAddress = localInfo.IpAddress, Type = FileSendInfo.InfoType.Over };
                                           
                                        }
                                        else
                                        {
                                            byte[] buffer = new byte[fileSpeed];
                                            fs.Position = seek;
                                            fs.Read(buffer, 0, buffer.Length);
                                            fs.Close();
                                            lb.Progress.Value = ((double)seek / (double)lb.FileLength) * 100;
                                            nfsInfo = new FileSendInfo { Id = fsInfo.Id, Seek = seek, Content = buffer, IpAddress = localInfo.IpAddress, Type = FileSendInfo.InfoType.Send };
                                        }
                                        fileRequestListen.SendMessage(Serializable.SerializableToBytes(nfsInfo), IPAddress.Parse(fsInfo.IpAddress), filePort);
                                    }
                                    catch (Exception ex0)
                                    {
                                        throw ex0;
                                    }
                                }));
                                break;
                            case FileSendInfo.InfoType.SendClose:
                            case FileSendInfo.InfoType.Over:
                                this.fileRCanvas.Dispatcher.Invoke(new Action(() =>
                                {
                                    var olb = GetFileLabelById(fsInfo.Id);
                                    if (olb != null)
                                        olb.Close();
                                }));
                                break;
                            case FileSendInfo.InfoType.AcceptClose:
                                this.fileCanvas.Dispatcher.Invoke(new Action(() =>
                                {
                                    var lb = fileCanvas.Children[fsInfo.Id] as Controls.FileLabel;
                                    if (lb != null)
                                        lb.Close();
                                }));
                                break;
                        }
                    });

                    while (running)
                    {
                        if (fileSend.Count > 0)
                        {
                            MessageInfo fileInfo = fileSend.Dequeue() as MessageInfo;
                            AttachmentInfo attr = fileInfo.Attachment[0];
                            this.fileCanvas.Dispatcher.Invoke(new Action(() =>
                            {
                                Controls.FileLabel lb = fileCanvas.Children[attr.Id] as Controls.FileLabel;
                                lb.onCloseFile += new Controls.FileLabel.CloseEvent(() =>
                                {
                                    FileSendInfo fsOver = new FileSendInfo { Id = attr.Id, IpAddress = localInfo.IpAddress, Type = FileSendInfo.InfoType.SendClose };
                                    fileRequestListen.SendMessage(Serializable.SerializableToBytes(fsOver), IPAddress.Parse(fileInfo.From.IpAddress), filePort);
                                });
                                byte[] buffer = new byte[fileSpeed];
                                FileStream fs = File.OpenRead(lb.FilePath);
                                fs.Position = 0;
                                fs.Read(buffer, 0, buffer.Length);
                                fs.Close();
                                FileSendInfo fsInfo = new FileSendInfo { Id = attr.Id, Seek = 0, Content = buffer, IpAddress = localInfo.IpAddress, Type = FileSendInfo.InfoType.Send };
                                fileRequestListen.SendMessage(Serializable.SerializableToBytes(fsInfo), IPAddress.Parse(fileInfo.From.IpAddress), filePort);
                                //fileRequestListen.SendMessage();

                                //todu 发送文件
                            }));
                        }

                        if (fileReceive.Count > 0)
                        {
#if DEBUG
                            try
                            {
#endif
                                //todo 接受文件 (需要Add到FileCanvas)
                                MessageInfo fileInfo = fileReceive.Dequeue() as MessageInfo;
                                AttachmentInfo attr = fileInfo.Attachment[0];
                                this.fileRCanvas.Dispatcher.Invoke(new Action(() =>
                                {

                                    var fb = new Controls.FileLabel(string.Format("{0}{1}.{2}", PathInfo.GetUserAttachmentDir(), attr.Name, attr.Ext));
                                    fb.Id = attr.Id;
                                    fb.FileLength = attr.Length;
                                    fb.fileIcon.Source = IconHelper.GetIcon(attr.Icon.Handle);
                                    fb.onCloseFile += new Controls.FileLabel.CloseEvent(() =>
                                    {
                                        foreach (UserControl uie in this.fileRCanvas.Children)
                                        {
                                            if (uie.Margin.Top > fb.Margin.Top)
                                                uie.Margin = new Thickness(uie.Margin.Left, uie.Margin.Top - fb.Height, uie.Margin.Right, uie.Margin.Bottom);
                                        }
                                        if (this.fileRCanvas.Height > 200)
                                            this.fileRCanvas.Height -= fb.Height;
                                    });
                                    fb.onClosedFile += new Controls.FileLabel.ClosedEvent(() =>
                                    {
                                        if (this.fileRCanvas.Children.Count > 0)
                                            return;
                                        this.Width -= 200;
                                        fileRScrollViewer.Visibility = System.Windows.Visibility.Collapsed;
                                        FileSendInfo fsOver = new FileSendInfo { Id = attr.Id, IpAddress = localInfo.IpAddress, Type = FileSendInfo.InfoType.AcceptClose };
                                        fileRequestListen.SendMessage(Serializable.SerializableToBytes(fsOver), IPAddress.Parse(fileInfo.From.IpAddress), filePort);
                                    });
                                    fb.onProgressBegin += new Controls.FileLabel.ProgressBeginEvent(() =>
                                    {
                                        fb.Height += 10;
                                        FileSendInfo fsInfo = new FileSendInfo { Id = attr.Id, Seek = 0, IpAddress = localInfo.IpAddress, Type = FileSendInfo.InfoType.Back };
                                        fileRequestListen.SendMessage(Serializable.SerializableToBytes(fsInfo), IPAddress.Parse(fileInfo.From.IpAddress), filePort);
                                    });
                                    fb.onProgressEnd += new Controls.FileLabel.ProgressEndEvent(() =>
                                    {
                                        fb.Height -= 10;
                                    });
                                    AddFileToCanvas(fb, ref this.fileRScrollViewer, ref this.fileRCanvas);
                                    fb.Accept();
                                }));
#if DEBUG
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
#endif
                        }
                    }
                }));

                fileThread.IsBackground = true;
                fileThread.Start();

                this.richTextBox1.AddHandler(RichTextBox.DragOverEvent, new DragEventHandler(richTextBox1_DragOver), true);
                this.richTextBox1.AddHandler(RichTextBox.DropEvent, new DragEventHandler(richTextBox1_Drop), true);
                this.richTextBox1.AddHandler(RichTextBox.DragLeaveEvent, new DragEventHandler(richTextBox1_DragLeave), true);
                bs = this.richTextBox1.BorderBrush;


            }
            catch (Exception e)
            {
                AppLog.SysLog(e.ToString());
            }
        }
Esempio n. 3
0
 private void AddUserList(UserInfo user)
 {
     ListBoxItem listbox = new ListBoxItem();
     listbox.Content = user.IpAddress;
     userList.Items.Add(listbox);
 }