Exemplo n.º 1
0
        /// <summary>
        /// Construct a HttpParallelDownloader.
        /// </summary>
        /// <param name="task">Download task, must with HttpRangedTarget.</param>
        /// <param name="folderProvider">Folder provider must not be null.</param>
        /// <param name="cacheProvider">Cache provider must not be null.</param>
        /// <param name="bufferProvider">Buffer provider must not be null.</param>
        /// <param name="checkPoint">Set the downloader to start at given checkPoint.</param>
        /// <param name="threadNum">Number of threads used.</param>
        /// <param name="threadSegmentSize">Downloading task is divided as segments
        /// before assigned to each thread. Segment size defines the approximate
        /// size of each segment.</param>
        public HttpParallelDownloader(
            DownloadTask task,
            IFolderProvider folderProvider,
            ICacheStorageProvider cacheProvider,
            IBufferProvider bufferProvider,
            byte[] checkPoint      = null,
            int threadNum          = 8,
            long threadSegmentSize = 8 * 1024 * 1024
            ) : base(task)
        {
            Ensure.That(task.Target is HttpRangableTarget, null, opts => opts.WithMessage(
                            $"type of {nameof(task.Target)} must be {nameof(HttpRangableTarget)}")
                        ).IsTrue();
            Ensure.That(folderProvider, nameof(folderProvider)).IsNotNull();
            Ensure.That(cacheProvider, nameof(cacheProvider)).IsNotNull();
            Ensure.That(bufferProvider, nameof(bufferProvider)).IsNotNull();
            Ensure.That(threadNum, nameof(threadNum)).IsGte(1);
            Ensure.That(threadSegmentSize, nameof(threadSegmentSize)).IsGt(1024 * 1024);

            this.folderProvider    = folderProvider;
            this.cacheProvider     = cacheProvider;
            this.bufferProvider    = bufferProvider;
            this.threadNum         = threadNum;
            this.threadSegmentSize = threadSegmentSize;

            Progress = new CompositeProgress((task.Target as HttpRangableTarget).DataLength);
            Speed    = SharedSpeedCalculatorFactory.NewSpeedCalculator();
            Progress.ProgressChanged += (sender, arg) => Speed.CurrentValue = Progress.DownloadedSize;

            if (checkPoint != null)
            {
                ApplyCheckPoint(checkPoint);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Construct a TorrentDownloader.
        /// </summary>
        /// <param name="task">Download task, must with HttpRangedTarget.</param>
        /// <param name="engine">Client engine of MonoTorrent which provides torrent and magnet downloading.</param>
        /// <param name="folderProvider">Folder provider must not be null.</param>
        /// <param name="cacheProvider">Cache provider must not be null.</param>
        /// <param name="checkPoint">Set the downloader to start at given checkPoint.</param>
        /// <param name="maximumConnections">
        /// The maximum number of concurrent open connections for this torrent.
        /// Defaults to 60.</param>
        /// <param name="maximumDownloadSpeed">
        /// The maximum download speed, in bytes per second, for this torrent.
        /// A value of 0 means unlimited. Defaults to 0.</param>
        /// <param name="maximumUploadSpeed">
        /// The maximum upload speed, in bytes per second, for this torrent.
        /// A value of 0 means unlimited. defaults to 0.</param>
        /// <param name="uploadSlots">
        /// The number of peers which can be uploaded to concurrently for this torrent.
        /// A value of 0 means unlimited. defaults to 8.</param>
        /// <param name="customAnnounceUrls">Custom announce URLs.</param>
        public TorrentDownloader(
            DownloadTask task,
            ClientEngine engine,
            IFolderProvider folderProvider,
            ICacheStorageProvider cacheProvider,
            byte[] checkPoint                 = null,
            int maximumConnections            = 60,
            int maximumDownloadSpeed          = 0,
            int maximumUploadSpeed            = 0,
            int uploadSlots                   = 8,
            IEnumerable <string> announceUrls = null
            ) : base(task)
        {
            Ensure.That(task.Target is TorrentTarget).IsTrue();
            Ensure.That(cacheProvider, nameof(cacheFolder)).IsNotNull();
            Ensure.That(folderProvider, nameof(folderProvider)).IsNotNull();
            Ensure.That(engine, nameof(engine)).IsNotNull();
            Ensure.That(maximumConnections).IsGt(0);
            Ensure.That(maximumDownloadSpeed).IsGte(0);
            Ensure.That(maximumUploadSpeed).IsGte(0);
            Ensure.That(uploadSlots).IsGt(0);

            this.engine               = engine;
            this.cacheProvider        = cacheProvider;
            this.folderProvider       = folderProvider;
            this.maximumConnections   = maximumConnections;
            this.maximumDownloadSpeed = maximumDownloadSpeed;
            this.maximumUploadSpeed   = maximumUploadSpeed;
            this.uploadSlots          = uploadSlots;
            this.announceUrls         = announceUrls?.ToList();

            TorrentTarget realTarget = (TorrentTarget)task.Target;

            Progress = new BaseMeasurableProgress(
                realTarget.Torrent.Files.Sum(
                    file => realTarget.IsFileSelected(file) ? file.Length : 0));
            Speed = SharedSpeedCalculatorFactory.NewSpeedCalculator();
            Progress.ProgressChanged += (sender, arg) => Speed.CurrentValue = Progress.DownloadedSize;

            if (checkPoint != null)
            {
                ApplyCheckPoint(checkPoint);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Construct a HttpDownloader with given task and configurations.
        /// </summary>
        /// <param name="task">Download task, must with HttpTarget.</param>
        /// <param name="folderProvider">Folder provider must not be null.</param>
        /// <param name="cacheProvider">Cache provider must not be null.</param>
        /// <param name="bufferProvider">Buffer provider must not be null.</param>
        public HttpDownloader(
            DownloadTask task,
            IFolderProvider folderProvider,
            ICacheStorageProvider cacheProvider,
            IBufferProvider bufferProvider
            ) : base(task)
        {
            Ensure.That(task.Target is HttpTarget, null,
                        opts => opts.WithMessage($"type of {nameof(task.Target)} must be {nameof(HttpTarget)}")
                        ).IsTrue();
            Ensure.That(folderProvider, nameof(folderProvider)).IsNotNull();
            Ensure.That(cacheProvider, nameof(cacheProvider)).IsNotNull();
            Ensure.That(bufferProvider, nameof(bufferProvider)).IsNotNull();

            this.folderProvider = folderProvider;
            this.cacheProvider  = cacheProvider;
            this.bufferProvider = bufferProvider;

            Progress = new BaseProgress();
            Speed    = SharedSpeedCalculatorFactory.NewSpeedCalculator();
            Progress.ProgressChanged += (sender, arg) => Speed.CurrentValue = Progress.DownloadedSize;
        }