< Summary - ReFlex - Library

Information
Class: Implementation.Components.TimerLoop
Assembly: ReFlex.Core.Implementation
File(s): D:\a\reflex\reflex\library\src\Core\Implementation\Components\TimerLoop.cs
Line coverage
50%
Covered lines: 26
Uncovered lines: 25
Coverable lines: 51
Total lines: 96
Line coverage: 50.9%
Branch coverage
17%
Covered branches: 5
Total branches: 28
Branch coverage: 17.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_IntervalLength()100%11100%
get_IsLoopRunning()0%620%
set_IsLoopRunning(...)0%620%
.ctor(...)100%11100%
Run()0%4260%
Stop()0%4260%
OnTimerTick()75%4485.71%
BroadcastCurrent()25%8883.33%

File(s)

D:\a\reflex\reflex\library\src\Core\Implementation\Components\TimerLoop.cs

#LineLine coverage
 1using System;
 2using System.Threading;
 3using System.Collections.Generic;
 4using System.Linq;
 5using System.Timers;
 6using Implementation.Interfaces;
 7using ReFlex.Core.Common.Components;
 8using ReFlex.Core.Common.Util;
 9
 10namespace Implementation.Components
 11{
 12    public class TimerLoop : ITimerLoop
 13    {
 14        private readonly INetworkManager _networkManager;
 15        private readonly IInteractionManager _interactionManager;
 16        private readonly ICalibrationManager _calibrationManager;
 17        private readonly System.Timers.Timer _timer;
 18
 19        private int _interactionsProcessing;
 20
 21        public event EventHandler<bool> IsLoopRunningChanged;
 22
 423        public int IntervalLength { get; set; } = 100;
 24
 25        public bool IsLoopRunning
 26        {
 027            get => _timer != null && _timer.Enabled;
 28            set
 029            {
 030                if (value)
 031                    Run();
 32                else
 033                    Stop();
 034            }
 35        }
 36
 237        public TimerLoop(INetworkManager networkManager, IInteractionManager interactionManager,
 238            ICalibrationManager calibrationManager)
 239        {
 240            _networkManager = networkManager;
 241            _interactionManager = interactionManager;
 242            _calibrationManager = calibrationManager;
 243            _timer = new System.Timers.Timer(IntervalLength);
 44
 245        }
 46
 47        public void Run()
 048        {
 049            if (_networkManager is null || _timer.Enabled)
 050                return;
 51
 052            _timer.Interval = IntervalLength;
 053            _timer.AutoReset = true;
 054            _timer.Elapsed += OnTimerTick;
 055            _timer.Start();
 056            IsLoopRunningChanged?.Invoke(this, true);
 057        }
 58
 59        public void Stop()
 060        {
 061            if (_timer is null || !_timer.Enabled)
 062                return;
 63
 064            _timer.Stop();
 065            _timer.Elapsed -= OnTimerTick;
 066            IsLoopRunningChanged?.Invoke(this, false);
 067        }
 68
 69        private async void OnTimerTick(object sender, EventArgs args)
 470        {
 471            if (_interactionManager == null) return;
 472            if (Interlocked.CompareExchange(ref _interactionsProcessing, 1, 0) != 0)
 273            {
 74                // A stalled remote request must not stall expiry or keep broadcasting stale touches.
 875                try { _interactionManager.AdvanceTime(); BroadcastCurrent(); }
 076                catch (Exception exception) { NLog.LogManager.GetCurrentClassLogger().Error(exception); }
 277                return;
 78            }
 79            try
 280            {
 281                await _interactionManager.Update();
 282                BroadcastCurrent();
 283            }
 084            catch (Exception exception) { NLog.LogManager.GetCurrentClassLogger().Error(exception); }
 685            finally { Volatile.Write(ref _interactionsProcessing, 0); }
 486        }
 87
 88        private void BroadcastCurrent()
 489        {
 490            var stream = _interactionManager.Interactions.Select(item => new Interaction(item)).ToList();
 891            for (var i = 0; i < stream.Count; i++)
 092                stream[i] = _calibrationManager?.Calibrate(stream[i]) ?? stream[i];
 493            _networkManager?.Broadcast(stream);
 494        }
 95    }
 96}