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)); } }); }
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 })); } }); }