private async Task Fetch <TCached>(IRetry retry, Func <Uri, byte[], TCached> factory, WebResponse webResponse, CancellationToken cancellationToken) where TCached : class
        {
label_12:
            HttpWebRequest request = this.CreateRequest();

            using (HttpWebResponse response = await this._webReader.SendAsync(request, true, cancellationToken, webResponse).ConfigureAwait(false))
            {
                int statusCode = (int)response.StatusCode;
                if (statusCode >= 200 && statusCode < 300)
                {
                    this._firstRequestCompleted = true;
                    Func <Uri, byte[], TCached> func1 = factory;
                    Uri responseUri = response.ResponseUri;
                    Func <Uri, byte[], TCached> func2 = func1;
                    HttpWebRequestWebCache      webRequestWebCache = this;
                    byte[] numArray = await this.FetchObject(response, cancellationToken).ConfigureAwait(false);

                    // ISSUE: variable of a boxed type
                    var local = (object)func2(responseUri, numArray);
                    webRequestWebCache._cachedObject = (object)local;
                }
                else if (HttpStatusCode.NotModified != response.StatusCode)
                {
                    if (RetryPolicy.IsRetryable(response.StatusCode))
                    {
                        if (await retry.CanRetryAfterDelayAsync(cancellationToken).ConfigureAwait(false))
                        {
                            goto label_12;
                        }
                    }
                    this._cachedObject = (object)null;
                    throw new WebException("Unable to fetch");
                }
            }
        }
        private async Task Fetch <TCached>(IRetry retry, Func <Uri, byte[], TCached> factory, WebResponse webResponse, CancellationToken cancellationToken) where TCached : class
        {
            while (true)
            {
                using (HttpRequestMessage request = this.CreateRequest())
                {
                    using (HttpResponseMessage response = await this._webReader.SendAsync(request, HttpCompletionOption.ResponseContentRead, cancellationToken, webResponse).ConfigureAwait(false))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            this._firstRequestCompleted = true;
                            Func <Uri, byte[], TCached> func1 = factory;
                            Uri requestUri = response.RequestMessage.RequestUri;
                            Func <Uri, byte[], TCached> func2 = func1;
                            HttpClientWebCache          httpClientWebCache = this;
                            byte[] numArray = await this.FetchObject(response).ConfigureAwait(false);

                            // ISSUE: variable of a boxed type
                            var local = (object)func2(requestUri, numArray);
                            httpClientWebCache._cachedObject = (object)local;
                            break;
                        }
                        if (HttpStatusCode.NotModified != response.StatusCode)
                        {
                            if (RetryPolicy.IsRetryable(response.StatusCode))
                            {
                                if (await retry.CanRetryAfterDelayAsync(cancellationToken).ConfigureAwait(false))
                                {
                                    goto label_16;
                                }
                            }
                            this._cachedObject = (object)null;
                            response.EnsureSuccessStatusCode();
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                label_16 :;
            }
        }
        public static async Task <TReturn> ReadStreamAsync <TReturn>(this IWebReader webReader, Uri url, IRetry retry, Func <Uri, Stream, TReturn> reader, CancellationToken cancellationToken)
        {
            TReturn @return;

            while (true)
            {
                using (IWebStreamResponse webStreamResponse = await webReader.GetWebStreamAsync(url, true, cancellationToken, (Uri)null, new long?(), new long?(), (WebResponse)null).ConfigureAwait(false))
                {
                    if (webStreamResponse.IsSuccessStatusCode)
                    {
                        Uri actualUrl = webStreamResponse.ActualUrl;
                        using (Stream stream = await webStreamResponse.GetStreamAsync(cancellationToken).ConfigureAwait(false))
                        {
                            using (MemoryStream memoryStream = new MemoryStream((int)(webStreamResponse.ContentLength ?? 4096L)))
                            {
                                await stream.CopyToAsync((Stream)memoryStream, 4096, cancellationToken).ConfigureAwait(false);

                                memoryStream.Position = 0L;
                                @return = reader(actualUrl, (Stream)memoryStream);
                                break;
                            }
                        }
                    }
                    else
                    {
                        if (!RetryPolicy.IsRetryable((HttpStatusCode)webStreamResponse.HttpStatusCode))
                        {
                            webStreamResponse.EnsureSuccessStatusCode();
                        }
                        bool canRetry = await retry.CanRetryAfterDelayAsync(cancellationToken).ConfigureAwait(false);

                        if (!canRetry)
                        {
                            webStreamResponse.EnsureSuccessStatusCode();
                        }
                    }
                }
            }
            return(@return);
        }
        private async Task Fetch <TCached>(IRetry retry, Func <Uri, byte[], TCached> factory, SM.Media.Web.WebResponse webResponse, CancellationToken cancellationToken) where TCached : class
        {
label_12:
            HttpConnectionRequest request = this.CreateRequest();

            using (IHttpConnectionResponse response = await this._webReader.SendAsync(request, true, cancellationToken, webResponse).ConfigureAwait(false))
            {
                if (response.IsSuccessStatusCode)
                {
                    this._firstRequestCompleted = true;
                    Func <Uri, byte[], TCached> func1 = factory;
                    Uri responseUri = response.ResponseUri;
                    Func <Uri, byte[], TCached> func2 = func1;
                    HttpConnectionWebCache      connectionWebCache = this;
                    byte[] numArray = await this.FetchObject(response, cancellationToken).ConfigureAwait(false);

                    // ISSUE: variable of a boxed type
                    __Boxed <TCached> local = (object)func2(responseUri, numArray);
                    connectionWebCache._cachedObject = (object)local;
                }
                else
                {
                    HttpStatusCode statusCode = response.Status.StatusCode;
                    if (HttpStatusCode.NotModified != statusCode)
                    {
                        if (RetryPolicy.IsRetryable(statusCode))
                        {
                            if (await retry.CanRetryAfterDelayAsync(cancellationToken).ConfigureAwait(false))
                            {
                                goto label_12;
                            }
                        }
                        this._cachedObject = (object)null;
                        response.EnsureSuccessStatusCode();
                        throw new WebException("Unable to fetch " + (object)request.Url);
                    }
                }
            }
        }
예제 #5
0
        private Task OpenStream(Action <ISegmentMetadata> setMetadata, CancellationToken cancellationToken)
        {
            this.ThrowIfDisposed();
            WebResponse webResponse = new WebResponse();
            IRetry      retry       = this._retryManager.CreateRetry(2, 200, new Func <Exception, bool>(RetryPolicy.IsWebExceptionRetryable));

            return(RetryExtensions.CallAsync(retry, (Func <Task>)(async() =>
            {
                long?nullable1;
                long?nullable2;
                while (true)
                {
                    if (this._startOffset.HasValue && this._endOffset.HasValue)
                    {
                        nullable1 = this._endOffset;
                        nullable2 = this._startOffset;
                        nullable1 = nullable1.HasValue & nullable2.HasValue ? new long?(nullable1.GetValueOrDefault() - nullable2.GetValueOrDefault()) : new long?();
                        long?nullable3;
                        if (!nullable1.HasValue)
                        {
                            nullable2 = new long?();
                            nullable3 = nullable2;
                        }
                        else
                        {
                            nullable3 = new long?(nullable1.GetValueOrDefault() + 1L);
                        }
                        this._expectedBytes = nullable3;
                    }
                    else
                    {
                        this._expectedBytes = new long?();
                    }
                    this._response = await this._webReader.GetWebStreamAsync(this._actualUrl ?? this._segment.Url, false, cancellationToken, this._segment.ParentUrl, this._startOffset, this._endOffset, webResponse).ConfigureAwait(false);
                    if (!this._response.IsSuccessStatusCode)
                    {
                        HttpStatusCode statusCode = (HttpStatusCode)this._response.HttpStatusCode;
                        if (HttpStatusCode.NotFound != statusCode && !RetryPolicy.IsRetryable(statusCode))
                        {
                            this._response.EnsureSuccessStatusCode();
                        }
                        bool canRetry = await retry.CanRetryAfterDelayAsync(cancellationToken).ConfigureAwait(false);
                        if (!canRetry)
                        {
                            if ((Uri)null != this._actualUrl && this._actualUrl != this._segment.Url)
                            {
                                this._actualUrl = (Uri)null;
                            }
                            else
                            {
                                this._response.EnsureSuccessStatusCode();
                            }
                        }
                        this._response.Dispose();
                        this._response = (IWebStreamResponse)null;
                    }
                    else
                    {
                        break;
                    }
                }
                this._actualUrl = this._response.ActualUrl;
                long?contentLength = this._response.ContentLength;
                if (!this._endOffset.HasValue)
                {
                    nullable1 = contentLength;
                    long?nullable3;
                    if (!nullable1.HasValue)
                    {
                        nullable2 = new long?();
                        nullable3 = nullable2;
                    }
                    else
                    {
                        nullable3 = new long?(nullable1.GetValueOrDefault() - 1L);
                    }
                    this._endOffset = nullable3;
                }
                if (!this._expectedBytes.HasValue)
                {
                    this._expectedBytes = contentLength;
                }
                SegmentReader segmentReader1 = this;
                ConfiguredTaskAwaitable <Stream> configuredTaskAwaitable = this._response.GetStreamAsync(cancellationToken).ConfigureAwait(false);
                PositionStream positionStream = new PositionStream(await configuredTaskAwaitable);
                segmentReader1._responseStream = (Stream)positionStream;
                Task <Stream> filterStreamTask = this._segment.CreateFilterAsync(this._responseStream, cancellationToken);
                if (null != filterStreamTask)
                {
                    SegmentReader segmentReader2 = this;
                    configuredTaskAwaitable = filterStreamTask.ConfigureAwait(false);
                    Stream stream = await configuredTaskAwaitable;
                    segmentReader2._readStream = stream;
                }
                else
                {
                    this._readStream = this._responseStream;
                }
                ISegmentMetadata segmentMetadata = this._webMetadataFactory.CreateSegmentMetadata(webResponse, (ContentType)null);
                setMetadata(segmentMetadata);
            }), cancellationToken));
        }