예제 #1
0
        public void StartNew(WinFileSystem win, ClientInfo client, FileFolderInfo selectedFile)
        {
            ControlledClient = client;
            string path = FileSystemDialog.GetSaveFile(null, true, false, selectedFile.Name);

            if (path != null)
            {
                FileTransmissionInfo trans = new FileTransmissionInfo()
                {
                    File = selectedFile
                };
                try
                {
                    download = new DownloadingInfo(win, client, path, trans);
                }
                catch (Exception ex)
                {
                    TaskDialog.ShowException(ex, "建立本地文件失败!");
                    return;
                }
                Telnet.Instance.FileSystemDownloadErrorReceived += FileSystemDownloadErrorReceived;
                Telnet.Instance.FileSystemDownloadPartReceived  += FileSystemDownloadPartReceived;

                Telnet.Instance.Send(new CommandBody(
                                         File_AskForDownloading, Global.CurrentClient.ID, client.ID, trans));

                download.Dialog.ShowDialog();
            }
        }
예제 #2
0
        public void StartNew(WinFileSystem win, ClientInfo client, string remoteFolderPath)
        {
            this.win         = win;
            ControlledClient = client;
            string path = FileSystemDialog.GetOpenFile();

            if (path != null)
            {
                var trans = new FileTransmissionInfo
                {
                    File = new FileFolderInfo()
                    {
                        Path = Path.Combine(remoteFolderPath, Path.GetFileName(path)), Length = new FileInfo(path).Length
                    }
                };
                currentUpload = new UploadInfo(win, ControlledClient, path, trans);

                Telnet.Instance.Send(new CommandBody(File_AskForStartUpload, Global.CurrentClient.ID, ControlledClient.ID, trans));
            }

            Telnet.Instance.FileSystemPrepareForUploadingReceived += FileSystemPrepareForUploadingReceived;;
        }
예제 #3
0
        public UploadInfo(WinFileSystem win, ClientInfo controlledClient, string localPath, FileTransmissionInfo trans)
        {
            LocalPath = localPath;
            File      = trans.File;
            ID        = trans.ID;
            Dialog    = new ProgressDialog(win)
            {
                Message = "正在上传" + File.Name
            };
            Dialog.Value   = 0;
            Dialog.Minimum = 0;
            Dialog.Maximum = trans.File.Length;
            Dialog.Title   = "上传";
            Dialog.Cancle += (p1, p2) =>
            {
                Canceled = true;
                Dispose();

                Telnet.Instance.Send(new CommandBody(File_AskForCancelUpload, Global.CurrentClient.ID, controlledClient.ID, ID));
            };
            Dialog.Closed += (p1, p2) =>
            {
                if (Dialog.Canceled == false)
                {
                    win.RefreshFolder();
                }
            };

            Stream = System.IO.File.OpenRead(localPath);
        }
예제 #4
0
        public DownloadingInfo(WinFileSystem win, ClientInfo controlledClient, string localPath, FileTransmissionInfo download)
        {
            LocalPath = localPath;
            File      = download.File;
            ID        = download.ID;
            Dialog    = new ProgressDialog(win)
            {
                Message = "正在下载" + File.Name
            };
            Dialog.Value   = 0;
            Dialog.Minimum = 0;
            Dialog.Maximum = 1;
            Dialog.Title   = "下载";
            Dialog.Cancle += (p1, p2) =>
            {
                Canceled = true;
                Dispose(true);

                Telnet.Instance.Send(new CommandBody(File_AskForCancelDownload, Global.CurrentClient.ID, controlledClient.ID, ID));
            };
            Dialog.Closed += (p1, p2) =>
            {
                if (Dialog.Canceled == false)
                {
                    win.RefreshFolder();
                }
            };

            Stream = System.IO.File.OpenWrite(localPath);
            Stream.SetLength(File.Length);
        }
        public static void StartDownloadingToA(CommandBody cmd)
        {
            Task.Run(() =>
            {
                FileTransmissionInfo download = cmd.Data as FileTransmissionInfo;
                var file = download.File;
                Debug.Assert(file.IsDirectory == false);
                try
                {
                    int bufferLength = 1024 * 1024;
                    byte[] buffer    = new byte[bufferLength];
                    using (FileStream fs = File.OpenRead(file.Path))
                    {
                        int length = 0;
                        while (true)
                        {
                            var cancel = Cancle.FirstOrDefault(p => p == download.ID);
                            if (cancel != default)
                            {
                                Cancle.TryTake(out cancel);
                                return;
                            }
                            long position = fs.Position;
                            int count     = bufferLength;
                            if (fs.Length - fs.Position < bufferLength)
                            {
                                count = (int)(fs.Length - fs.Position);
                            }
                            length = fs.Read(buffer, 0, count);
                            if (length == 0)
                            {
                                break;
                            }
                            if (length < bufferLength)
                            {
                                byte[] newBuffer = new byte[length];
                                Array.Copy(buffer, newBuffer, length);
                                buffer = newBuffer;
                            }

                            Telnet.Instance.Send(new CommandBody(ApiCommand.File_Download, cmd.AId, cmd.BId,
                                                                 new FileTransmissionPartInfo()
                            {
                                Content = buffer, Path = file.Path, Position = position, Length = fs.Length, ID = download.ID
                            }));
                            while (true)
                            {
                                var canNext = CanSendNextPart.FirstOrDefault(p => p == download.ID);
                                if (canNext != default)
                                {
                                    CanSendNextPart.TryTake(out canNext);
                                    break;
                                }
                                Thread.Sleep(30);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Telnet.Instance.Send(new CommandBody(ApiCommand.File_ReadDownloadingFileError, cmd.AId, cmd.BId,
                                                         new FileFolderFeedback()
                    {
                        ID = download.ID, Path = file.Path, Message = ex.Message, HasError = true
                    }));
                }
            });
        }