Пример #1
0
        internal async Task <BinaryResponse> UrlGetToBinaryWithProgressRetry(string request, string referer = null)
        {
            if (string.IsNullOrEmpty(request))
            {
                ArchiLogger.LogNullError(nameof(request));
                return(null);
            }

            BinaryResponse response = null;

            for (byte i = 0; (i < MaxTries) && (response == null); i++)
            {
                response = await UrlGetToBinaryWithProgress(request, referer).ConfigureAwait(false);
            }

            if (response != null)
            {
                return(response);
            }

            ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, MaxTries));
            ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request));
            return(null);
        }
Пример #2
0
        internal async Task <BinaryResponse> UrlGetToBinaryWithProgress(string request, string referer = null, ERequestOptions requestOptions = ERequestOptions.None, byte maxTries = MaxTries)
        {
            if (string.IsNullOrEmpty(request) || (maxTries == 0))
            {
                ArchiLogger.LogNullError(nameof(request) + " || " + nameof(maxTries));

                return(null);
            }

            BinaryResponse result = null;

            for (byte i = 0; i < maxTries; i++)
            {
                const byte printPercentage = 10;
                const byte maxBatches      = 99 / printPercentage;

                using (HttpResponseMessage response = await InternalGet(request, referer, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false)) {
                    if (response == null)
                    {
                        continue;
                    }

                    if (response.StatusCode.IsClientErrorCode())
                    {
                        if (requestOptions.HasFlag(ERequestOptions.ReturnClientErrors))
                        {
                            result = new BinaryResponse(response);
                        }

                        break;
                    }

                    ArchiLogger.LogGenericDebug("0%...");

                    uint contentLength = (uint)response.Content.Headers.ContentLength.GetValueOrDefault();

                    using (MemoryStream ms = new MemoryStream((int)contentLength)) {
                        try {
                            using (Stream contentStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) {
                                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 >= maxBatches))
                                    {
                                        continue;
                                    }

                                    readThisBatch += (uint)read;

                                    if (readThisBatch < contentLength / printPercentage)
                                    {
                                        continue;
                                    }

                                    readThisBatch -= contentLength / printPercentage;
                                    ArchiLogger.LogGenericDebug((++batch * printPercentage) + "%...");
                                }
                            }
                        } catch (Exception e) {
                            ArchiLogger.LogGenericDebuggingException(e);

                            return(null);
                        }

                        ArchiLogger.LogGenericDebug("100%");

                        return(new BinaryResponse(response, ms.ToArray()));
                    }
                }
            }

            if (maxTries > 1)
            {
                ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, maxTries));
                ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request));
            }

            return(result);
        }
Пример #3
0
        internal async Task <BinaryResponse?> UrlGetToBinary(string request, string?referer = null, ERequestOptions requestOptions = ERequestOptions.None, byte maxTries = MaxTries, IProgress <byte>?progressReporter = null)
        {
            if (string.IsNullOrEmpty(request) || (maxTries == 0))
            {
                throw new ArgumentNullException(nameof(request) + " || " + nameof(maxTries));
            }

            BinaryResponse?result = null;

            for (byte i = 0; i < maxTries; i++)
            {
                await using StreamResponse? response = await UrlGetToStream(request, referer, requestOptions | ERequestOptions.ReturnClientErrors, 1).ConfigureAwait(false);

                if (response?.StatusCode.IsClientErrorCode() == true)
                {
                    if (requestOptions.HasFlag(ERequestOptions.ReturnClientErrors))
                    {
                        result = new BinaryResponse(response);
                    }

                    break;
                }

                if (response?.StatusCode.IsServerErrorCode() == true)
                {
                    if (requestOptions.HasFlag(ERequestOptions.ReturnServerErrors))
                    {
                        result = new BinaryResponse(response);
                    }

                    break;
                }

                if (response?.Content == null)
                {
                    continue;
                }

                progressReporter?.Report(0);

#if NETFRAMEWORK
                using MemoryStream ms = new MemoryStream((int)response.Length);
#else
                await using MemoryStream ms = new MemoryStream((int)response.Length);
#endif

                try {
                    byte batch             = 0;
                    uint readThisBatch     = 0;
                    uint batchIncreaseSize = response.Length / 100;

                    byte[] buffer = new byte[8192];                     // This is HttpClient's buffer, using more doesn't make sense

                    while (response.Content.CanRead)
                    {
                        int read = await response.Content.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);

                        if (read == 0)
                        {
                            break;
                        }

                        await ms.WriteAsync(buffer, 0, read).ConfigureAwait(false);

                        if ((batchIncreaseSize == 0) || (batch >= 99))
                        {
                            continue;
                        }

                        readThisBatch += (uint)read;

                        if (readThisBatch < batchIncreaseSize)
                        {
                            continue;
                        }

                        readThisBatch -= batchIncreaseSize;
                        progressReporter?.Report(++batch);
                    }
                } catch (Exception e) {
                    ArchiLogger.LogGenericDebuggingException(e);

                    return(null);
                }

                progressReporter?.Report(100);

                return(new BinaryResponse(response, ms.ToArray()));
            }

            if (maxTries > 1)
            {
                ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, maxTries));
                ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request));
            }

            return(result);
        }
Пример #4
0
        internal async Task <BinaryResponse> UrlGetToBinaryWithProgress(string request, string referer = null, ERequestOptions requestOptions = ERequestOptions.None, byte maxTries = MaxTries)
        {
            if (string.IsNullOrEmpty(request) || (maxTries == 0))
            {
                ArchiLogger.LogNullError(nameof(request) + " || " + nameof(maxTries));

                return(null);
            }

            BinaryResponse result = null;

            for (byte i = 0; i < maxTries; i++)
            {
                const byte printPercentage = 10;
                const byte maxBatches      = 99 / printPercentage;

                await using StreamResponse response = await UrlGetToStream(request, referer, requestOptions | ERequestOptions.ReturnClientErrors, 1).ConfigureAwait(false);

                if (response?.StatusCode.IsClientErrorCode() == true)
                {
                    if (requestOptions.HasFlag(ERequestOptions.ReturnClientErrors))
                    {
                        result = new BinaryResponse(response);
                    }

                    break;
                }

                if (response?.Content == null)
                {
                    continue;
                }

                ArchiLogger.LogGenericDebug("0%...");

#if !NETFRAMEWORK
                await
#endif
                using MemoryStream ms = new MemoryStream((int)response.Length);

                try {
                    byte   batch         = 0;
                    uint   readThisBatch = 0;
                    byte[] buffer        = new byte[8192];              // This is HttpClient's buffer, using more doesn't make sense

                    while (response.Content.CanRead)
                    {
                        int read = await response.Content.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);

                        if (read == 0)
                        {
                            break;
                        }

                        await ms.WriteAsync(buffer, 0, read).ConfigureAwait(false);

                        if ((response.Length == 0) || (batch >= maxBatches))
                        {
                            continue;
                        }

                        readThisBatch += (uint)read;

                        if (readThisBatch < response.Length / printPercentage)
                        {
                            continue;
                        }

                        readThisBatch -= response.Length / printPercentage;
                        ArchiLogger.LogGenericDebug((++batch * printPercentage) + "%...");
                    }
                } catch (Exception e) {
                    ArchiLogger.LogGenericDebuggingException(e);

                    return(null);
                }

                ArchiLogger.LogGenericDebug("100%");

                return(new BinaryResponse(response, ms.ToArray()));
            }

            if (maxTries > 1)
            {
                ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, maxTries));
                ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request));
            }

            return(result);
        }