Пример #1
0
 protected static MultiMirrorFileDownloadSpec GetDlSpec(FileQueueSpec spec,
     KeyValuePair<KeyValuePair<string, Func<IAbsoluteFilePath, bool>>, ITransferStatus> file) {
     var dlSpec = new MultiMirrorFileDownloadSpec(file.Key.Key, spec.Location.GetChildFileWithName(file.Key.Key)) {
         Progress = file.Value,
         Verification = file.Key.Value
     };
     dlSpec.LocalFile.MakeSureParentPathExists();
     return dlSpec;
 }
 public void Download(MultiMirrorFileDownloadSpec spec) {
     spec.Start();
     try {
         while (true) {
             var host = _mirrorStrategy.GetHost();
             if (TryDownload(spec, host))
                 break;
             Thread.Sleep(MillisecondsTimeout);
         }
     } catch (Exception) {
         spec.Fail();
         throw;
     }
 }
 public void Download(MultiMirrorFileDownloadSpec spec, CancellationToken token) {
     spec.Start();
     try {
         while (true) {
             token.ThrowIfCancellationRequested();
             var host = _mirrorStrategy.GetHost();
             if (TryDownload(spec, host))
                 break;
             Thread.Sleep(MillisecondsTimeout);
         }
     } catch (Exception) {
         spec.Fail();
         throw;
     }
 }
 public async Task DownloadAsync(MultiMirrorFileDownloadSpec spec) {
     spec.Start();
     try {
         while (true) {
             var host = _mirrorStrategy.GetHost();
             spec.UpdateHost(host);
             if (await TryDownloadAsync(spec, host).ConfigureAwait(false))
                 break;
             await Task.Delay(MillisecondsTimeout).ConfigureAwait(false);
         }
     } catch (Exception) {
         spec.Fail();
         throw;
     }
 }
 async Task<bool> TryDownloadAsync(MultiMirrorFileDownloadSpec spec, Uri host) {
     try {
         await TryDownloadFileAsync(spec, host).ConfigureAwait(false);
         spec.End();
         return true;
     } catch (TransferException) {} catch (VerificationError) {}
     return false;
 }
 bool TryDownload(MultiMirrorFileDownloadSpec spec, Uri host) {
     try {
         TryDownloadFile(spec, host);
         spec.End();
         return true;
     } catch (TransferException) {} catch (VerificationError) {}
     return false;
 }
 static FileDownloadSpec BuildSpec(MultiMirrorFileDownloadSpec spec, Uri host) {
     return spec.Progress == null
         ? new FileDownloadSpec(spec.GetUri(host), spec.LocalFile) {
             Verification = spec.Verification,
             CancellationToken = spec.CancellationToken
         }
         : new FileDownloadSpec(spec.GetUri(host), spec.LocalFile, spec.Progress) {
             Verification = spec.Verification,
             CancellationToken = spec.CancellationToken
         };
 }
 protected async Task TryDownloadFileAsync(MultiMirrorFileDownloadSpec spec, Uri host) {
     try {
         await _downloader.DownloadAsync(BuildSpec(spec, host))
             .ConfigureAwait(false);
     } catch (Exception) {
         _mirrorStrategy.Failure(host);
         throw;
     }
     _mirrorStrategy.Success(host);
 }
 protected void TryDownloadFile(MultiMirrorFileDownloadSpec spec, Uri host) {
     try {
         _downloader.Download(BuildSpec(spec, host));
     } catch (Exception) {
         _mirrorStrategy.Failure(host);
         throw;
     }
     _mirrorStrategy.Success(host);
 }