Example #1
0
        public DateTime Now(string timeServer)
        {
            try
            {
                SntpMessage requestMessage  = new SntpMessage();
                SntpMessage responseMessage = new SntpMessage();

                if (!StringUtility.IsNullOrEmpty(timeServer))
                {
                    // Resolve server address
                    IPHostEntry timeHost       = Dns.GetHostEntry(timeServer);
                    IPEndPoint  remoteEndPoint = new IPEndPoint(timeHost.AddressList[0], Constants.SNTP_CLIENT_PORT);

                    responseMessage = SendRequest(requestMessage, remoteEndPoint);
                }
                else
                {
                    responseMessage = SendRequest(requestMessage);
                }

                return(responseMessage.TransmitDateTime);
            }
            catch
            {
                return(DateTime.MinValue);
            }
        }
Example #2
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="TimeChangedEventArgs" /> class.
        /// </summary>
        /// <param name="message">Response Message.</param>
        public TimeChangedEventArgs(SntpMessage message)
        {
            this.ResponseMessage = message;
            this.DeviceTime      = DateTime.Now.ToUniversalTime();
            this.NetworkTime     = message.TransmitDateTime;
            this.ClockOffset     = message.LocalClockOffset;

            //Debug.Print(TimeZone.CurrentTimeZone.GetUtcOffset(responseMessage.TransmitDateTime).ToString());
            //Debug.Print(TimeZone.CurrentTimeZone.GetUtcOffset(this.NetworkTime).ToString());

            Utility.SetLocalTime(message.TransmitDateTime.AddHours(-8));
            Logger.WriteInfo(this, "Device time changed from " + this.DeviceTime.ToString() + " to " + DateTime.Now.ToString() + " using network time");
        }
Example #3
0
        private void Synchronize(object state)
        {
            SntpMessage requestMessage  = new SntpMessage();
            SntpMessage responseMessage = new SntpMessage();

            responseMessage = SendRequest(requestMessage);

            if (responseMessage != null)
            {
                TimeChangedEventArgs args = new TimeChangedEventArgs(responseMessage);
                this.OnTimeChanged(args);
            }
        }
        /// <summary>
        ///     Initializes a new instance of the <see cref="TimeChangedEventArgs" /> class.
        /// </summary>
        /// <param name="message">Response Message.</param>
        public TimeChangedEventArgs(SntpMessage message)
        {
            this.ResponseMessage = message;
            this.DeviceTime = DateTime.Now.ToUniversalTime();
            this.NetworkTime = message.TransmitDateTime;
            this.ClockOffset = message.LocalClockOffset;

            //Debug.Print(TimeZone.CurrentTimeZone.GetUtcOffset(responseMessage.TransmitDateTime).ToString());
            //Debug.Print(TimeZone.CurrentTimeZone.GetUtcOffset(this.NetworkTime).ToString());

            Utility.SetLocalTime(message.TransmitDateTime.AddHours(-8));
            Logger.WriteInfo(this, "Device time changed from " + this.DeviceTime.ToString() + " to " + DateTime.Now.ToString() + " using network time");
        }
Example #5
0
        public SntpMessage SendRequest(SntpMessage sentMessage, IPEndPoint remoteEndPoint)
        {
            try
            {
                using (Socket _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
                {
                    if (remoteEndPoint == null)
                    {
                        try
                        {
                            IPHostEntry timeHost = Dns.GetHostEntry(_primaryServer);
                            remoteEndPoint = new IPEndPoint(timeHost.AddressList[0], Constants.SNTP_CLIENT_PORT);
                            _socket.Connect(remoteEndPoint);
                        }
                        catch
                        {
                            Logger.WriteInfo(this, "Failed to contact primary server " + _primaryServer + " failed.  Trying again using alternate server " + _alternateServer
                                             + " endpoint " + remoteEndPoint.ToString());

                            IPHostEntry timeHost = Dns.GetHostEntry(_alternateServer);
                            remoteEndPoint = new IPEndPoint(timeHost.AddressList[0], Constants.SNTP_CLIENT_PORT);
                            _socket.Connect(remoteEndPoint);
                        }
                    }

                    try
                    {
                        _socket.Connect(remoteEndPoint);
                    }
                    catch
                    {
                        Logger.WriteInfo(this, "Failed to contact primary server " + _primaryServer + " failed.  Trying again using alternate server " + _alternateServer
                                         + " endpoint " + remoteEndPoint.ToString());
                    }

                    if (remoteEndPoint != null)
                    {
                        Byte[] messageBuffer = new Byte[Constants.SNTP_MAX_MESSAGE_SIZE];
                        _socket.ReceiveTimeout = Constants.SNTP_RECEIVE_TIMEOUT;
                        _socket.SendTimeout    = Constants.SNTP_SEND_TIMEOUT;

                        // send message
                        _socket.SendTo(sentMessage.ToArray(), remoteEndPoint);
                        Logger.WriteDebug(this, "PACKET was "
                                          + " successfully sent to client endpoint " +
                                          remoteEndPoint.ToString());
                        Logger.WriteDebug(sentMessage.ToString());

                        // receive message
                        _socket.Receive(messageBuffer);
                        _socket.Close();

                        SntpMessage recivedMessage = new SntpMessage(messageBuffer);
                        Logger.WriteDebug(this, "PACKET was successfully received from client endpoint " +
                                          remoteEndPoint.ToString());
                        Logger.WriteDebug(recivedMessage.ToString());

                        return(recivedMessage);
                    }
                }
            }
            catch (SocketException ex)
            {
                Logger.WriteError(this, ex.Message + "Socket Error Code: " + ex.ErrorCode.ToString(), ex);
            }
            catch (Exception ex)
            {
                Logger.WriteError(this, "Error Message:" + ex.Message.ToString(), ex);
            }

            return(null);
        }
Example #6
0
 public SntpMessage SendRequest(SntpMessage sentMessage)
 {
     return(this.SendRequest(sentMessage, null));
 }