private void Initialize() { if (RemoteSNTPServer == null) { RemoteSNTPServer = SNTPSettings.GetSNTPServer(); } }
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); } } }
/// <summary> /// 获取配置信息。如果不存在,则返回默认的设置 /// </summary> /// <returns></returns> public static SNTPSettings GetConfigOrDefault() { SNTPSettings settings = SNTPSettings.GetConfig(); if (settings == null) { settings = new SNTPSettings(); } return(settings); }
/// <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; }
/// <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); }
/// <summary> /// /// </summary> /// <param name="timeout"></param> /// <returns></returns> public static double GetLocalClockOffset(TimeSpan timeout) { return(GetLocalClockOffset(SNTPSettings.GetSNTPServer(), timeout)); }
/// <summary> /// /// </summary> /// <returns></returns> public static double GetLocalClockOffset() { return(GetLocalClockOffset(SNTPSettings.GetConfigOrDefault().Timeout)); }
/// <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)); }
/// <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)); }
/// <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)); }