This commit is contained in:
JustArchi
2017-12-16 11:34:04 +01:00
parent 87de865877
commit c19a5c78da
3 changed files with 50 additions and 13 deletions

View File

@@ -168,7 +168,7 @@ namespace ArchiSteamFarm {
ArchiLogger.LogGenericInfo(string.Format(Strings.UpdateDownloadingNewVersion, newVersion, binaryAsset.Size / 1024 / 1024)); ArchiLogger.LogGenericInfo(string.Format(Strings.UpdateDownloadingNewVersion, newVersion, binaryAsset.Size / 1024 / 1024));
byte[] result = await Program.WebBrowser.UrlGetToBytesRetry(binaryAsset.DownloadURL).ConfigureAwait(false); byte[] result = await Program.WebBrowser.UrlGetToBytesWithProgressRetry(binaryAsset.DownloadURL).ConfigureAwait(false);
if (result == null) { if (result == null) {
return null; return null;
} }

View File

@@ -3,8 +3,8 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework> <TargetFramework>netcoreapp2.0</TargetFramework>
<AssemblyVersion>3.0.5.6</AssemblyVersion> <AssemblyVersion>3.0.5.4</AssemblyVersion>
<FileVersion>3.0.5.6</FileVersion> <FileVersion>3.0.5.4</FileVersion>
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
<ErrorReport>none</ErrorReport> <ErrorReport>none</ErrorReport>
<ApplicationIcon>ASF.ico</ApplicationIcon> <ApplicationIcon>ASF.ico</ApplicationIcon>

View File

@@ -21,6 +21,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -88,7 +89,7 @@ namespace ArchiSteamFarm {
return htmlDocument; return htmlDocument;
} }
internal async Task<byte[]> UrlGetToBytesRetry(string request, string referer = null) { internal async Task<byte[]> UrlGetToBytesWithProgressRetry(string request, string referer = null) {
if (string.IsNullOrEmpty(request)) { if (string.IsNullOrEmpty(request)) {
ArchiLogger.LogNullError(nameof(request)); ArchiLogger.LogNullError(nameof(request));
return null; return null;
@@ -96,7 +97,7 @@ namespace ArchiSteamFarm {
byte[] result = null; byte[] result = null;
for (byte i = 0; (i < MaxTries) && (result == null); i++) { for (byte i = 0; (i < MaxTries) && (result == null); i++) {
result = await UrlGetToBytes(request, referer).ConfigureAwait(false); result = await UrlGetToBytesWithProgress(request, referer).ConfigureAwait(false);
} }
if (result != null) { if (result != null) {
@@ -277,18 +278,54 @@ namespace ArchiSteamFarm {
} }
} }
private async Task<byte[]> UrlGetToBytes(string request, string referer = null) { private async Task<byte[]> UrlGetToBytesWithProgress(string request, string referer = null) {
if (string.IsNullOrEmpty(request)) { if (string.IsNullOrEmpty(request)) {
ArchiLogger.LogNullError(nameof(request)); ArchiLogger.LogNullError(nameof(request));
return null; return null;
} }
using (HttpResponseMessage httpResponse = await UrlGetToResponse(request, referer).ConfigureAwait(false)) { const byte printPercentage = 10;
using (HttpResponseMessage httpResponse = await UrlGetToResponse(request, referer, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false)) {
if (httpResponse == null) { if (httpResponse == null) {
return null; return null;
} }
return await httpResponse.Content.ReadAsByteArrayAsync().ConfigureAwait(false); ASF.ArchiLogger.LogGenericDebug("0%...");
using (MemoryStream ms = new MemoryStream()) {
using (Stream contentStream = await httpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false)) {
long contentLength = httpResponse.Content.Headers.ContentLength.GetValueOrDefault();
byte batch = 0;
uint readThisBatch = 0;
byte[] buffer = new byte[8192]; // This is HttpClient's buffer, using more doesn't make sense
while (contentStream.CanRead) {
int read = await contentStream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
if (read == 0) {
break;
}
await ms.WriteAsync(buffer, 0, read).ConfigureAwait(false);
if ((contentLength == 0) || (batch >= printPercentage)) {
continue;
}
readThisBatch += (uint) read;
if (readThisBatch < contentLength / printPercentage) {
continue;
}
readThisBatch = 0;
ASF.ArchiLogger.LogGenericDebug(++batch * printPercentage + "%...");
}
}
ASF.ArchiLogger.LogGenericDebug("100%");
return ms.ToArray();
}
} }
} }
@@ -337,13 +374,13 @@ namespace ArchiSteamFarm {
return !string.IsNullOrEmpty(content) ? StringToHtmlDocument(content) : null; return !string.IsNullOrEmpty(content) ? StringToHtmlDocument(content) : null;
} }
private async Task<HttpResponseMessage> UrlGetToResponse(string request, string referer = null) { private async Task<HttpResponseMessage> UrlGetToResponse(string request, string referer = null, HttpCompletionOption httpCompletionOptions = HttpCompletionOption.ResponseContentRead) {
if (string.IsNullOrEmpty(request)) { if (string.IsNullOrEmpty(request)) {
ArchiLogger.LogNullError(nameof(request)); ArchiLogger.LogNullError(nameof(request));
return null; return null;
} }
HttpResponseMessage result = await UrlRequest(new Uri(request), HttpMethod.Get, null, referer).ConfigureAwait(false); HttpResponseMessage result = await UrlRequest(new Uri(request), HttpMethod.Get, null, referer, httpCompletionOptions).ConfigureAwait(false);
return result; return result;
} }
@@ -445,7 +482,7 @@ namespace ArchiSteamFarm {
return null; return null;
} }
private async Task<HttpResponseMessage> UrlRequest(Uri requestUri, HttpMethod httpMethod, IReadOnlyCollection<KeyValuePair<string, string>> data = null, string referer = null, byte maxRedirections = MaxTries) { private async Task<HttpResponseMessage> UrlRequest(Uri requestUri, HttpMethod httpMethod, IReadOnlyCollection<KeyValuePair<string, string>> data = null, string referer = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseContentRead, byte maxRedirections = MaxTries) {
if ((requestUri == null) || (httpMethod == null)) { if ((requestUri == null) || (httpMethod == null)) {
ArchiLogger.LogNullError(nameof(requestUri) + " || " + nameof(httpMethod)); ArchiLogger.LogNullError(nameof(requestUri) + " || " + nameof(httpMethod));
return null; return null;
@@ -467,7 +504,7 @@ namespace ArchiSteamFarm {
} }
try { try {
responseMessage = await HttpClient.SendAsync(requestMessage).ConfigureAwait(false); responseMessage = await HttpClient.SendAsync(requestMessage, httpCompletionOption).ConfigureAwait(false);
} catch (Exception e) { } catch (Exception e) {
ArchiLogger.LogGenericDebuggingException(e); ArchiLogger.LogGenericDebuggingException(e);
return null; return null;
@@ -512,7 +549,7 @@ namespace ArchiSteamFarm {
} }
} }
return await UrlRequest(redirectUri, httpMethod, data, referer, --maxRedirections).ConfigureAwait(false); return await UrlRequest(redirectUri, httpMethod, data, referer, httpCompletionOption, --maxRedirections).ConfigureAwait(false);
} }
} }
} }