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);
        }
 private void CreateOrRestoreSliceList()
 {
     // 分片文件存在,则从该文件中还原分片信息
     if (File.Exists(SliceFileName))
     {
         SliceList = SliceHelper.RestoreSliceList(SliceFileName);
         return;
     }
     // 新建分片
     SliceList = SliceHelper.CreateSliceList(from.size, MinSliceSize);
     //保存一次新创建的分片列表
     SliceHelper.SaveSliceList(SliceFileName, SliceList);
 }
Exemplo n.º 3
0
        private void CreateOrRestoreSliceList()
        {
            // 分片文件存在,则从该文件中还原分片信息
            if (File.Exists(SliceFileName))
            {
                SliceList = SliceHelper.RestoreSliceList(SliceFileName);
                return;
            }
            long slizeSize = SliceHelper.CalculateSliceSize(fromFileInfo.Length, MIN_SLICE_SIZE, MAX_SLICE_COUNT);

            // 新建分片
            SliceList = SliceHelper.CreateSliceList(fromFileInfo.Length, (int)slizeSize);
            //保存一次新创建的分片列表
            SliceHelper.SaveSliceList(SliceFileName, SliceList);
        }
 private void Flush()
 {
     if (cache.TotalSize > 0)
     {
         lock (cache)
         {
             if (cache.TotalSize > 0)
             {
                 cache.Flush();
                 cache.Reset();
                 SliceHelper.SaveSliceList(SliceFileName, SliceList); // 保存最新的分片数据
             }
         }
     }
 }
Exemplo n.º 5
0
        private void uploadTask(object objTID)
        {
            long tid = (long)objTID;

            try
            {
                BaiduPCS    pcs;
                Slice       slice;
                PcsFileInfo rf;
                pcs = this.pcs.clone();
                while (tid == Interlocked.Read(ref taskId))
                {
                    slice = popSlice();
                    if (slice == null)
                    {
                        break;
                    }
                    slice.tid         = tid;
                    pcs.WriteUserData = slice;
                    rf = pcs.upload_slicefile(new OnReadBlockFunction(Read), slice, (uint)slice.totalSize);
                    if (!string.IsNullOrEmpty(rf.md5))
                    {
                        slice.md5    = rf.md5;
                        slice.status = SliceStatus.Successed;
                    }
                    else if (slice.status == SliceStatus.Cancelled)
                    {
                        Cancel();
                    }
                    else
                    {
                        slice.status = SliceStatus.Failed;
                        lock (locker) Error = new Exception(pcs.getError());
                        StopAllDownloadThreads();
                    }
                    lock (sliceFileLocker) SliceHelper.SaveSliceList(SliceFileName, SliceList);
                }
            }
            catch (Exception ex)
            {
                lock (locker) Error = ex;
                StopAllDownloadThreads();
            }
            Interlocked.Decrement(ref runningThreadCount);
        }
        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);
        }