Skip to content

Commit

Permalink
Merge pull request #119 from /issues/111-CancellationToken
Browse files Browse the repository at this point in the history
  • Loading branch information
tpill90 authored May 7, 2024
2 parents ccb0254 + baa1fe1 commit a715d1c
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions BattleNetPrefill/Web/CdnRequestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,9 @@ public async Task<byte[]> GetRequestAsBytesAsync(Request request, ProgressTask t
requestMessage.Headers.Range = new RangeHeaderValue(startBytes, endBytes);
}

using var responseMessage = await _client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead);
await using Stream responseStream = await responseMessage.Content.ReadAsStreamAsync();
using var cts = new CancellationTokenSource();
using var responseMessage = await _client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, cts.Token);
await using Stream responseStream = await responseMessage.Content.ReadAsStreamAsync(cts.Token);

responseMessage.EnsureSuccessStatusCode();
if (writeToDevNull)
Expand All @@ -256,7 +257,7 @@ public async Task<byte[]> GetRequestAsBytesAsync(Request request, ProgressTask t
while (true)
{
// Dump the received data, so we don't have to waste time writing it to disk.
var read = await responseStream.ReadAsync(buffer, 0, buffer.Length);
var read = await responseStream.ReadAsync(buffer, cts.Token);
if (read == 0)
{
return null;
Expand All @@ -274,7 +275,7 @@ public async Task<byte[]> GetRequestAsBytesAsync(Request request, ProgressTask t
}

await using var memoryStream = new MemoryStream();
await responseStream.CopyToAsync(memoryStream);
await responseStream.CopyToAsync(memoryStream, cts.Token);

var byteArray = memoryStream.ToArray();
if (SkipDiskCache)
Expand All @@ -285,7 +286,7 @@ public async Task<byte[]> GetRequestAsBytesAsync(Request request, ProgressTask t
// Cache to disk
FileInfo file = new FileInfo(Path.Combine(AppConfig.CacheDir + uri.AbsolutePath));
file.Directory.Create();
await File.WriteAllBytesAsync(file.FullName, byteArray);
await File.WriteAllBytesAsync(file.FullName, byteArray, cts.Token);

return await Task.FromResult(byteArray);
}
Expand Down

0 comments on commit a715d1c

Please sign in to comment.