コード例 #1
0
        } // 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
            {
                while (retryTime <= maxRetryTime)
                {
                    // initialize DVB-STP client
                    _streamClient = new DvbStpStreamClient(MulticastIpAddress, MulticastPort, Token)
                    {
                        NoDataTimeout          = -1,        // not implemented by DvbStpStreamClient
                        ReceiveDatagramTimeout = 60 * 1000, // 60 seconds
                        OperationTimeout       = -1         // forever
                    };
                    _streamClient.SegmentPayloadReceived += SegmentPayloadReceived;

                    var retry = false;
                    try
                    {
                        _processor?.Dispose();
                        _processor = new SegmentsProcessor();
                        _processor.ScheduleReceived += (sender, args) =>
                                                       ProgramReceived?.BeginInvoke(this, EventArgs.Empty, null, null);
                        _processor.ParseError += (sender, args) => ParseError?.BeginInvoke(this, EventArgs.Empty, null, null);

                        _processor.Start(DataStore);
                        _streamClient.DownloadStream();
                        break;
                    }
                    catch (SocketException) // reception error
                    {
                        retry = true;
                    }
                    catch (TimeoutException) // reception timeout
                    {
                        // 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) || Token.IsCancellationRequested || (!retry))
                    {
                        break;
                    }

                    // wait and then retry, increasing wait time
                    retryTime += retryIncrement;
                    Thread.Sleep(retryTime);
                } // while
            }
            catch
            {
                FatalError?.BeginInvoke(this, EventArgs.Empty, null, null);
            }
            finally
            {
                _streamClient?.Close();
                _streamClient = null;
                _processor?.WaitCompletion();
                _processor?.Dispose();
                _processor = null;
                DataStore  = null;
            } // try-finally
        }     // Download
コード例 #2
0
        } // 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