NDEVR
API Documentation
NtpClient

NTP client that queries a remote NTP server for the current time. More...

Inheritance diagram for NtpClient:
[legend]
Collaboration diagram for NtpClient:
[legend]

Public Member Functions

 NtpClient (const StringView &host, uint02 bind_port, uint02 remote_port)
 Constructs an NTP client and initiates a time request.
virtual ~NtpClient ()
 Virtual destructor.
bool bind (const QHostAddress &bindAddress=QHostAddress::Any, quint16 bindPort=0)
void replyReceived (const QHostAddress &address, quint16 port, const NtpReply &reply)
 This signal is emitted whenever NTP reply is received.
bool sendRequest (const QHostAddress &address, quint16 port)
 Sends an NTP request to the specified address and port.
bool sendRequest (const String &address, quint16 port)
 Sends NTP request.
const QUdpSocket * socket () const
 Returns the underlying UDP socket used for NTP communication.
Public Member Functions inherited from TimeRequest
void errorOccurredSignal (int error)
 Emitted when an error occurs, with the error code.
void finishedSignal ()
 Emitted when the time request has finished.
TimeSpan localClockOffset () const
 Calculates the offset between the local clock and the server clock.
TimeSpan roundTripDelay () const
 Calculates the round-trip delay of the time request.

Private Member Functions

virtual Time rxTime () const override
 Returns the receive time from the NTP reply destination timestamp.
virtual Time serverTime () const override
 Returns the server time from the NTP reply origin timestamp.
virtual Time txTime () const override
 Returns the transmit time from the NTP reply.

Detailed Description

NTP client that queries a remote NTP server for the current time.

Inherits from TimeRequest and uses a QUdpSocket to send NTP requests and receive NTP replies. Emits replyReceived when a valid NTP response arrives.

Definition at line 25 of file NtpClient.h.

Constructor & Destructor Documentation

◆ NtpClient()

NtpClient::NtpClient ( const StringView & host,
uint02 bind_port,
uint02 remote_port )
inline

Constructs an NTP client and initiates a time request.

Parameters
[in]hostThe hostname of the NTP server to query.
[in]bind_portThe local UDP port to bind the socket to.
[in]remote_portThe remote NTP server port to send the request to.

Definition at line 35 of file NtpClient.h.

References StringView::getAs().

Member Function Documentation

◆ bind()

bool NtpClient::bind ( const QHostAddress & bindAddress = QHostAddress::Any,
quint16 bindPort = 0 )
inline
Parameters
bindAddressNetwork address to bind upd socket to. It's passed to QUdpSocket::bind.
bindPortNetwork port to bind udp socket to.
Returns
Whether the socket was successfully bound.

Definition at line 72 of file NtpClient.h.

◆ replyReceived()

void NtpClient::replyReceived ( const QHostAddress & address,
quint16 port,
const NtpReply & reply )

This signal is emitted whenever NTP reply is received.

Parameters
addressAddress of the server that sent this reply.
portPort of the server that sent this reply.
replyNTP reply.

References cast(), TimeRequest::finishedSignal(), replyReceived(), and Time::SystemTime().

Referenced by replyReceived().

◆ rxTime()

virtual Time NtpClient::rxTime ( ) const
inlineoverrideprivatevirtual

Returns the receive time from the NTP reply destination timestamp.

Returns
The destination time when the reply was received.

Implements TimeRequest.

Definition at line 167 of file NtpClient.h.

◆ sendRequest() [1/2]

bool NtpClient::sendRequest ( const QHostAddress & address,
quint16 port )
inline

Sends an NTP request to the specified address and port.

Parameters
[in]addressNTP server address.
[in]portNTP server port.
Returns
Whether the NTP request was successfully sent.

Definition at line 104 of file NtpClient.h.

References ClientMode, NtpPacket::flags, NtpPacketFlags::mode, Time::SystemTime(), NtpPacket::transmitTimestamp, and NtpPacketFlags::versionNumber.

◆ sendRequest() [2/2]

bool NtpClient::sendRequest ( const String & address,
quint16 port )
inline

Sends NTP request.

Parameters
addressNTP server address.
portNTP server port.
Returns
Whether the NTP request was successfully sent.

Definition at line 93 of file NtpClient.h.

References String::getAs(), and sendRequest().

Referenced by sendRequest().

◆ serverTime()

virtual Time NtpClient::serverTime ( ) const
inlineoverrideprivatevirtual

Returns the server time from the NTP reply origin timestamp.

Returns
The origin time from the NTP reply.

Implements TimeRequest.

Definition at line 159 of file NtpClient.h.

◆ socket()

const QUdpSocket * NtpClient::socket ( ) const
inline

Returns the underlying UDP socket used for NTP communication.

Returns
A pointer to the QUdpSocket.

Definition at line 121 of file NtpClient.h.

◆ txTime()

virtual Time NtpClient::txTime ( ) const
inlineoverrideprivatevirtual

Returns the transmit time from the NTP reply.

Returns
The time at which the server sent the reply.

Implements TimeRequest.

Definition at line 175 of file NtpClient.h.


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