My Project
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Properties | List of all members
LiteNetLib.Utils.NtpPacket Class Reference

Represents RFC4330 SNTP packet used for communication to and from a network time server. More...

Public Member Functions

 NtpPacket ()
 Initializes default request packet.
 

Static Public Member Functions

static NtpPacket FromServerResponse (byte[] bytes, DateTime destinationTimestamp)
 Initializes packet from data received from a server.
 

Properties

byte[] Bytes [get]
 Gets RFC4330-encoded SNTP packet.
 
NtpLeapIndicator LeapIndicator [get]
 Gets the leap second indicator.
 
int VersionNumber [get]
 Gets or sets protocol version number.
 
NtpMode Mode [get]
 Gets or sets SNTP packet mode, i.e. whether this is client or server packet.
 
int Stratum [get]
 Gets server's distance from the reference clock.
 
int Poll [get]
 Gets server's preferred polling interval.
 
int Precision [get]
 Gets the precision of server clock.
 
TimeSpan RootDelay [get]
 Gets the total round-trip delay from the server to the reference clock.
 
TimeSpan RootDispersion [get]
 Gets the estimated error in time reported by the server.
 
uint ReferenceId [get]
 Gets the ID of the time source used by the server or Kiss-o'-Death code sent by the server.
 
DateTime? ReferenceTimestamp [get]
 Gets or sets the time when the server clock was last set or corrected.
 
DateTime? OriginTimestamp [get]
 Gets or sets the time when the client sent its request.
 
DateTime? ReceiveTimestamp [get]
 Gets or sets the time when the request was received by the server.
 
DateTime? TransmitTimestamp [get]
 Gets or sets the time when the packet was sent.
 
DateTime? DestinationTimestamp [get]
 Gets or sets the time of reception of response SNTP packet on the client.
 
TimeSpan RoundTripTime [get]
 Gets the round-trip time to the server.
 
TimeSpan CorrectionOffset [get]
 Gets the offset that should be added to local time to synchronize it with server time.
 

Detailed Description

Represents RFC4330 SNTP packet used for communication to and from a network time server.

Most applications should just use the NtpPacket.CorrectionOffset property.

The same data structure represents both request and reply packets. Request and reply differ in which properties are set and to what values.

The only real property is NtpPacket.Bytes. All other properties read from and write to the underlying byte array with the exception of NtpPacket.DestinationTimestamp, which is not part of the packet on network and it is instead set locally after receiving the packet.

Copied from GuerrillaNtp project with permission from Robert Vazan (@robertvazan) under MIT license, see https://github.com/RevenantX/LiteNetLib/pull/236

Constructor & Destructor Documentation

◆ NtpPacket()

LiteNetLib.Utils.NtpPacket.NtpPacket ( )
inline

Initializes default request packet.

Properties NtpPacket.Mode and NtpPacket.VersionNumber are set appropriately for request packet. Property NtpPacket.TransmitTimestamp is set to System.DateTime.UtcNow.

Member Function Documentation

◆ FromServerResponse()

static NtpPacket LiteNetLib.Utils.NtpPacket.FromServerResponse ( byte[]  bytes,
DateTime  destinationTimestamp 
)
inlinestatic

Initializes packet from data received from a server.

Parameters
bytesData received from the server.
destinationTimestampUtc time of reception of response SNTP packet on the client.
Returns

Property Documentation

◆ Bytes

byte [] LiteNetLib.Utils.NtpPacket.Bytes
get

Gets RFC4330-encoded SNTP packet.

Byte array containing RFC4330-encoded SNTP packet. It is at least 48 bytes long.

This is the only real property. All other properties except NtpPacket.DestinationTimestamp read from or write to this byte array.

◆ CorrectionOffset

TimeSpan LiteNetLib.Utils.NtpPacket.CorrectionOffset
get

Gets the offset that should be added to local time to synchronize it with server time.

Time difference between server and client. It should be added to local time to get server time. It is calculated from timestamps in the packet as 0.5 * ((t1 - t0) - (t3 - t2)) where t0 is NtpPacket.OriginTimestamp, t1 is NtpPacket.ReceiveTimestamp, t2 is NtpPacket.TransmitTimestamp, and t3 is NtpPacket.DestinationTimestamp. This property throws an exception in request packets.

◆ DestinationTimestamp

DateTime? LiteNetLib.Utils.NtpPacket.DestinationTimestamp
get

Gets or sets the time of reception of response SNTP packet on the client.

Time of reception of response SNTP packet on the client. It is null in request packets.

This property is not part of the protocol and has to be set when reply packet is received.

See also
NtpPacket.CorrectionOffset, NtpPacket.RoundTripTime

◆ LeapIndicator

NtpLeapIndicator LiteNetLib.Utils.NtpPacket.LeapIndicator
get

