예제 #1
0
        /* ----------------------------------------------------------------- */
        ///
        /// WhenTick
        ///
        /// <summary>
        /// 一定間隔毎に実行されます。
        /// </summary>
        ///
        /// <remarks>
        /// アプリケーションの起動直後等に NTP 通信を実行すると精度の
        /// 悪い傾向が確認されています。そのため、毎回の実行時の最初に
        /// 500 ミリ秒の待機時間を設ける事としています。
        /// </remarks>
        ///
        /* ----------------------------------------------------------------- */
        private async Task WhenTick()
        {
            if (Subscriptions.Count <= 0)
            {
                return;
            }
            if (!Network.Available)
            {
                this.LogDebug("Network not available");
                return;
            }

            await Task.Delay(500).ConfigureAwait(false); // see ramarks

            for (var i = 0; i < RetryCount; ++i)
            {
                try
                {
                    if (State != TimerState.Run)
                    {
                        return;
                    }
                    var client = new NtpClient(Server, Port)
                    {
                        Timeout = Timeout
                    };
                    var packet = await client.GetAsync();

                    if (packet != null && packet.IsValid)
                    {
                        await PublishAsync(packet.LocalClockOffset).ConfigureAwait(false);
                    }
                    else
                    {
                        throw new ArgumentException("InvalidPacket");
                    }
                    return;
                }
                catch (Exception err)
                {
                    this.LogWarn(Server, $"{err.Message} ({i + 1}/{RetryCount})");
                    await Task.Delay(RetryInterval).ConfigureAwait(false);
                }
            }
        }
예제 #2
0
        /* ----------------------------------------------------------------- */
        ///
        /// WhenTick
        ///
        /// <summary>
        /// 一定間隔毎に実行されます。
        /// </summary>
        ///
        /// <remarks>
        /// アプリケーションの起動直後等に NTP 通信を実行すると精度の
        /// 悪い傾向が確認されています。そのため、毎回の実行時の最初に
        /// 500 ミリ秒の待機時間を設ける事としています。
        /// </remarks>
        ///
        /* ----------------------------------------------------------------- */
        private async Task WhenTick()
        {
            if (Subscriptions.Count <= 0)
            {
                return;
            }

            await Task.Delay(500).ConfigureAwait(false); // see ramarks

            for (var i = 0; i < RetryCount; ++i)
            {
                try
                {
                    if (State != TimerState.Run)
                    {
                        return;
                    }
                    var client = new NtpClient(Server, Port)
                    {
                        Timeout = Timeout
                    };
                    var packet = await client.GetAsync();

                    if (packet != null && packet.IsValid)
                    {
                        await PublishAsync(packet.LocalClockOffset).ConfigureAwait(false);
                    }
                    else
                    {
                        throw new ArgumentException("InvalidPacket");
                    }
                    break;
                }
                catch (Exception err)
                {
                    this.LogWarn(err.ToString(), err);
                    await Task.Delay(RetryInterval).ConfigureAwait(false);

                    this.LogDebug($"Retry\tCount:{i + 1}\tServer:{Server}");
                }
            }
        }