Exemplo n.º 1
0
        private void OnOpenReadCompleted(object sender, OpenReadCompletedEventArgs args)
        {
            DownloadWebClient webClient = sender as DownloadWebClient;

            if (webClient == null)
            {
                return;
            }

            lock (this.cancelSync)
            {
                if (this.isCancelled)
                {
                    return;
                }

                if (!webClient.HasResponse)
                {
                    this.TriggerWebClientDownloadFileAsync();
                    return;
                }

                bool   appendExistingChunk = webClient.IsPartialResponse;
                Stream destinationStream   = this.CreateDestinationStream(appendExistingChunk);
                if (destinationStream != null)
                {
                    this.TrySetStreamReadTimeout(args.Result, (int)this.SourceStreamReadTimeout.TotalMilliseconds);

                    this.worker                  = new StreamCopyWorker();
                    this.worker.Completed       += this.OnWorkerCompleted;
                    this.worker.ProgressChanged += this.OnWorkerProgressChanged;
                    this.worker.CopyAsync(args.Result, destinationStream, this.TotalBytesToReceive);
                }
            }
        }
Exemplo n.º 2
0
 private void TryCleanupExistingDownloadWebClient()
 {
     if (this.downloadWebClient == null)
     {
         return;
     }
     try
     {
         lock (this)
         {
             if (this.downloadWebClient != null)
             {
                 this.downloadWebClient.DownloadFileCompleted   -= this.OnDownloadCompleted;
                 this.downloadWebClient.DownloadProgressChanged -= this.OnDownloadProgressChanged;
                 this.downloadWebClient.OpenReadCompleted       -= this.OnOpenReadCompleted;
                 this.downloadWebClient.CancelAsync();
                 this.downloadWebClient.Dispose();
                 this.downloadWebClient = null;
             }
         }
     }
     catch (Exception)
     {
     }
 }
Exemplo n.º 3
0
        private DownloadWebClient CreateWebClient()
        {
            DownloadWebClient webClient = new DownloadWebClient();

            webClient.DownloadFileCompleted   += this.OnDownloadCompleted;
            webClient.DownloadProgressChanged += this.OnDownloadProgressChanged;
            webClient.OpenReadCompleted       += this.OnOpenReadCompleted;
            return(webClient);
        }
Exemplo n.º 4
0
        private void Download(Uri source, string fileDestination, long totalBytesToReceive)
        {
            try
            {
                FileUtils.TryGetFileSize(fileDestination, out long seekPosition);

                this.TryCleanupExistingDownloadWebClient();
                this.downloadWebClient = this.CreateWebClient();
                this.downloadWebClient.OpenReadAsync(source, seekPosition);
            }
            catch (Exception e)
            {
                if (!this.AttemptDownload())
                {
                    this.InvokeDownloadCompleted(CompletedState.Failed, this.localFileName, e);
                }
            }
        }
Exemplo n.º 5
0
        private void TriggerWebClientDownloadFileAsync()
        {
            try
            {
                this.isFallback = true;
                string destinationDirectory = Path.GetDirectoryName(this.localFileName);
                if (destinationDirectory != null && !Directory.Exists(destinationDirectory))
                {
                    Directory.CreateDirectory(destinationDirectory);
                }
                this.TryCleanupExistingDownloadWebClient();

                this.downloadWebClient = this.CreateWebClient();
                this.downloadWebClient.DownloadFileAsync(this.fileSource, this.localFileName);
            }
            catch (Exception ex)
            {
                if (!this.AttemptDownload())
                {
                    this.InvokeDownloadCompleted(CompletedState.Failed, this.localFileName, ex);
                }
            }
        }
Exemplo n.º 6
0
        private void TriggerWebClientDownloadFileAsync()
        {
            try
            {
                isFallback = true;
                var destinationDirectory = Path.GetDirectoryName(localFileName);
                if (destinationDirectory != null && !Directory.Exists(destinationDirectory))
                {
                    Directory.CreateDirectory(destinationDirectory);
                }
                TryCleanupExistingDownloadWebClient();

                downloadWebClient = CreateWebClient();
                downloadWebClient.DownloadFileAsync(fileSource, localFileName);
            }
            catch (Exception ex)
            {
                if (!AttemptDownload())
                {
                    InvokeDownloadCompleted(CompletedState.Failed, localFileName, ex);
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// OnDownloadCompleted event handler
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="args">AsyncCompletedEventArgs instance</param>
        protected void OnDownloadCompleted(object sender, AsyncCompletedEventArgs args)
        {
            DownloadWebClient webClient = sender as DownloadWebClient;

            if (webClient == null)
            {
                //logger.Warn("Wrong sender in OnDownloadCompleted: Actual:{0} Expected:{1}", sender.GetType(), typeof(DownloadWebClient));
                this.InvokeDownloadCompleted(CompletedState.Failed, this.localFileName);
                return;
            }

            if (args.Cancelled)
            {
                //logger.Debug("Download cancelled. Source: {0} Destination: {1}", fileSource, localFileName);
                this.DeleteDownloadedFile();

                this.InvokeDownloadCompleted(CompletedState.Canceled, this.localFileName);
                this.readyToDownload.Set();
            }
            else if (args.Error != null)
            {
                if (this.isFallback)
                {
                    this.DeleteDownloadedFile();
                }

                ////We may have NameResolutionFailure on internet connectivity problem.
                ////We don't use DnsFallbackResolver if we successfully started downloading, and then got internet problem.
                ////If we change [this.fileSource] here - we lose downloaded chunk in Cache (i.e. we create a new Cache item for new [this.fileSource]
                if (this.attemptNumber == 1 && this.DnsFallbackResolver != null && this.IsNameResolutionFailure(args.Error))
                {
                    Uri newFileSource = this.DnsFallbackResolver.Resolve(this.fileSource);
                    if (newFileSource != null)
                    {
                        this.fileSource = newFileSource;
                        this.AttemptDownload();
                        return;
                    }
                }

                if (!this.AttemptDownload())
                {
                    this.InvokeDownloadCompleted(CompletedState.Failed, null, args.Error);
                    this.readyToDownload.Set();
                }
            }
            else
            {
                if (this.useFileNameFromServer)
                {
                    this.localFileName = this.ApplyNewFileName(this.localFileName, webClient.GetOriginalFileNameFromDownload());
                }

                if (this.UseCaching)
                {
                    this.downloadCache.Add(this.fileSource, this.localFileName, webClient.ResponseHeaders);
                }

                ////we may have the destination file not immediately closed after downloading
                this.WaitFileClosed(this.localFileName, TimeSpan.FromSeconds(3));

                this.InvokeDownloadCompleted(CompletedState.Succeeded, this.localFileName, null);
                this.readyToDownload.Set();
            }
        }