Exemplo n.º 1
0
        public void Stop()
        {
            State = ClockSyncerState.Stopped;

            if (_timer != null)
            {
                _timer.Stop();
                _timer.Elapsed -= Timer_Elapsed;
                _timer          = null;
            }
        }
Exemplo n.º 2
0
        public async void Start()
        {
            State = ClockSyncerState.Started;
            _deltas.Clear();

            await Sync();

            _expectedTime   = DateTime.Now.Ticks + _clockSyncOptions.CheckInterval;
            _timer          = new System.Timers.Timer(_clockSyncOptions.CheckInterval);
            _timer.Elapsed += Timer_Elapsed;
            _timer.Start();
        }
Exemplo n.º 3
0
        private async Task Sync()
        {
            State = ClockSyncerState.Synchronizing;
            var sampleTasks = new List <Task <long> >();

            for (var i = 0; i < _clockSyncOptions.SampleSize; i++)
            {
                sampleTasks.Add(SendWithDelay <long>(i * _clockSyncOptions.SampleDelay, Sample));
            }
            _syncing = Task.WhenAll(sampleTasks).ContinueWith(samples =>
            {
                if (State != ClockSyncerState.Synchronizing)
                {
                    return;
                }
                State = ClockSyncerState.Idle;
                _delta.OnNext(GetDelta());
            });

            await _syncing;

            _syncing = null;
        }