/// <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; }
/// <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; }
/// <summary> /// 通过配置文件创建<see cref="LocalFileConfig"/> /// </summary> /// <param name="path">配置文件路径</param> /// <returns></returns> public static LocalFileConfig Load(string path) { XDocument xml; // 以UTF-8的格式加载配置文件 using (var reader = new StreamReader(path, Encoding.UTF8)) { xml = XDocument.Load(reader); } LocalFileConfig fileInfo = new LocalFileConfig(); foreach (XElement field in xml.Root.Elements()) { switch (field.Name.LocalName) { case "Source": { if (string.IsNullOrEmpty(field.Value)) { break; } fileInfo._mainSource = FileSource.CreateFromXElement(field); } break; case "Mirrors": { List <FileSource> mirrors = new List <FileSource>(); foreach (var mirror in field.Elements()) { mirrors.Add(FileSource.CreateFromXElement(mirror)); } fileInfo._mirrors = mirrors; } break; case "RemoteInfo": { if (string.IsNullOrEmpty(field.Value)) { break; } fileInfo._remoteInfo = RemoteFileInfo.CreateFromXElement(field); } break; case "Segments": { List <LocalSegment> segments = new List <LocalSegment>(); foreach (var segment in field.Elements()) { segments.Add(LocalSegment.CreateFromXElement(segment)); } fileInfo._segments = segments; } break; default: break; } }// foreach return(fileInfo); }