Beispiel #1
0
        private void WorkerThreadStart()
        {
            lock (this.SyncObject)
            {
                QueryServerCompletedEventArgs e = QueryServer();

                asyncOperation.PostOperationCompleted(OperationCompleted, e);
            }
        }
Beispiel #2
0
        // Protected Methods 

        /// <summary>
        /// Raises the QueryServerCompleted event.
        /// </summary>
        /// <param name="e">A QueryServerCompletedEventArgs instance.</param>
        protected virtual void OnQueryServerCompleted(QueryServerCompletedEventArgs e)
        {
            EventHandler <QueryServerCompletedEventArgs> eh = QueryServerCompleted;

            if (eh != null)
            {
                eh(this, e);
            }
        }
Beispiel #3
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);
                }
            }
        }
Beispiel #4
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);
            }
        }
Beispiel #5
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);
            }
        }