private uint onWrite(BaiduPCS sender, byte[] data, uint contentlength, object userdata) { if (IsCancelled) { return(0); } try { if (data.Length > 0) { Stream stream = (Stream)userdata; stream.Write(data, 0, data.Length); } DoneSize += data.Length; ProgressEventArgs args = new ProgressEventArgs(DoneSize, from.size); fireProgress(args); if (args.Cancel) { IsCancelled = true; return(0); } return((uint)data.Length); } catch (Exception ex) { Error = ex; } return(0); }
public MultiThreadUploader(BaiduPCS pcs, string from, string to, string workfolder, int threadCount) : base(pcs, from, to) { this.WorkFolder = workfolder; this.ThreadCount = threadCount; }
public MultiThreadDownloader(BaiduPCS pcs, PcsFileInfo from, string to, string workfolder, int threadCount, int minSliceSize = MIN_SLICE_SIZE) : base(pcs, from, to) { this.WorkFolder = workfolder; this.ThreadCount = threadCount; this.MinSliceSize = minSliceSize; }
public frmLogin(BaiduPCS pcs) { InitializeComponent(); DialogResult = System.Windows.Forms.DialogResult.Cancel; this.pcs = pcs; pcs.GetCaptcha += new GetCaptchaFunction(OnGetCaptcha); pcs.GetInput += new GetInputFunction(onGetInput); }
private uint onWrite(BaiduPCS sender, byte[] data, uint contentlength, object userdata) { Slice slice = (Slice)userdata; if (slice.tid != Interlocked.Read(ref taskId))//本次任务被取消 { lock (locker) { if (IsCancelled) { slice.status = SliceStatus.Cancelled; } else { slice.status = SliceStatus.Failed; } } return(0); } long size = data.Length; if (size > slice.totalSize - slice.doneSize) { size = slice.totalSize - slice.doneSize; } if (size > 0) { try { bigfile.Update(slice.start + slice.doneSize, data, 0, (int)size); } catch (Exception ex) { throw; } } slice.doneSize += size; lock (locker) DoneSize += size; if (slice.doneSize == slice.totalSize) //分片已经下载完成 { slice.status = SliceStatus.Successed; size = 0; } lock (sliceFileLocker) SliceHelper.SaveSliceList(SliceFileName, SliceList); // 保存最新的分片数据 long downloadedSize = 0; lock (locker) downloadedSize = DoneSize; ProgressEventArgs args = new ProgressEventArgs(downloadedSize, from.size); fireProgress(args); if (args.Cancel) { slice.status = SliceStatus.Cancelled; return(0); } return((uint)size); }
public virtual void Download() { if (Downloading) { throw new Exception("Can't download, since the previous download is not complete."); } FileStream stream = null; DoneSize = 0; Success = false; IsCancelled = false; Error = null; Downloading = true; try { BaiduPCS pcs = this.pcs.clone(); pcs.Write += onWrite; CreateDirectory(to); stream = new FileStream(to, FileMode.Create, FileAccess.Write); pcs.WriteUserData = stream; fireThreadChanged(new ThreadCountChangedEventArgs(1, 1)); PcsRes rc = pcs.download(from.path, 0, 0, 0); if (rc == PcsRes.PCS_OK) { Success = true; IsCancelled = false; } else if (IsCancelled) { Success = false; IsCancelled = true; } else { Success = false; IsCancelled = false; if (Error == null) { Error = new Exception(pcs.getError()); } } } catch (Exception ex) { Success = false; IsCancelled = false; Error = ex; } if (stream != null) { stream.Close(); } Downloading = false; fireCompleted(new CompletedEventArgs(Success, IsCancelled, Error)); fireThreadChanged(new ThreadCountChangedEventArgs(0, 1)); }
public static IntPtr pcs_malloc(uint sz) { if (BaiduPCS.pcs_isdebug()) { return(NativeMethods.pcs_mem_malloc_arg1(sz)); } else { return(NativeMethods.pcs_mem_malloc_raw(sz)); } }
public static void pcs_free(IntPtr ptr) { if (BaiduPCS.pcs_isdebug()) { NativeMethods.pcs_mem_free(ptr); } else { NativeMethods.pcs_mem_free_raw(ptr); } }
private int onProgress(BaiduPCS sender, double dltotal, double dlnow, double ultotal, double ulnow, object userdata) { ProgressEventArgs args = new ProgressEventArgs((long)ulnow, (long)ultotal); fireProgress(args); if (args.Cancel) { IsCancelled = true; return(-1); } return(0); }
// <returns>返回 0 表示已经到文件结尾,将停止上传。 // 返回 NativeConst.CURL_READFUNC_ABORT 将离开停止上传,并返回上传错误; // 返回 NativeConst.CURL_READFUNC_PAUSE 将暂停上传。</returns> private int Read(BaiduPCS sender, out byte[] buf, uint size, uint nmemb, object userdata) { Slice slice = (Slice)userdata; buf = null; if (slice.tid != Interlocked.Read(ref taskId))//本次任务被取消 { lock (locker) { if (IsCancelled) { slice.status = SliceStatus.Cancelled; } else { slice.status = SliceStatus.Failed; } } return(NativeConst.CURL_READFUNC_ABORT); } int sz = (int)(size * nmemb); if (sz > slice.totalSize - slice.doneSize) { sz = (int)(slice.totalSize - slice.doneSize); } buf = new byte[sz]; if (sz > 0) { using (FileStream fs = new FileStream(from, FileMode.Open, FileAccess.Read)) { fs.Position = slice.start + slice.doneSize; sz = fs.Read(buf, 0, sz); } } slice.doneSize += sz; long uploadedSize = 0; lock (locker) { DoneSize += sz; uploadedSize = DoneSize; } ProgressEventArgs args = new ProgressEventArgs(uploadedSize, fromFileInfo.Length); fireProgress(args); if (args.Cancel) { slice.status = SliceStatus.Cancelled; return(NativeConst.CURL_READFUNC_ABORT); } return(sz); }
static bool OnGetCaptcha(BaiduPCS sender, byte[] imgBytes, out string captcha, object userdata) { captcha = null; frmCaptcha frm = new frmCaptcha(imgBytes); frm.TopMost = true; if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK) { captcha = frm.Captcha; return(true); } return(false); }
public virtual void Upload() { if (Uploading) { throw new Exception("Can't upload, since the previous upload is not complete."); } DoneSize = 0; Success = false; IsCancelled = false; Error = null; Uploading = true; try { BaiduPCS pcs = this.pcs.clone(); pcs.Progress += onProgress; fireThreadChanged(new ThreadCountChangedEventArgs(1, 1)); Result = pcs.upload(to, from, IsOverWrite); if (!Result.IsEmpty) { Success = true; IsCancelled = false; } else if (IsCancelled) { Success = false; IsCancelled = true; } else { Success = false; IsCancelled = false; if (Error == null) { Error = new Exception(pcs.getError()); } } } catch (Exception ex) { Success = false; IsCancelled = false; Error = ex; } Uploading = false; fireCompleted(new CompletedEventArgs(Success, IsCancelled, Error)); fireThreadChanged(new ThreadCountChangedEventArgs(0, 1)); }
public override void Upload() { if (Uploading) { throw new Exception("Can't upload, since the previous upload is not complete."); } DoneSize = 0; Success = false; IsCancelled = false; Error = null; Uploading = true; try { BaiduPCS pcs = this.pcs.clone(); string filemd5 = null; string slicemd5 = null; Result = pcs.rapid_upload(to, from, ref filemd5, ref slicemd5, IsOverWrite); FileMD5 = filemd5; if (!Result.IsEmpty) { Success = true; IsCancelled = false; } else if (IsCancelled) { Success = false; IsCancelled = true; } else { Success = false; IsCancelled = false; if (Error == null) { Error = new Exception(pcs.getError()); } } } catch (Exception ex) { Success = false; IsCancelled = false; Error = ex; } Uploading = false; fireCompleted(new CompletedEventArgs(Success, IsCancelled, Error)); }
private bool onGetInput(BaiduPCS sender, string tips, out string captcha, object userdata) { captcha = null; frmInput frm = new frmInput(); if (tips != null && tips.StartsWith("Please input the sms password", StringComparison.InvariantCultureIgnoreCase)) { tips = "Please input the sms password (If not receive a sms password, click cancel, and then login again)."; frm.Tips = tips; frm.Text = "Please input the sms password"; } frm.TopMost = true; if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK) { captcha = frm.Value; return(true); } return(false); }
public Uploader(BaiduPCS pcs, string from, string to) { this.pcs = pcs; this.from = from; this.to = to; }
public RapidUploader(BaiduPCS pcs, string from, string to) : base(pcs, from, to) { }
public frmLogin(BaiduPCS pcs) { InitializeComponent(); DialogResult = System.Windows.Forms.DialogResult.Cancel; this.pcs = pcs; }
public Downloader(BaiduPCS pcs, PcsFileInfo from, string to) { this.pcs = pcs; this.from = from; this.to = to; }
public override void Upload() { if (Uploading) { throw new Exception("Can't upload, since the previous upload is not complete."); } DoneSize = 0; Success = false; IsCancelled = false; Error = null; Uploading = true; SliceFileName = null; SliceList = null; fromFileInfo = null; try { BaiduPCS pcs = this.pcs.clone(); string key; if (string.IsNullOrEmpty(FileMD5)) { string validate_md5, validate2_md5; fromFileInfo = new FileInfo(from); if (!pcs.md5(from, 0, MIN_SLICE_SIZE, out validate_md5)) { throw new Exception("Can't calculate md5 for " + from + "."); } if (!pcs.md5(from, fromFileInfo.Length - MIN_SLICE_SIZE, MIN_SLICE_SIZE, out validate2_md5)) { throw new Exception("Can't calculate md5 for " + from + "."); } key = from + " => " + to + ", size=" + fromFileInfo.Length.ToString() + ", validate1_md5=" + validate_md5 + ", validate2_md5=" + validate2_md5; key = Utils.md5(key.ToLower()); } else { key = FileMD5; } SliceFileName = "upload-" + key + ".slice"; SliceFileName = Path.Combine(WorkFolder, pcs.getUID(), SliceFileName); StateFileNameDecideEventArgs args = new StateFileNameDecideEventArgs() { SliceFileName = SliceFileName }; fireStateFileNameDecide(args); SliceFileName = args.SliceFileName; CreateOrRestoreSliceList(); // 创建或还原分片列表 foreach (Slice slice in SliceList) { if (slice.status != SliceStatus.Successed) { slice.status = SliceStatus.Pending; //重新上传未成功的分片 slice.doneSize = 0; } else { DoneSize += slice.doneSize; } } UploadSliceList(); // 启动线程来下载分片 Wait(); // 等待所有线程退出 List <string> md5list = new List <string>(); if (CheckResult(md5list)) // 检查下载结果 { Result = pcs.create_superfile(to, md5list.ToArray(), IsOverWrite); if (!Result.IsEmpty) { Success = true; IsCancelled = false; } else { Success = false; IsCancelled = false; Error = new Exception(pcs.getError()); } } else { Success = false; } } catch (Exception ex) { Success = false; IsCancelled = false; Error = ex; } if (Success) { SliceHelper.DeleteSliceFile(SliceFileName); } Uploading = false; fireCompleted(new CompletedEventArgs(Success, IsCancelled, Error)); }
private uint onWrite(BaiduPCS sender, byte[] data, uint contentlength, object userdata) { Slice slice = (Slice)userdata; try { if (slice.tid != Interlocked.Read(ref taskId))//本次任务被取消 { lock (locker) { if (IsCancelled) { slice.status = SliceStatus.Cancelled; } else { slice.status = SliceStatus.Failed; } } return(0); } long size = data.Length; if (size > slice.totalSize - slice.doneSize) { size = slice.totalSize - slice.doneSize; } if (size > 0) { lock (cache) cache.Add(slice.start + slice.doneSize, data, (int)size); } slice.doneSize += size; lock (locker) DoneSize += size; if (slice.doneSize == slice.totalSize) //分片已经下载完成 { slice.status = SliceStatus.Successed; size = 0; } lock (cache) { if (cache.TotalSize >= AppSettings.MaxCacheSize * 1024) { cache.Flush(); cache.Reset(); SliceHelper.SaveSliceList(SliceFileName, SliceList); // 保存最新的分片数据 } } long downloadedSize = 0; lock (locker) downloadedSize = DoneSize; ProgressEventArgs args = new ProgressEventArgs(downloadedSize, from.size); fireProgress(args); if (args.Cancel) { slice.status = SliceStatus.Cancelled; lock (locker) Error = new Exception("Cancelled"); return(0); } return((uint)size); } catch (Exception ex) { lock (locker) Error = ex; slice.status = SliceStatus.Error; } return(0); }