Gets the leap second indicator.

Leap second warning, if any. Special value NtpLeapIndicator.AlarmCondition indicates unsynchronized server clock. Default is NtpLeapIndicator.NoWarning.

Only servers fill in this property. Clients can consult this property for possible leap second warning.

◆ Mode

NtpMode LiteNetLib.Utils.NtpPacket.Mode
get

Gets or sets SNTP packet mode, i.e. whether this is client or server packet.

SNTP packet mode. Default is NtpMode.Client in newly created packets. Server reply should have this property set to NtpMode.Server.

◆ OriginTimestamp

DateTime? LiteNetLib.Utils.NtpPacket.OriginTimestamp
get

Gets or sets the time when the client sent its request.

This property is null in request packets. In reply packets, it is the time when the client sent its request. Servers copy this value from NtpPacket.TransmitTimestamp that they find in received request packet.

See also
NtpPacket.CorrectionOffset, NtpPacket.RoundTripTime

◆ Poll

int LiteNetLib.Utils.NtpPacket.Poll
get

Gets server's preferred polling interval.

Polling interval in log2 seconds, e.g. 4 stands for 16s and 17 means 131,072s.

◆ Precision

int LiteNetLib.Utils.NtpPacket.Precision
get

Gets the precision of server clock.

Clock precision in log2 seconds, e.g. -20 for microsecond precision.

◆ ReceiveTimestamp

DateTime? LiteNetLib.Utils.NtpPacket.ReceiveTimestamp
get

Gets or sets the time when the request was received by the server.

This property is null in request packets. In reply packets, it is the time when the server received client request.

See also
NtpPacket.CorrectionOffset, NtpPacket.RoundTripTime

◆ ReferenceId

uint LiteNetLib.Utils.NtpPacket.ReferenceId
get

Gets the ID of the time source used by the server or Kiss-o'-Death code sent by the server.

ID of server's time source or Kiss-o'-Death code. Purpose of this property depends on value of NtpPacket.Stratum property.

Stratum 1 servers write here one of several special values that describe the kind of hardware clock they use.

Stratum 2 and lower servers set this property to IPv4 address of their upstream server. If upstream server has IPv6 address, the address is hashed, because it doesn't fit in this property.

When server sets NtpPacket.Stratum to special value 0, this property contains so called kiss code that instructs the client to stop querying the server.

◆ ReferenceTimestamp

DateTime? LiteNetLib.Utils.NtpPacket.ReferenceTimestamp
get

Gets or sets the time when the server clock was last set or corrected.

Time when the server clock was last set or corrected or null when not specified.

This Property is usually set only by servers. It usually lags server's current time by several minutes, so don't use this property for time synchronization.

◆ RootDelay

TimeSpan LiteNetLib.Utils.NtpPacket.RootDelay
get

Gets the total round-trip delay from the server to the reference clock.

Round-trip delay to the reference clock. Normally a positive value smaller than one second.

◆ RootDispersion

TimeSpan LiteNetLib.Utils.NtpPacket.RootDispersion
get

Gets the estimated error in time reported by the server.

Estimated error in time reported by the server. Normally a positive value smaller than one second.

◆ RoundTripTime

TimeSpan LiteNetLib.Utils.NtpPacket.RoundTripTime
get

Gets the round-trip time to the server.

Time the request spent traveling to the server plus the time the reply spent traveling back. This is calculated from timestamps in the packet as (t1 - t0) + (t3 - t2) where t0 is NtpPacket.OriginTimestamp, t1 is NtpPacket.ReceiveTimestamp, t2 is NtpPacket.TransmitTimestamp, and t3 is NtpPacket.DestinationTimestamp. This property throws an exception in request packets.

◆ Stratum

int LiteNetLib.Utils.NtpPacket.Stratum
get

Gets server's distance from the reference clock.

Distance from the reference clock. This property is set only in server reply packets. Servers connected directly to reference clock hardware set this property to 1. Statum number is incremented by 1 on every hop down the NTP server hierarchy.

Special value 0 indicates that this packet is a Kiss-o'-Death message with kiss code stored in NtpPacket.ReferenceId.

◆ TransmitTimestamp

DateTime? LiteNetLib.Utils.NtpPacket.TransmitTimestamp
get

Gets or sets the time when the packet was sent.

Time when the packet was sent. It should never be null. Default value is System.DateTime.UtcNow.

This property must be set by both clients and servers.

See also
NtpPacket.CorrectionOffset, NtpPacket.RoundTripTime

◆ VersionNumber

int LiteNetLib.Utils.NtpPacket.VersionNumber
get

Gets or sets protocol version number.

SNTP protocol version. Default is 4, which is the latest version at the time of this writing.

In request packets, clients should leave this property at default value 4. Servers usually reply with the same protocol version.


The documentation for this class was generated from the following file: