2#include <NDEVR/ItemDownloader.h>
3#include <NDEVR/String.h>
8#include <QtCore/QObject>
9#include <QtNetwork/QHostAddress>
10#include <QtNetwork/QUdpSocket>
12#include <NDEVR/NtpPacketFlags.h>
13#include <NDEVR/NtpTimestamp.h>
14#include <NDEVR/NtpPacket.h>
15#include <NDEVR/NtpMode.h>
16#include <NDEVR/NtpReply.h>
37 QHostInfo::lookupHost(host.
getAs<QString>(), [
this, bind_port, remote_port](
const QHostInfo& host)
39 if (host.error() != QHostInfo::NoError)
41 emit errorOccurredSignal(0);
44 QList<QHostAddress> addresses = host.addresses();
45 if(addresses.size() > 0)
47 if (bind(QHostAddress::Any, bind_port))
48 sendRequest(addresses[0], remote_port);
49 else if (bind(QHostAddress::Any, bind_port + 1))
50 sendRequest(addresses[0], remote_port);
51 else if (bind(QHostAddress::Any, bind_port + 2))
52 sendRequest(addresses[0], remote_port);
54 emit errorOccurredSignal(0);
58 emit errorOccurredSignal(0);
72 bool bind(
const QHostAddress& bindAddress = QHostAddress::Any, quint16 bindPort = 0)
74 m_socket.reset(
new QUdpSocket(
this));
75 if (!m_socket->bind(bindAddress, bindPort))
78 connect(m_socket.get(), &QUdpSocket::readyRead,
this, &NtpClient::readPendingDatagrams);
79 connect(m_socket.get(), &QUdpSocket::errorOccurred,
this, [
this]
81 emit errorOccurredSignal(0);
106 assert(m_socket->state() == QAbstractSocket::BoundState);
108 memset(&packet, 0,
sizeof(packet));
112 if (m_socket->writeDatagram(
reinterpret_cast<const char*
>(&packet),
sizeof(packet), address, port) < 0)
122 return m_socket.get();
139 void readPendingDatagrams()
141 while (m_socket->hasPendingDatagrams()) {
143 memset(&packet, 0,
sizeof(packet));
145 QHostAddress address;
147 if (m_socket->readDatagram(
reinterpret_cast<char*
>(&packet),
sizeof(packet), &address, &port) <
cast<qint64>(
sizeof(
NtpPacket)))
161 return m_reply.originTime();
169 return m_reply.destinationTime();
177 return m_reply.transmitTime();
181 std::unique_ptr<QUdpSocket> m_socket;
virtual ~NtpClient()
Virtual destructor.
bool bind(const QHostAddress &bindAddress=QHostAddress::Any, quint16 bindPort=0)
virtual Time txTime() const override
Returns the transmit time from the NTP reply.
virtual Time serverTime() const override
Returns the server time from the NTP reply origin timestamp.
NtpClient(const StringView &host, uint02 bind_port, uint02 remote_port)
Constructs an NTP client and initiates a time request.
virtual Time rxTime() const override
Returns the receive time from the NTP reply destination timestamp.
bool sendRequest(const QHostAddress &address, quint16 port)
Sends an NTP request to the specified address and port.
const QUdpSocket * socket() const
Returns the underlying UDP socket used for NTP communication.
void replyReceived(const QHostAddress &address, quint16 port, const NtpReply &reply)
This signal is emitted whenever NTP reply is received.
bool sendRequest(const String &address, quint16 port)
Sends NTP request.
Represents a parsed NTP reply received from an NTP server.
The core String View class for the NDEVR API.
t_type getAs() const
Converts a string into an object.
The core String class for the NDEVR API.
decltype(auto) getAs() const
Converts a string into an object.
A request to get the time from the network.
void finishedSignal()
Emitted when the time request has finished.
Represents a timestamp with utilities for manipulation and conversion.
static Time SystemTime()
Retrieves the current system time which is a combination of std::chrono::steady_clock to ensure smoot...
The primary namespace for the NDEVR SDK.
uint16_t uint02
-Defines an alias representing a 2 byte, unsigned integer -Can represent exact integer values 0 throu...
constexpr t_to cast(const Angle< t_from > &value)
Casts an Angle from one backing type to another.
Full NTP packet combining mandatory fields and optional authentication.
unsigned char versionNumber
Version number of the protocol (3 or 4).
Mandatory part of an NTP packet, containing all required fields.
NtpPacketFlags flags
Flags.
NtpTimestamp transmitTimestamp
The time at which the reply departed the server for client.
A 64-bit NTP timestamp with seconds and fractional parts since Jan 1, 1900.