Example #1
0
        /// <inheritdoc />
        public override async Task <int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            int?result = null;
            await Retry.Do(
                CustomHttpClient.RetryTimes,
                CustomHttpClient.RetryDelay,
                async() =>
            {
                try
                {
                    if ((position != lastposition) || (responseStream == null))
                    {
                        if (position != lastposition)
                        {
                            Close();
                        }

                        lastposition = position;

                        var client = await http.GetHttpClient(url);
                        if (position != 0)
                        {
                            client.AddRange(position);
                        }

                        client.Method = "GET";

                        response      = (HttpWebResponse)await client.GetResponseAsync();
                        var lengthStr = response.GetResponseHeader("Content-Length");
                        long len;
                        if (long.TryParse(lengthStr, out len))
                        {
                            length = position + len;
                        }

                        responseStream = response.GetResponseStream();
                        if (InitialSeek.HasValue)
                        {
                            responseStream?.Seek(InitialSeek.Value, SeekOrigin.Current);
                        }
                    }

                    Contract.Assert(responseStream != null, "responseStream!=null");

                    result = await responseStream.ReadAsync(buffer, offset, count, cancellationToken);

                    position     += result.Value;
                    lastposition += result.Value;
                    return(true);
                }
                catch (Exception)
                {
                    Close();
                    throw;
                }
            }, http.GeneralExceptionProcessor).ConfigureAwait(false);

            if (result == null)
            {
                throw new NullReferenceException("Read result was not set");
            }

            return(result.Value);
        }