/// <summary> /// Processes the packet and returns a response. /// </summary> /// <param name="packetInfo">The <see cref="Acn.Sntp.Sockets.NewPacketEventArgs"/> instance containing the event data.</param> protected void ProcessPacket(NewPacketEventArgs packetInfo) { NtpData data = packetInfo.Packet; data.LeapIndicator = LeapIndicator.NoWarning; data.VersionNumber = 4; data.Mode = NtpMode.Server; data.Stratum = NtpStratum.PrimaryReference; data.RootDispersion = 0; data.RootDelay = 0; data.ReferenceID = "LOCL"; data.ReceiveTimestamp = packetInfo.RecievedTime; data.CopyTransmitToOriginate(); data.TransmitTimestamp = DateTime.UtcNow; socket.Send(packetInfo.SourceEndPoint, data); }
/// <summary> /// Gets the time delta from an NTP server. /// Does one round trip. /// </summary> /// <param name="host">The host.</param> /// <param name="timeSocket">The time socket.</param> /// <returns>The returned time packet</returns> private NtpData GetTimeDelta(IPEndPoint host, UdpClient timeSocket) { // Initialize data structure NtpData sendData = new NtpData(); sendData.Initialize(4, NtpMode.Client, DateTime.Now); byte[] sendBytes = sendData.ToArray(); timeSocket.Send(sendBytes, sendBytes.Length); NtpData receiveData = new NtpData(timeSocket.Receive(ref host)); receiveData.ReceptionTimestamp = DateTime.Now; if (!receiveData.IsResponseValid()) { throw new Exception("Invalid response from " + TimeServer); } return(receiveData); }