From 1e6e28cd575b1b854b9af7bfe4f1f1759da03903 Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Sat, 18 Feb 2023 22:37:11 -0700 Subject: [PATCH] Start downloading asynchronously --- Source/AaxDecrypter/AudiobookDownloadBase.cs | 2 ++ Source/AaxDecrypter/NetworkFileStream.cs | 21 ++++++------------- .../UnencryptedAudiobookDownloader.cs | 3 +-- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/Source/AaxDecrypter/AudiobookDownloadBase.cs b/Source/AaxDecrypter/AudiobookDownloadBase.cs index 43ceb5e0..ca730ca1 100644 --- a/Source/AaxDecrypter/AudiobookDownloadBase.cs +++ b/Source/AaxDecrypter/AudiobookDownloadBase.cs @@ -3,6 +3,7 @@ using Dinah.Core.Net.Http; using Dinah.Core.StepRunner; using FileManager; using System; +using System.Diagnostics; using System.IO; using System.Threading.Tasks; @@ -66,6 +67,7 @@ namespace AaxDecrypter public async Task RunAsync() { + await InputFileStream.BeginDownloadingAsync(); var progressTask = Task.Run(reportProgress); AsyncSteps[$"Cleanup"] = CleanupAsync; diff --git a/Source/AaxDecrypter/NetworkFileStream.cs b/Source/AaxDecrypter/NetworkFileStream.cs index c7283ee8..ca89532b 100644 --- a/Source/AaxDecrypter/NetworkFileStream.cs +++ b/Source/AaxDecrypter/NetworkFileStream.cs @@ -136,10 +136,10 @@ namespace AaxDecrypter /// Begins downloading to in a background thread. /// The downloader - private Task BeginDownloading() + public async Task BeginDownloadingAsync() { if (ContentLength != 0 && WritePosition == ContentLength) - return Task.CompletedTask; + return; if (ContentLength != 0 && WritePosition > ContentLength) throw new WebException($"Specified write position (0x{WritePosition:X10}) is larger than {nameof(ContentLength)} (0x{ContentLength:X10})."); @@ -149,7 +149,7 @@ namespace AaxDecrypter foreach (var header in RequestHeaders) request.Headers.Add(header.Key, header.Value); - var response = new HttpClient().Send(request, HttpCompletionOption.ResponseHeadersRead, _cancellationSource.Token); + var response = await new HttpClient().SendAsync(request, HttpCompletionOption.ResponseHeadersRead, _cancellationSource.Token); if (response.StatusCode != HttpStatusCode.PartialContent) throw new WebException($"Server at {Uri.Host} responded with unexpected status code: {response.StatusCode}."); @@ -159,11 +159,11 @@ namespace AaxDecrypter if (WritePosition == 0) ContentLength = response.Content.Headers.ContentLength.GetValueOrDefault(); - var networkStream = response.Content.ReadAsStream(_cancellationSource.Token); + var networkStream = await response.Content.ReadAsStreamAsync(_cancellationSource.Token); _downloadedPiece = new EventWaitHandle(false, EventResetMode.AutoReset); //Download the file in the background. - return Task.Run(() => DownloadFile(networkStream), _cancellationSource.Token); + _backgroundDownloadTask = Task.Run(() => DownloadFile(networkStream), _cancellationSource.Token); } /// Download to . @@ -247,14 +247,7 @@ namespace AaxDecrypter public override bool CanWrite => false; [JsonIgnore] - public override long Length - { - get - { - _backgroundDownloadTask ??= BeginDownloading(); - return ContentLength; - } - } + public override long Length => ContentLength; [JsonIgnore] public override long Position { get => _readFile.Position; set => Seek(value, SeekOrigin.Begin); } @@ -274,8 +267,6 @@ namespace AaxDecrypter public override int Read(byte[] buffer, int offset, int count) { - _backgroundDownloadTask ??= BeginDownloading(); - var toRead = Math.Min(count, Length - Position); WaitToPosition(Position + toRead); return IsCancelled ? 0 : _readFile.Read(buffer, offset, count); diff --git a/Source/AaxDecrypter/UnencryptedAudiobookDownloader.cs b/Source/AaxDecrypter/UnencryptedAudiobookDownloader.cs index ad9144ee..138c8ea0 100644 --- a/Source/AaxDecrypter/UnencryptedAudiobookDownloader.cs +++ b/Source/AaxDecrypter/UnencryptedAudiobookDownloader.cs @@ -26,8 +26,7 @@ namespace AaxDecrypter protected override async Task Step_DownloadAndDecryptAudiobookAsync() { - // MUST put InputFileStream.Length first, because it starts background downloader. - while (InputFileStream.Length > InputFilePosition && !InputFileStream.IsCancelled) + while (InputFilePosition < InputFileStream.Length && !InputFileStream.IsCancelled) await Task.Delay(200); if (IsCanceled)