| | | 1 | | using System; |
| | | 2 | | using System.Threading; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Timers; |
| | | 6 | | using Implementation.Interfaces; |
| | | 7 | | using ReFlex.Core.Common.Components; |
| | | 8 | | using ReFlex.Core.Common.Util; |
| | | 9 | | |
| | | 10 | | namespace 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 | | |
| | 4 | 23 | | public int IntervalLength { get; set; } = 100; |
| | | 24 | | |
| | | 25 | | public bool IsLoopRunning |
| | | 26 | | { |
| | 0 | 27 | | get => _timer != null && _timer.Enabled; |
| | | 28 | | set |
| | 0 | 29 | | { |
| | 0 | 30 | | if (value) |
| | 0 | 31 | | Run(); |
| | | 32 | | else |
| | 0 | 33 | | Stop(); |
| | 0 | 34 | | } |
| | | 35 | | } |
| | | 36 | | |
| | 2 | 37 | | public TimerLoop(INetworkManager networkManager, IInteractionManager interactionManager, |
| | 2 | 38 | | ICalibrationManager calibrationManager) |
| | 2 | 39 | | { |
| | 2 | 40 | | _networkManager = networkManager; |
| | 2 | 41 | | _interactionManager = interactionManager; |
| | 2 | 42 | | _calibrationManager = calibrationManager; |
| | 2 | 43 | | _timer = new System.Timers.Timer(IntervalLength); |
| | | 44 | | |
| | 2 | 45 | | } |
| | | 46 | | |
| | | 47 | | public void Run() |
| | 0 | 48 | | { |
| | 0 | 49 | | if (_networkManager is null || _timer.Enabled) |
| | 0 | 50 | | return; |
| | | 51 | | |
| | 0 | 52 | | _timer.Interval = IntervalLength; |
| | 0 | 53 | | _timer.AutoReset = true; |
| | 0 | 54 | | _timer.Elapsed += OnTimerTick; |
| | 0 | 55 | | _timer.Start(); |
| | 0 | 56 | | IsLoopRunningChanged?.Invoke(this, true); |
| | 0 | 57 | | } |
| | | 58 | | |
| | | 59 | | public void Stop() |
| | 0 | 60 | | { |
| | 0 | 61 | | if (_timer is null || !_timer.Enabled) |
| | 0 | 62 | | return; |
| | | 63 | | |
| | 0 | 64 | | _timer.Stop(); |
| | 0 | 65 | | _timer.Elapsed -= OnTimerTick; |
| | 0 | 66 | | IsLoopRunningChanged?.Invoke(this, false); |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | private async void OnTimerTick(object sender, EventArgs args) |
| | 4 | 70 | | { |
| | 4 | 71 | | if (_interactionManager == null) return; |
| | 4 | 72 | | if (Interlocked.CompareExchange(ref _interactionsProcessing, 1, 0) != 0) |
| | 2 | 73 | | { |
| | | 74 | | // A stalled remote request must not stall expiry or keep broadcasting stale touches. |
| | 8 | 75 | | try { _interactionManager.AdvanceTime(); BroadcastCurrent(); } |
| | 0 | 76 | | catch (Exception exception) { NLog.LogManager.GetCurrentClassLogger().Error(exception); } |
| | 2 | 77 | | return; |
| | | 78 | | } |
| | | 79 | | try |
| | 2 | 80 | | { |
| | 2 | 81 | | await _interactionManager.Update(); |
| | 2 | 82 | | BroadcastCurrent(); |
| | 2 | 83 | | } |
| | 0 | 84 | | catch (Exception exception) { NLog.LogManager.GetCurrentClassLogger().Error(exception); } |
| | 6 | 85 | | finally { Volatile.Write(ref _interactionsProcessing, 0); } |
| | 4 | 86 | | } |
| | | 87 | | |
| | | 88 | | private void BroadcastCurrent() |
| | 4 | 89 | | { |
| | 4 | 90 | | var stream = _interactionManager.Interactions.Select(item => new Interaction(item)).ToList(); |
| | 8 | 91 | | for (var i = 0; i < stream.Count; i++) |
| | 0 | 92 | | stream[i] = _calibrationManager?.Calibrate(stream[i]) ?? stream[i]; |
| | 4 | 93 | | _networkManager?.Broadcast(stream); |
| | 4 | 94 | | } |
| | | 95 | | } |
| | | 96 | | } |