예제 #1
0
 private void Initialize()
 {
     if (RemoteSNTPServer == null)
     {
         RemoteSNTPServer = SNTPSettings.GetSNTPServer();
     }
 }
예제 #2
0
        private static void SyncLocalTimeOffset()
        {
            double localOffset = 0;

            while (true)
            {
                try
                {
                    localOffset = GetLocalClockOffset();

                    _RWLocalOffsetLock.EnterWriteLock();

                    try
                    {
                        LocalOffsetSeconds = localOffset;
                    }
                    finally
                    {
                        _RWLocalOffsetLock.ExitWriteLock();
                    }
                }
                catch (System.Exception)
                {
                }

                try
                {
                    Thread.Sleep(SNTPSettings.GetConfigOrDefault().PoolInterval);
                }
                catch (System.Exception)
                {
                    Thread.Sleep(60000);
                }
            }
        }
예제 #3
0
        /// <summary>
        /// 获取配置信息。如果不存在,则返回默认的设置
        /// </summary>
        /// <returns></returns>
        public static SNTPSettings GetConfigOrDefault()
        {
            SNTPSettings settings = SNTPSettings.GetConfig();

            if (settings == null)
            {
                settings = new SNTPSettings();
            }

            return(settings);
        }
예제 #4
0
        /// <summary>
        /// Creates a new instance of SNTPClient.
        /// </summary>
        public SNTPClient()
        {
            this.InitializeComponent();

            this.Initialize();
            this.ThreadStart         = new WorkerThreadStartDelegate(WorkerThreadStart);
            this.OperationCompleted  = new SendOrPostCallback(AsyncOperationCompleted);
            this.Timeout             = SNTPSettings.GetConfigOrDefault().Timeout;
            this.VersionNumber       = DefaultVersionNumber;
            this.UpdateLocalDateTime = false;
        }
예제 #5
0
        /// <summary>
        /// 从配置信息中获取远程时间服务器的设置
        /// </summary>
        /// <returns></returns>
        public static RemoteSNTPServer GetSNTPServer()
        {
            SNTPSettings settings = SNTPSettings.GetConfig();

            RemoteSNTPServer server = RemoteSNTPServer.Default;

            if (settings != null)
            {
                server = new RemoteSNTPServer(settings.ServerName, settings.Port);
            }

            return(server);
        }
예제 #6
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="timeout"></param>
 /// <returns></returns>
 public static double GetLocalClockOffset(TimeSpan timeout)
 {
     return(GetLocalClockOffset(SNTPSettings.GetSNTPServer(), timeout));
 }
예제 #7
0
 /// <summary>
 ///
 /// </summary>
 /// <returns></returns>
 public static double GetLocalClockOffset()
 {
     return(GetLocalClockOffset(SNTPSettings.GetConfigOrDefault().Timeout));
 }
예제 #8
0
 /// <summary>
 /// Gets the real local date and time using the default server and the specified timeout.
 /// If there is an error or exception, DateTime.MinValue is returned.
 /// </summary>
 /// <param name="timeout">The timeout in milliseconds used for sending and receiving.</param>
 /// <returns>The real local date and time.</returns>
 public static DateTime GetNow(TimeSpan timeout)
 {
     return(GetNow(SNTPSettings.GetSNTPServer(), timeout));
 }
예제 #9
0
 /// <summary>
 /// Gets the real local date and time using the specified server and a total timeout of 1 second.
 /// If there is an error or exception, DateTime.MinValue is returned.
 /// </summary>
 /// <param name="remoteSNTPServer">The server to use.</param>
 /// <returns>The real local date and time.</returns>
 public static DateTime GetNow(RemoteSNTPServer remoteSNTPServer)
 {
     return(GetNow(remoteSNTPServer, SNTPSettings.GetConfigOrDefault().Timeout));
 }
예제 #10
0
 /// <summary>
 /// Gets the real local date and time using the default server and a total timeout of 1 second.
 /// If there is an error or exception, DateTime.MinValue is returned.
 /// </summary>
 /// <returns>The real local date and time.</returns>
 public static DateTime GetNow()
 {
     return(GetNow(SNTPSettings.GetSNTPServer(), SNTPSettings.GetConfigOrDefault().Timeout));
 }