예제 #1
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);
        }
예제 #2
0
        /// <summary>
        /// This is the 'nuts and bolts' method that queries the server.
        /// </summary>
        /// <returns>A QueryServerResults instance that holds the results of the query.</returns>
        private QueryServerCompletedEventArgs QueryServer()
        {
            QueryServerCompletedEventArgs result = new QueryServerCompletedEventArgs();

            Initialize();
            using (UdpClient client = new UdpClient())
            {
                try
                {
                    // Configure and connect the socket.
                    IPEndPoint ipEndPoint = RemoteSNTPServer.GetIPEndPoint();
                    client.Client.SendTimeout    = (int)this.Timeout.TotalMilliseconds;
                    client.Client.ReceiveTimeout = (int)this.Timeout.TotalMilliseconds;
                    client.Connect(ipEndPoint);

                    // Send and receive the data, and save the completion DateTime.
                    SNTPData request = SNTPData.GetClientRequestPacket(this.VersionNumber);
                    client.Send(request, request.Length);
                    result.Data = client.Receive(ref ipEndPoint);
                    result.Data.DestinationDateTime = DateTime.Now.ToUniversalTime();

                    // Check the data
                    if (result.Data.Mode == Mode.Server)
                    {
                        result.Succeeded = true;

                        // Call other method(s) if needed
                        if (this.UpdateLocalDateTime)
                        {
                            this.UpdateTime(result.Data.LocalClockOffset);
                            result.LocalDateTimeUpdated = true;
                        }
                    }
                    else
                    {
                        result.ErrorData = new ErrorData("The response from the server was invalid.");
                    }
                    return(result);
                }
                catch (Exception ex)
                {
                    result.ErrorData = new ErrorData(ex);
                    return(result);
                }
            }
        }
예제 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="remoteSNTPServer">The server to use.</param>
        /// <param name="timeout">The timeout in milliseconds used for sending and receiving.</param>
        /// <returns>The real local offset by seconds.</returns>
        public static double GetLocalClockOffset(RemoteSNTPServer remoteSNTPServer, TimeSpan timeout)
        {
            SNTPClient sntpClient = new SNTPClient();

            sntpClient.UpdateLocalDateTime = false;
            sntpClient.RemoteSNTPServer    = remoteSNTPServer;
            sntpClient.Timeout             = timeout;
            QueryServerCompletedEventArgs args = sntpClient.QueryServer();

            if (args.Succeeded)
            {
                return(args.Data.LocalClockOffset);
            }
            else
            {
                throw new ApplicationException(args.ErrorData.ErrorText);
            }
        }
예제 #4
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="remoteSNTPServer">The server to use.</param>
        /// <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(RemoteSNTPServer remoteSNTPServer, TimeSpan timeout)
        {
            SNTPClient sntpClient = new SNTPClient();

            sntpClient.UpdateLocalDateTime = false;
            sntpClient.RemoteSNTPServer    = remoteSNTPServer;
            sntpClient.Timeout             = timeout;

            QueryServerCompletedEventArgs args = sntpClient.QueryServer();

            if (args.Succeeded)
            {
                return(DateTime.Now.AddSeconds(args.Data.LocalClockOffset));
            }
            else
            {
                return(DateTime.MinValue);
            }
        }
예제 #5
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));
 }