Esempio n. 1
0
 public SubtSenderThread(SubtLocalPeer localPeer, string threadName)
 {
     _threadName   = threadName;
     _localPeer    = localPeer;
     _actionsQueue = new ActionsQueue(exc => _localPeer.HandleException(exc), null);
     _thread       = new Thread(ThreadEntry);
     _thread.Name  = threadName;
     _thread.Start();
     _thread.Priority = ThreadPriority.Highest;
 }
Esempio n. 2
0
        void ThreadEntry()
        {
            var        sw           = Stopwatch.StartNew();
            var        previousTs32 = _localPeer.LocalPeer.Time32;
            const uint period32     = (uint)TimeSpan.TicksPerMillisecond * 10;
            int        counter      = 0;

            while (!_disposing)
            {
                try
                {
                    _actionsQueue.ExecuteQueued();

                    var timeNow32 = _localPeer.LocalPeer.Time32;

                    while (MiscProcedures.TimeStamp1IsLess(previousTs32, timeNow32) && !_disposing)
                    {
                        if (_debug)
                        {
                            Debugger.Break();
                        }

                        previousTs32 = unchecked (previousTs32 + period32);
                        foreach (var stream in _streams.Values)
                        {
                            stream.SendPacketsIfNeeded_10ms();
                        }
                        counter++;
                        if (counter % 10 == 0)
                        {
                            foreach (var stream in _streams.Values)
                            {
                                stream.SendPayloadPacketsIfNeeded_100ms();
                            }
                        }
                        if (counter % 100 == 0)
                        {
                            foreach (var stream in _streams.Values)
                            {
                                stream.SendPayloadPacketsIfNeeded_1s();
                            }
                        }
                    }
                }
                catch (Exception exc)
                {
                    _localPeer.HandleException(exc);
                }
                Thread.Sleep(10);
            }
        }
Esempio n. 3
0
        internal SubtMeasurement MeasureIfNeeded(SubtLocalPeer subtLocalPeer) // manager thread
        {
            try
            {
                var now = subtLocalPeer.LocalPeer.DateTimeNowUtc;
                if (_initializedTime == null)
                {
                    return(null);
                }
                if (now < _initializedTime.Value.AddTicks(SubtLogicConfiguration.MeasurementInitializationTimeTicks))
                {
                    return(null);
                }

                if (_lastTimeMeasured == null || _lastTimeMeasured.Value.AddTicks(SubtLogicConfiguration.MeasurementsIntervalTicks) < now)
                {
                    _lastTimeMeasured = now;

                    // measure
                    var m = Measure(subtLocalPeer);

                    lock (_measurementsInRam)
                    {
                        _measurementsInRam.AddFirst(m);
                        if (_measurementsInRam.Count > SubtLogicConfiguration.MaxMeasurementsCountInRAM)
                        {
                            _measurementsInRam.RemoveLast();
                        }
                    }

                    OnMeasured?.Invoke(m);
                    return(m);
                }
            }
            catch (Exception exc)
            {
                subtLocalPeer.HandleException(exc);
            }
            return(null);
        }