Example #1
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;;
        }
Example #2
0
        public void StartUploadingToB(UploadInfo upload, ClientInfo controlledClient)
        {
            var dialog = upload.Dialog;
            var fs     = upload.Stream;

            win.Dispatcher.BeginInvoke((Action)(() =>
            {
                dialog.ShowDialog();
            }));
            Task.Run(() =>
            {
                //Debug.Assert(file.IsDirectory == false);
                try
                {
                    int bufferLength = 1024 * 1024;
                    byte[] buffer    = new byte[bufferLength];

                    int length = 0;
                    while (true)
                    {
                        if (upload.Canceled)
                        {
                            return;
                        }
                        long position = fs.Position;
                        win.Dispatcher.Invoke(() =>
                        {
                            try
                            {
                                dialog.Value   = position;
                                dialog.Message = $"正在上传{  upload.File.Name}:{Number.ByteToFitString(upload.Stream.Position)}/{Number.ByteToFitString(fs.Length)}";
                            }
                            catch (System.ObjectDisposedException)
                            {
                            }
                        });


                        int count = bufferLength;
                        if (fs.Length - fs.Position < bufferLength)
                        {
                            count = (int)(fs.Length - fs.Position);
                        }
                        length = fs.Read(buffer, 0, count);
                        if (length == 0)
                        {
                            win.Dispatcher.Invoke(() =>
                            {
                                dialog.Message = "上传完成";
                                upload.Dispose();
                            });
                            break;
                        }
                        if (length < bufferLength)
                        {
                            byte[] newBuffer = new byte[length];
                            Array.Copy(buffer, newBuffer, length);
                            buffer = newBuffer;
                        }

                        Telnet.Instance.Send(new CommandBody(ApiCommand.File_Upload, Global.CurrentClient.ID, controlledClient.ID,
                                                             new FileTransmissionPartInfo()
                        {
                            Content = buffer, Path = upload.File.Path, Position = position, Length = fs.Length, ID = upload.ID
                        }));

                        while (true)
                        {
                            if (Error.ContainsKey(upload.ID))
                            {
                                string message = Error[upload.ID];
                                upload.Dialog.SetToError();
                                upload.Dialog.Message = "上传失败:" + message;
                                //Telnet.Instance.Send(new CommandBody(File_AskForCancelUpload, Global.CurrentClient.ID, controlledClient.ID, upload.ID));
                            }
                            var canNext = CanSendNextPart.FirstOrDefault(p => p == upload.ID);
                            if (canNext != default)
                            {
                                CanSendNextPart.TryTake(out canNext);
                                break;
                            }
                            Thread.Sleep(30);
                        }
                    }
                }
                catch (System.ObjectDisposedException)
                {
                }
                catch (Exception ex)
                {
                    upload.Dialog.SetToError();
                    upload.Dialog.Message = "上传失败:" + ex.Message;
                    Telnet.Instance.Send(new CommandBody(File_AskForCancelUpload, Global.CurrentClient.ID, controlledClient.ID, upload.ID));
                }
            });
        }