Example #1
0
 /// <summary>
 /// Check whether a given download should be deferred to be started later.
 /// Decision is made based on whether the host is throttled and whether
 /// we're already downloading something else from it.
 /// </summary>
 /// <param name="target">Info about a requested download</param>
 /// <returns>
 /// true to queue, false to start immediately
 /// </returns>
 private bool shouldQueue(Net.DownloadTarget target)
 {
     return(throttledHosts.Contains(target.url.Host) &&
            downloads.Any(dl =>
                          dl.target.url.Host == target.url.Host &&
                          dl.bytesLeft > 0));
 }
        private void DownloadModule(Net.DownloadTarget target)
        {
            if (shouldQueue(target))
            {
                // Throttled host already downloading, we will get back to this later
                queuedDownloads.Add(target);
            }
            else
            {
                // We need a new variable for our closure/lambda, hence index = 1+prev max
                int index = downloads.Count;

                var dl = new NetAsyncDownloaderDownloadPart(target);
                downloads.Add(dl);

                // Encode spaces to avoid confusing URL parsers
                User.RaiseMessage("Downloading \"{0}\"",
                                  dl.target.url.ToString().Replace(" ", "%20"));

                // Schedule for us to get back progress reports.
                dl.Progress += (sender, args) =>
                               FileProgressReport(index,
                                                  args.ProgressPercentage,
                                                  args.BytesReceived,
                                                  args.TotalBytesToReceive);

                // And schedule a notification if we're done (or if something goes wrong)
                dl.Done += (sender, args) =>
                           FileDownloadComplete(index, args.Error);

                // Start the download!
                dl.Download(dl.target.url, dl.path);
            }
        }
Example #3
0
            public NetAsyncDownloaderDownloadPart(Net.DownloadTarget target, string path = null)
            {
                this.url           = target.url;
                this.fallbackUrl   = target.fallbackUrl;
                this.triedFallback = false;
                this.path          = path ?? Path.GetTempFileName();
                size = bytesLeft = target.size;
                lastProgressUpdateTime = DateTime.Now;

                agent.Headers.Add("User-Agent", Net.UserAgentString);

                // Tell the server what kind of files we want
                if (!string.IsNullOrEmpty(target.mimeType))
                {
                    log.InfoFormat("Setting MIME type {0}", target.mimeType);
                    agent.Headers.Add("Accept", target.mimeType);
                }

                // Check whether to use an auth token for this host
                string token;

                if (Win32Registry.TryGetAuthToken(this.url.Host, out token) &&
                    !string.IsNullOrEmpty(token))
                {
                    log.InfoFormat("Using auth token for {0}", this.url.Host);
                    // Send our auth token to the GitHub API (or whoever else needs one)
                    agent.Headers.Add("Authorization", $"token {token}");
                }
            }
 public NetAsyncDownloaderDownloadPart(Net.DownloadTarget target, string path = null)
 {
     this.target                 = target;
     this.mimeType               = target.mimeType;
     this.triedFallback          = false;
     this.path                   = path ?? Path.GetTempFileName();
     this.size                   = bytesLeft = target.size;
     this.lastProgressUpdateTime = DateTime.Now;
 }
 /// <summary>
 /// Check whether a given download should be deferred to be started later.
 /// Decision is made based on whether we're already downloading something
 /// else from the same host.
 /// </summary>
 /// <param name="target">Info about a requested download</param>
 /// <returns>
 /// true to queue, false to start immediately
 /// </returns>
 private bool shouldQueue(Net.DownloadTarget target)
 {
     return(downloads.Any(dl =>
                          dl.target.url.Host == target.url.Host &&
                          dl.bytesLeft > 0));
 }