Example #1
0
        /// <summary>
        ///     Send transmissions in a loop.
        /// </summary>
        protected void SendLoop()
        {
            TimeSpan prevSendingInterval = TimeSpan.Zero;
            TimeSpan sendingInterval     = _sendingIntervalOnNoData;

            try
            {
                while (!_stopped)
                {
                    using (StorageTransmission transmission = _storage.Peek())
                    {
                        if (_stopped)
                        {
                            // This second verification is required for cases where 'stopped' was set while peek was happening.
                            // Once the actual sending starts the design is to wait until it finishes and deletes the transmission.
                            // So no extra validation is required.
                            break;
                        }

                        // If there is a transmission to send - send it.
                        if (transmission != null)
                        {
                            bool shouldRetry = Send(transmission, ref sendingInterval);
                            if (!shouldRetry)
                            {
                                // If retry is not required - delete the transmission.
                                _storage.Delete(transmission);
                            }
                        }
                        else
                        {
                            sendingInterval = _sendingIntervalOnNoData;
                        }
                    }

                    LogInterval(prevSendingInterval, sendingInterval);
                    DelayHandler.WaitOne(sendingInterval);
                    prevSendingInterval = sendingInterval;
                }

                _stoppedHandler.Set();
            }
            catch (ObjectDisposedException)
            {
            }
        }