Пример #1
0
        private async Task <DownloadResponse> DownloadDataTaskAsync()
        {
            cancellationToken.ThrowIfCancellationRequested();

            using (var dataDownloader = new WebClientEx())
                using (cancellationToken.Register(dataDownloader.CancelAsync))
                {
                    if (downloadRange != null)
                    {
                        dataDownloader.SetRange(downloadRange.Low, downloadRange.High);
                    }

                    cancellationToken.ThrowIfCancellationRequested();

                    Logger.Info(
                        $"{request.StreamType}: Segment: {SegmentId(request.SegmentId)} Requested. URL: {request.DownloadSegment.Url} Range: {downloadRange}");

                    long bytesReceived = 0;
                    dataDownloader.DownloadProgressChanged += (sender, args) => { bytesReceived = args.BytesReceived; };

                    Stopwatch watch = null;
                    try
                    {
                        watch = Stopwatch.StartNew();
                        var data = await dataDownloader.DownloadDataTaskAsync(request.DownloadSegment.Url).ConfigureAwait(false);

                        return(new DownloadResponse
                        {
                            StreamType = request.StreamType,
                            DownloadSegment = request.DownloadSegment,
                            SegmentId = request.SegmentId,
                            Data = data
                        });
                    }
                    finally
                    {
                        if (watch != null)
                        {
                            throughputHistory.Push((int)bytesReceived, watch.Elapsed);
                        }
                    }
                }
        }
Пример #2
0
        private async Task <DownloadResponse> DownloadDataTaskAsync()
        {
            cancellationToken.ThrowIfCancellationRequested();

            Logger.Info(
                $"{request.StreamType}: Segment: {SegmentId(request.SegmentId)} Requested. URL: {request.DownloadSegment.Url} Range: {downloadRange}");

            using (var httpRequest = CreateHttpRequest())
            {
                var  watch = Stopwatch.StartNew();
                long totalBytesReceived = 0;
                try
                {
                    using (var response = await client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead,
                                                                 cancellationToken))
                    {
                        StatusCode = response.StatusCode;
                        response.EnsureSuccessStatusCode();

                        using (var stream = await response.Content.ReadAsStreamAsync())
                            using (var reader = new AsyncBinaryReader(stream))
                            {
                                byte[] buffer;
                                do
                                {
                                    buffer = await ReadChunk(reader);

                                    PushChunk(buffer);
                                    totalBytesReceived += buffer.Length;
                                } while (buffer.Length != 0);

                                return(CreateDownloadResponse());
                            }
                    }
                }
                finally
                {
                    throughputHistory.Push((int)totalBytesReceived, watch.Elapsed);
                }
            }
        }