Skip to content

Commit

Permalink
Dispose HttpResponseMessage while disposing BridgedNetworkStream
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-nyan committed Dec 8, 2023
1 parent dfe216b commit 4f8e1f1
Showing 4 changed files with 23 additions and 13 deletions.
18 changes: 15 additions & 3 deletions CollapseLauncher/Classes/RegionManagement/BridgedNetworkStream.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
using System;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace CollapseLauncher
{
class BridgedNetworkStream : Stream
{
private protected readonly HttpResponseMessage _networkResponse;
private protected readonly Stream _networkStream;
private protected long _networkLength;
private protected long _currentPosition = 0;

public BridgedNetworkStream(Stream networkStream, long networkLength)
internal static async ValueTask<BridgedNetworkStream> CreateStream(HttpResponseMessage networkResponse, CancellationToken token)
{
Stream networkStream = await networkResponse?.Content?.ReadAsStreamAsync(token);
return new BridgedNetworkStream(networkResponse, networkStream);
}

public BridgedNetworkStream(HttpResponseMessage networkResponse, Stream networkStream)
{
_networkResponse = networkResponse;
_networkLength = networkResponse?.Content?.Headers?.ContentLength ?? 0;
_networkStream = networkStream;
_networkLength = networkLength;
}

~BridgedNetworkStream() => Dispose();
@@ -77,7 +88,8 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
if (disposing)
{
_networkStream.Dispose();
_networkResponse?.Dispose();
_networkStream?.Dispose();
}
}
}
8 changes: 2 additions & 6 deletions CollapseLauncher/Classes/RegionManagement/FallbackCDNUtil.cs
Original file line number Diff line number Diff line change
@@ -331,16 +331,12 @@ public static async ValueTask<HttpResponseMessage> GetURLHttpResponse(string URL

public static async ValueTask<BridgedNetworkStream> GetHttpStreamFromResponse(string URL, CancellationToken token)
{
using HttpResponseMessage responseMsg = await GetURLHttpResponse(URL, token);
HttpResponseMessage responseMsg = await GetURLHttpResponse(URL, token);
return await GetHttpStreamFromResponse(responseMsg, token);
}

public static async ValueTask<BridgedNetworkStream> GetHttpStreamFromResponse(HttpResponseMessage responseMsg, CancellationToken token)
{
long networkLength = responseMsg?.Content?.Headers?.ContentLength ?? 0;

return new BridgedNetworkStream(await responseMsg.Content.ReadAsStreamAsync(token), networkLength);
}
=> await BridgedNetworkStream.CreateStream(responseMsg, token);

// Re-send the events to the static DownloadProgress
private static void HttpInstance_DownloadProgressAdapter(object sender, DownloadEvent e) => DownloadProgress?.Invoke(sender, e);
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.Linq;
@@ -250,6 +251,7 @@ internal static async void TryDownloadToCompletenessAsync(string url, FileInfo f

internal static async ValueTask TryDownloadToCompleteness(string url, FileInfo fileInfo, CancellationToken token)
{
byte[] buffer = ArrayPool<byte>.Shared.Rent(4 << 10);
try
{
// Try get the remote stream and download the file
@@ -274,7 +276,6 @@ internal static async ValueTask TryDownloadToCompleteness(string url, FileInfo f
// Copy (and download) the remote streams to local
LogWriteLine($"Start downloading resource from: {url}", LogType.Default, true);
int read = 0;
byte[] buffer = new byte[4 << 10];
while ((read = await netStream.ReadAsync(buffer, token)) > 0)
await outStream.WriteAsync(buffer, 0, read, token);

@@ -291,6 +292,10 @@ internal static async ValueTask TryDownloadToCompleteness(string url, FileInfo f
ErrorSender.SendException(ex, ErrorType.Connection);
#endif
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}
}

private async ValueTask FetchLauncherDownloadInformation(CancellationToken token, PresetConfigV2 Preset)
3 changes: 0 additions & 3 deletions CollapseLauncher/Classes/RepairManagement/Honkai/Fetch.cs
Original file line number Diff line number Diff line change
@@ -13,14 +13,11 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Windows.Media.Protection.PlayReady;
using static Hi3Helper.Data.ConverterTool;
using static Hi3Helper.Locale;
using static Hi3Helper.Logger;

0 comments on commit 4f8e1f1

Please sign in to comment.