Ejemplo n.º 1
0
 public override void Dispose()
 {
     if (FileSegmentsManager != null)
     {
         FileSegmentsManager.RemoveFile(FileName);
     }
     base.Dispose();
 }
 /// <summary>
 /// 通过指定的单位块大小实例化片断管理对象(单位kb)
 /// </summary>
 /// <param name="segmentUnit"></param>
 /// <returns></returns>
 public static FileSegmentsManager Create(int segmentUnit)
 {
     if (_segmentManagerInstance != null)
     {
         return(_segmentManagerInstance);
     }
     lock (AsyncRoot)
     {
         if (_segmentManagerInstance == null)
         {
             if (segmentUnit > 100)
             {
                 _segmentUnit = segmentUnit * 1024;
             }
             return(_segmentManagerInstance = new FtpFileSegmentsManager());
         }
         return(_segmentManagerInstance);
     }
 }
        public override void DownLoadFileAsync(object userState = null)
        {
            UserState = userState;
            var action = new Action(() =>
            {
                //获取拆分的下载片断
                FileSegmentsManager = FtpFileSegmentsManager.Create();
                FileSegmentsManager.AddFile(TempDir, FileName, FileLength, LastModified);
                Segments.Value.AddRange(FileSegmentsManager.GetSegmentsByFileName(FileName));
                var sgmtGroupList = Segments.Value.Take(MaxThreadCount);
                int skipCount     = 0;
                while (sgmtGroupList.Any())
                {
                    foreach (var segment in sgmtGroupList)
                    {
                        var cts  = new CancellationTokenSource();
                        var task = Task.Factory.StartNew(FillSenmentData, segment, cts.Token);
                        SegmentRunningTasks.Value.Add(task);
                        //task.Wait(); //方便调试
                    }
                    ;
                    try
                    {
                        Task.WaitAll(SegmentRunningTasks.Value.ToArray());
                    }
                    catch (AggregateException ae)
                    {
                        OnFileDownloadCompletedEventHandler(new FileDownLoadCompletedEventArgs(TransferState.Error, ae.GetErrorString(), UserState));
                        break;
                    }
                    catch (Exception e)
                    {
                        OnFileDownloadCompletedEventHandler(new FileDownLoadCompletedEventArgs(TransferState.Error, e.GetErrorString(), UserState));
                        break;
                    }
                    skipCount    += MaxThreadCount;
                    sgmtGroupList = Segments.Value.Skip(skipCount).Take(MaxThreadCount);
                }
            });

            action.BeginInvoke(null, null);
        }
Ejemplo n.º 4
0
        public override void DownLoadFileAsync(object userState = null)
        {
            UserState = userState;
            var action = new Action(() =>
            {
                /*
                 * 如支持断点续传且文件大小>=10MB
                 * 则获取拆分的下载片断
                 */
                if (!BreakpointResumeDisabled && FileLength >= 10 * 1024 * 1024)
                {
                    FileSegmentsManager = HttpFileSegmentsManager.Create();
                    FileSegmentsManager.AddFile(TempDir, FileName, FileLength, LastModified);
                    Segments.Value.AddRange(FileSegmentsManager.GetSegmentsByFileName(FileName));
                    var sgmtGroupList = Segments.Value.Take(MaxThreadCount);
                    int skipCount     = 0;
                    while (sgmtGroupList.Any())
                    {
                        foreach (var segment in sgmtGroupList)
                        {
                            var cts  = new System.Threading.CancellationTokenSource();
                            var task = Task.Factory.StartNew(FillSenmentData, segment, cts.Token);
                            SegmentRunningTasks.Value.Add(task);
                            //task.Wait(); //方便调试
                        }
                        ;
                        try
                        {
                            Task.WaitAll(SegmentRunningTasks.Value.ToArray());
                        }
                        catch (AggregateException ae)
                        {
                            OnFileDownloadCompletedEventHandler(new FileDownLoadCompletedEventArgs(TransferState.Error, ae.GetErrorString(), this.UserState));
                            break;
                        }
                        catch (Exception e)
                        {
                            OnFileDownloadCompletedEventHandler(new FileDownLoadCompletedEventArgs(TransferState.Error, e.GetErrorString(), this.UserState));
                            break;
                        }
                        skipCount    += MaxThreadCount;
                        sgmtGroupList = Segments.Value.Skip(skipCount).Take(MaxThreadCount);
                    }
                }
                else
                {
                    if (wc == null)
                    {
                        wc = new MyWebClient();
                    }
                    var tempDir = Path.Combine(TempDir, FileName, DateTime.Now.ToFileTimeUtc().ToString());
                    if (!Directory.Exists(tempDir))
                    {
                        Directory.CreateDirectory(tempDir);
                    }
                    var targetFileName = string.IsNullOrEmpty(ExtensionName) ? Path.Combine(tempDir, FileName) : Path.Combine(tempDir, FileName) + "." + ExtensionName;

                    wc.DownloadProgressChanged += (s, e) =>
                    {
                        lock (this.wc)
                        {
                            OnProgressChanged(new ProgressChangedEventArgs(e.ProgressPercentage, this.UserState));
                        }
                    };
                    wc.DownloadFileCompleted += (s, e) =>
                    {
                        if (e.Error == null && !e.Cancelled)
                        {
                            OnFileDownloadCompletedEventHandler(new FileDownLoadCompletedEventArgs(TransferState.Completed, e.UserState.ToString(), UserState));
                        }
                        else
                        {
                            OnFileDownloadCompletedEventHandler(new FileDownLoadCompletedEventArgs(TransferState.Error, e.Error?.GetErrorString(), this.UserState));
                        }
                    };
                    wc.DownloadFileAsync(new Uri(DownloadFileUrl), targetFileName, targetFileName);
                }
            });

            action.BeginInvoke(null, null);
        }