Exemple #1
0
        public async Task <m3u8_part_ts> DownloadPart(m3u8_part_ts part
                                                      , Uri baseAddress
                                                      , CancellationToken?cancellationToken = null)
        {
            if (baseAddress == null)
            {
                throw (new m3u8_ArgumentException(nameof(baseAddress)));
            }
            if (part.RelativeUrlName.IsNullOrWhiteSpace())
            {
                throw (new m3u8_ArgumentException(nameof(part.RelativeUrlName)));
            }
            //----------------------------------------------------------------------------------------------------------------//

            var url = new Uri(baseAddress, part.RelativeUrlName);
            var ct  = cancellationToken.GetValueOrDefault(CancellationToken.None);
            var attemptRequestCountByPart = InitParams.AttemptRequestCount.GetValueOrDefault(1);

            for (var attemptRequestCount = attemptRequestCountByPart; 0 < attemptRequestCount; attemptRequestCount--)
            {
                try
                {
                    using (HttpResponseMessage response = await _HttpClient.GetAsync(url, ct))
                        using (HttpContent content = response.Content)
                        {
                            if (!response.IsSuccessStatusCode)
                            {
                                var responseText = content.ReadAsStringAsyncEx(ct);
                                throw (new m3u8_Exception(response.CreateExceptionMessage(responseText)));
                            }

                            var bytes = content.ReadAsByteArrayAsyncEx(ct);
                            part.SetBytes(bytes);
                            return(part);
                        }
                }
                catch (Exception ex)
                {
                    if ((attemptRequestCount == 1) || ct.IsCancellationRequested)
                    {
                        part.SetError(ex);
                        return(part);
                    }
                }
            }

            throw (new m3u8_Exception($"No content found while {attemptRequestCountByPart} attempt requests"));
        }
Exemple #2
0
 internal static RequestStepActionParams CreateSuccess(int totalPartCount, int partOrderNumber, m3u8_part_ts part)
 => new RequestStepActionParams()
 {
     TotalPartCount = totalPartCount, PartOrderNumber = partOrderNumber, Part = part
 };