public async Task <TCached> ReadAsync <TCached>(Func <Uri, byte[], TCached> factory, CancellationToken cancellationToken, WebResponse webResponse = null) where TCached : class { if (null == (object)(this._cachedObject as TCached)) { this._cachedObject = (object)null; } IRetry retry = RetryManagerExtensions.CreateWebRetry(this._retryManager, 2, 250); await RetryExtensions.CallAsync(retry, (Func <Task>)(() => this.Fetch <TCached>(retry, factory, webResponse, cancellationToken)), cancellationToken).ConfigureAwait(false); return(this._cachedObject as TCached); }
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)); }