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; }
private void du_onProgress(object sender, ProgressEventArgs e) { IProgressable d = (IProgressable)sender; OperationInfo op = (OperationInfo)d.State; op.totalSize = e.totalSize; op.doneSize = e.doneSize; fireOnProgress(op); if (op.status != OperationStatus.Processing) e.Cancel = true; else if (IsPause) { e.Cancel = true; op.status = OperationStatus.Pending; } else if (op.sid != Interlocked.Read(ref sid)) { e.Cancel = true; op.status = OperationStatus.Pending; } }
protected virtual void fireProgress(ProgressEventArgs args) { if (Progress != null) Progress(this, args); }
private uint onWrite(BaiduPCS sender, byte[] data, uint contentlength, object userdata) { if (IsCancelled) return 0; 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; }
// <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; }
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) { bigfile.Update(slice.start + slice.doneSize, data, 0, (int)size); } 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; }
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); }