} // SegmentPayloadReceived private DvbStpStreamClient CreateStreamClient() { // initialize DVB-STP client var streamClient = new DvbStpStreamClient(_settings.EndPoint.Address, _settings.EndPoint.Port, _settings.CancellationToken) { NoDataTimeout = -1, // not implemented by DvbStpStreamClient ReceiveDatagramTimeout = (int)Math.Floor(_settings.ReceiveDatagramTimeout.TotalMilliseconds), OperationTimeout = -1 // forever }; streamClient.SegmentPayloadReceived += SegmentPayloadReceived; return(streamClient); } // CreateStreamClient
} // StartAsync private void Download() { DvbStpStreamClient streamClient = null; try { _processor = new SegmentDataProcessor(); var retryTime = new TimeSpan(0, 0, 0); while (retryTime <= _settings.MaxRetryTime) { streamClient = CreateStreamClient(); // ReSharper disable once RedundantAssignment // assignment to false is necessary var retry = false; try { _processor.Start(); streamClient.DownloadStream(); break; } catch (SocketException) // reception error { retry = true; } // try-catch // Safety check. If we asked to stop the download, but an exception is thrown in between, // we can ignore it and end the reception; if (streamClient.CancelRequested) { break; } // ReSharper disable once ConditionIsAlwaysTrueOrFalse // ReSharper disable once HeuristicUnreachableCode if (!retry) { continue; } // wait and then retry, increasing wait time retryTime += _settings.RetryIncrement; Thread.Sleep(retryTime); } // while _processor.WaitCompletion(); } finally { if (streamClient != null) { streamClient.Close(); streamClient = null; } // if if (_processor != null) { _processor.Dispose(); _processor = null; } // if if (_downloadTask != null) { _downloadTask.Dispose(); _downloadTask = null; } /// if } // try-finally } // Download