Ejemplo n.º 1
0
        public static void Main()
        {
            bool useDownloadSpeedThrottling = false;

            // Please insert an URL of a large file here, otherwise the download will be finished too quickly to really demonstrate the functionality.
            var url               = new Uri("http://yourdomain.com/yourfile.zip");
            var file              = new System.IO.FileInfo("yourfile.zip");
            var requestBuilder    = new SimpleWebRequestBuilder();
            var dlChecker         = new DownloadChecker();
            var httpDlBuilder     = new SimpleDownloadBuilder(requestBuilder, dlChecker);
            var timeForHeartbeat  = 3000;
            var timeToRetry       = 5000;
            var maxRetries        = 5;
            var resumingDlBuilder = new ResumingDownloadBuilder(timeForHeartbeat, timeToRetry, maxRetries, httpDlBuilder);
            List <DownloadRange> alreadyDownloadedRanges = null;
            var bufferSize            = 4096;
            var numberOfParts         = 4;
            var maxRetryDownloadParts = 2;
            var download     = new MultiPartDownload(url, bufferSize, numberOfParts, resumingDlBuilder, requestBuilder, dlChecker, alreadyDownloadedRanges, maxRetryDownloadParts);
            var speedMonitor = new DownloadSpeedMonitor(maxSampleCount: 128);

            speedMonitor.Attach(download);
            var progressMonitor = new DownloadProgressMonitor();

            progressMonitor.Attach(download);

            if (useDownloadSpeedThrottling)
            {
                var downloadThrottling = new DownloadThrottling(maxBytesPerSecond: 200 * 1024, maxSampleCount: 128);
                downloadThrottling.Attach(download);
            }

            var dlSaver = new DownloadToFileSaver(file);

            dlSaver.Attach(download);
            download.DownloadCompleted += OnCompleted;
            download.DownloadCancelled += OnDownloadCancelled;
            download.DownloadStopped   += OnDownloadStopped;
            download.Start();

            while (!finished)
            {
                Thread.Sleep(1000);

                var alreadyDownloadedSizeInBytes = progressMonitor.GetCurrentProgressInBytes(download);
                var totalDownloadSizeInBytes     = progressMonitor.GetTotalFilesizeInBytes(download);
                var currentSpeedInBytesPerSecond = speedMonitor.GetCurrentBytesPerSecond();

                var currentProgressInPercent   = progressMonitor.GetCurrentProgressPercentage(download) * 100;
                var alreadyDownloadedSizeInKiB = (alreadyDownloadedSizeInBytes / 1024);
                var totalDownloadSizeInKiB     = (totalDownloadSizeInBytes / 1024);
                var currentSpeedInKiBPerSecond = (currentSpeedInBytesPerSecond / 1024);
                var remainingTimeInSeconds     = currentSpeedInBytesPerSecond == 0 ? 0 : (totalDownloadSizeInBytes - alreadyDownloadedSizeInBytes) / currentSpeedInBytesPerSecond;

                Console.WriteLine(
                    "Progress: " + currentProgressInPercent + "% " + "(" + alreadyDownloadedSizeInKiB + " of " + totalDownloadSizeInKiB + " KiB)" +
                    "   Speed: " + currentSpeedInKiBPerSecond + " KiB/sec." +
                    "   Remaining time: " + remainingTimeInSeconds + " sec.");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Update Downlaoder
        /// </summary>
        /// <param name="downloadurl">file url to download</param>
        public void DownloadUpdate()
        {
            bool useDownloadSpeedThrottling = false;

            var file              = new FileInfo(Path.GetFileName(DownloadUrl.ToString()));
            var requestBuilder    = new SimpleWebRequestBuilder();
            var dlChecker         = new DownloadChecker();
            var httpDlBuilder     = new SimpleDownloadBuilder(requestBuilder, dlChecker);
            var timeForHeartbeat  = 3000;
            var timeToRetry       = 5000;
            var maxRetries        = 25;
            var resumingDlBuilder = new ResumingDownloadBuilder(timeForHeartbeat, timeToRetry, maxRetries, httpDlBuilder);
            List <DownloadRange> alreadyDownloadedRanges = null;
            var bufferSize    = 4096;
            var numberOfParts = 4;
            var download      = new MultiPartDownload(DownloadUrl, bufferSize, numberOfParts, resumingDlBuilder, requestBuilder, dlChecker, alreadyDownloadedRanges);
            var speedMonitor  = new DownloadSpeedMonitor(maxSampleCount: 50000);

            speedMonitor.Attach(download);
            var progressMonitor = new DownloadProgressMonitor();

            progressMonitor.Attach(download);

            if (useDownloadSpeedThrottling)
            {
                var downloadThrottling = new DownloadThrottling(maxBytesPerSecond: 200 * 10024, maxSampleCount: 1288);
                downloadThrottling.Attach(download);
            }

            var dlSaver = new DownloadToFileSaver(file);

            dlSaver.Attach(download);
            download.DownloadCompleted += OnCompleted;
            download.Start();

            while (!DownloadFinished)
            {
                var alreadyDownloadedSizeInBytes = progressMonitor.GetCurrentProgressInBytes(download);
                var totalDownloadSizeInBytes     = progressMonitor.GetTotalFilesizeInBytes(download);
                var currentSpeedInBytesPerSecond = speedMonitor.GetCurrentBytesPerSecond();

                var currentProgressInPercent   = progressMonitor.GetCurrentProgressPercentage(download) * 100;
                var alreadyDownloadedSizeInKiB = (alreadyDownloadedSizeInBytes / 1024);
                var totalDownloadSizeInKiB     = (totalDownloadSizeInBytes / 1024);
                var currentSpeedInKiBPerSecond = (currentSpeedInBytesPerSecond / 1024);
                var remainingTimeInSeconds     = currentSpeedInBytesPerSecond == 0 ? 0 : (totalDownloadSizeInBytes - alreadyDownloadedSizeInBytes) / currentSpeedInBytesPerSecond;

                DownloaderProgress.Progress       = currentProgressInPercent;
                DownloaderProgress.DownlaodedSize = alreadyDownloadedSizeInKiB;
                DownloaderProgress.TotalSize      = totalDownloadSizeInKiB;
                DownloaderProgress.Speed          = currentSpeedInKiBPerSecond;
                DownloaderProgress.RemainingTime  = remainingTimeInSeconds;
            }
        }
Ejemplo n.º 3
0
        public static void Main()
        {
            // Please insert an URL of a large file here, otherwise the download will be finished too quickly to really demonstrate the functionality.
            var url               = new Uri("https://raw.githubusercontent.com/Toqe/Downloader/master/README.md");
            var file              = new System.IO.FileInfo("README.md");
            var requestBuilder    = new SimpleWebRequestBuilder();
            var dlChecker         = new DownloadChecker();
            var httpDlBuilder     = new SimpleDownloadBuilder(requestBuilder, dlChecker);
            var timeForHeartbeat  = 3000;
            var timeToRetry       = 5000;
            var maxRetries        = 5;
            var resumingDlBuilder = new ResumingDownloadBuilder(timeForHeartbeat, timeToRetry, maxRetries, httpDlBuilder);
            List <DownloadRange> alreadyDownloadedRanges = null;
            var bufferSize    = 4096;
            var numberOfParts = 4;
            var download      = new MultiPartDownload(url, bufferSize, numberOfParts, resumingDlBuilder, requestBuilder, dlChecker, alreadyDownloadedRanges);
            var speedMonitor  = new DownloadSpeedMonitor(maxSampleCount: 32);

            speedMonitor.Attach(download);
            var progressMonitor = new DownloadProgressMonitor();

            progressMonitor.Attach(download);
            var dlSaver = new DownloadToFileSaver(file);

            dlSaver.Attach(download);
            download.DownloadCompleted += OnCompleted;
            download.Start();

            while (!finished)
            {
                Thread.Sleep(1000);
                Console.WriteLine(
                    "Progress: " +
                    (progressMonitor.GetCurrentProgressPercentage(download) * 100) + "% " +
                    "(" + (progressMonitor.GetCurrentProgressInBytes(download) / 1024) + " of " +
                    (progressMonitor.GetTotalFilesizeInBytes(download) / 1024) + " KiB)" +
                    "   Speed: " +
                    (speedMonitor.GetCurrentBytesPerSecond() / 1024) + " KiB/sec.");
            }
        }
Ejemplo n.º 4
0
 void CalculateAvrage()
 {
     if (lastCalc > Time.time)
     {
         return;
     }
     lastCalc = Time.time + 0.5f;
     if (progressMonitor != null)
     {
         lock (AvrDownload)
         {
             if (AvrDownload.Count > 128)
             {
                 AvrDownload.Clear();
             }
             var s = speedMonitor.GetCurrentBytesPerSecond();
             if (s > 0)
             {
                 AvrDownload.Add(s);
             }
             DownloadSpeed = (int)AvrDownload.Average();
         }
     }
     if (progressMonitor != null)
     {
         lock (AvrDownload)
         {
             long time = DownloadSpeed == 0 ? 0 : (GetTotalFilesizeInBytes() - GetCurrentProgressInBytes()) / DownloadSpeed;
             if (AvrTime.Count > 128)
             {
                 AvrTime.Clear();
             }
             AvrTime.Add(time);
             RemainingTime = (long)AvrTime.Average();
         }
     }
 }