Ejemplo n.º 1
0
        /// <summary>
        /// 创建<see cref="Downloader"/>
        /// </summary>
        /// <param name="url">下载地址</param>
        /// <param name="mirrors">镜像地址</param>
        /// <param name="savePath">保存路径</param>
        /// <param name="threadCount">下载线程数</param>
        /// <param name="cacheLength">缓存大小,缓存可减少硬盘写入压力,不使用缓存可设置为<see cref="Downloader.EmptyCacheLength"/></param>
        public Downloader(string url, IEnumerable <string> mirrors, string savePath, int threadCount, int cacheLength)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentException("url can not be null or empty.");
            }
            if (string.IsNullOrEmpty(savePath))
            {
                throw new ArgumentException("savePath can not be null or empty.");
            }
            if (threadCount <= 0)
            {
                throw new ArgumentException("threadCount must bigger than 0.");
            }

            this._workID            = EmptyWorkID;
            this._segmentCalculator = new MinSizeSegmentCalculator(SegmentMinSize);
            // 初始化_localFileCache
            var mainSource = new FileSource(url);
            List <FileSource> mirrorSources = null;

            if (mirrors != null)
            {
                mirrorSources = new List <FileSource>();
                foreach (var mirror in mirrors)
                {
                    mirrorSources.Add(new FileSource(mirror));
                }
            }// if

            this._config = new LocalFileConfig
            {
                MainSource = mainSource,
                Mirrors    = mirrorSources,
            };

            this._maxThreadCount = threadCount;
            this._savePath       = savePath;
            this._configPath     = DownloadHelper.GetConfigPathFromFilePath(savePath);
            this._tmpPath        = DownloadHelper.GetTmpPathFromFilePath(savePath);
            this._cacheLength    = cacheLength;
            this._status         = DownloaderStatuses.Idle;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 创建<see cref="Downloader"/>
        /// </summary>
        /// <param name="configPath">本地文件</param>
        /// <param name="threadCount">下载线程数</param>
        /// <param name="cacheLength">缓存大小,缓存可减少硬盘写入压力,不使用缓存可设置为<see cref="Downloader.EmptyCacheLength"/></param>
        public Downloader(string configPath, int threadCount, int cacheLength)
        {
            if (string.IsNullOrEmpty(configPath))
            {
                throw new ArgumentException("configPath can not be null or empty.");
            }

            var localFileConfig = LocalFileConfig.Load(configPath);

            if (localFileConfig.MainSource == null)
            {
                throw new Exception("LocalFileConfig.MainSource can not be null.");
            }
            if (localFileConfig.RemoteInfo == null)
            {
                throw new Exception("LocalFileConfig.RemoteInfo can not be null.");
            }
            if (localFileConfig.RemoteInfo.Size < 0)
            {
                throw new Exception("LocalFileConfig.RemoteInfo.Size must bigger than 0.");
            }
            if (!localFileConfig.HasSegment)
            {
                throw new Exception("LocalFileConfig.Segments can not be empty.");
            }

            this._workID            = EmptyWorkID;
            this._segmentCalculator = new MinSizeSegmentCalculator(SegmentMinSize);
            this._config            = localFileConfig;
            this._maxThreadCount    = threadCount;
            this._configPath        = configPath;
            this._savePath          = DownloadHelper.GetFilePathFromConfigPath(configPath);
            this._tmpPath           = DownloadHelper.GetTmpPathFromFilePath(this._savePath);
            this._cacheLength       = cacheLength;
            this._status            = DownloaderStatuses.Idle;
        }