Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Core/Reactive/Internal/SystemClock.Default.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 using System.ComponentModel;
4 using System.Reactive.Concurrency;
5 using System.Reactive.Disposables;
6
7 namespace System.Reactive.PlatformServices
8 {
9 /// <summary>
10 /// (Infrastructure) Provides access to the local system clock.
11 /// </summary>
12 [EditorBrowsable(EditorBrowsableState.Never)]
13 public class DefaultSystemClock : ISystemClock
14 {
15 /// <summary>
16 /// Gets the current time.
17 /// </summary>
18 public DateTimeOffset UtcNow
19 {
20 get { return DateTimeOffset.UtcNow; }
21 }
22 }
23
24 internal class DefaultSystemClockMonitor : PeriodicTimerSystemClockMonitor
25 {
26 private static readonly TimeSpan DEFAULT_PERIOD = TimeSpan.FromSeconds(1);
27
28 public DefaultSystemClockMonitor()
29 : base(DEFAULT_PERIOD)
30 {
31 }
32 }
33
34 /// <summary>
35 /// (Infrastructure) Monitors for system clock changes based on a periodic timer.
36 /// </summary>
37 [EditorBrowsable(EditorBrowsableState.Never)]
38 public class PeriodicTimerSystemClockMonitor : INotifySystemClockChanged
39 {
40 private readonly TimeSpan _period;
41 private readonly SerialDisposable _timer;
42
43 private DateTimeOffset _lastTime;
44 private EventHandler<SystemClockChangedEventArgs> _systemClockChanged;
45
46 private const int SYNC_MAXRETRIES = 100;
47 private const double SYNC_MAXDELTA = 10;
48 private const int MAXERROR = 100;
49
50 /// <summary>
51 /// Creates a new monitor for system clock changes with the specified polling frequency.
52 /// </summary>
53 /// <param name="period">Polling frequency for system clock changes.</param>
54 public PeriodicTimerSystemClockMonitor(TimeSpan period)
55 {
56 _period = period;
57 _timer = new SerialDisposable();
58 }
59
60 /// <summary>
61 /// Event that gets raised when a system clock change is detected.
62 /// </summary>
63 public event EventHandler<SystemClockChangedEventArgs> SystemClockChanged
64 {
65 add
66 {
67 NewTimer();
68
69 _systemClockChanged += value;
70 }
71
72 remove
73 {
74 _systemClockChanged -= value;
75
76 _timer.Disposable = Disposable.Empty;
77 }
78 }
79
80 private void NewTimer()
81 {
82 _timer.Disposable = Disposable.Empty;
83
84 var n = 0;
85 do
86 {
87 _lastTime = SystemClock.UtcNow;
88 _timer.Disposable = ConcurrencyAbstractionLayer.Current.StartPeriodicTimer(TimeChanged, _period);
89 } while (Math.Abs((SystemClock.UtcNow - _lastTime).TotalMilliseconds) > SYNC_MAXDELTA && ++n < SYNC_MAXRETRIES);
90
91 if (n >= SYNC_MAXRETRIES)
92 throw new InvalidOperationException(Strings_Core.FAILED_CLOCK_MONITORING);
93 }
94
95 private void TimeChanged()
96 {
97 var now = SystemClock.UtcNow;
98 var diff = now - (_lastTime + _period);
99 if (Math.Abs(diff.TotalMilliseconds) >= MAXERROR)
100 {
101 var scc = _systemClockChanged;
102 if (scc != null)
103 scc(this, new SystemClockChangedEventArgs(_lastTime + _period, now));
104
105 NewTimer();
106 }
107 else
108 {
109 _lastTime = SystemClock.UtcNow;
110 }
111 }
112 }
113 }