} // WaitCompletion public void Dispose() { if (SegmentsQueue == null) { return; } SegmentsQueue = null; EnqueuedSegments.Dispose(); ProcessSegmentsEnded.Dispose(); Datastore = null; } // Dispose
public void Start(EpgDatastore datastore) { if (SegmentsQueue != null) { throw new InvalidOperationException(); } SegmentsQueue = new ConcurrentQueue <byte[]>(); EnqueuedSegments = new AutoResetEvent(false); ProcessSegmentsEnded = new AutoResetEvent(false); NoMoreSegments = false; Datastore = datastore; var worker = new Thread(ProcessSegments); worker.Start(); } // Start
} // StartAsync private void Download() { var retryIncrement = new TimeSpan(0, 0, 5); var retryTime = new TimeSpan(0, 0, 0); var maxRetryTime = new TimeSpan(0, 1, 0); try { Processor = new SegmentsProcessor(); // initialize DVB-STP client StreamClient = new DvbStpStreamClient(MulticastIpAddress, MulticastPort); StreamClient.NoDataTimeout = -1; // not implemented by DvbStpStreamClient StreamClient.ReceiveDatagramTimeout = 60 * 1000; // 60 seconds StreamClient.OperationTimeout = -1; // forever StreamClient.SegmentPayloadReceived += SegmentPayloadReceived; while (retryTime <= maxRetryTime) { var retry = false; try { Processor.Start(Datastore); StreamClient.DownloadStream(); break; } catch (SocketException) // reception error { retry = true; } catch (TimeoutException) // reception timedout { // took too much time to process the stream }// 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; } if (retry) { // wait and then retry, increasing wait time retryTime += retryIncrement; Thread.Sleep(retryTime); continue; } // if } // while Processor.WaitCompletion(); } finally { if (StreamClient != null) { StreamClient.Close(); StreamClient = null; } // if if (Processor != null) { Processor.Dispose(); Processor = null; } // if Datastore = null; } // try-finally } // Download
} // constructor public async Task StartAsync(EpgDatastore datastore) { Datastore = datastore; await Task.Run((Action)Download); } // StartAsync