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>
28 QHostInfo::lookupHost(host.
getAs<QString>(), [
this, bind_port, remote_port](
const QHostInfo& host)
30 if (host.error() != QHostInfo::NoError)
32 emit errorOccurredSignal(0);
35 QList<QHostAddress> addresses = host.addresses();
36 if(addresses.size() > 0)
38 if (bind(QHostAddress::Any, bind_port))
39 sendRequest(addresses[0], remote_port);
40 else if (bind(QHostAddress::Any, bind_port + 1))
41 sendRequest(addresses[0], remote_port);
42 else if (bind(QHostAddress::Any, bind_port + 2))
43 sendRequest(addresses[0], remote_port);
45 emit errorOccurredSignal(0);
49 emit errorOccurredSignal(0);
63 bool bind(
const QHostAddress& bindAddress = QHostAddress::Any, quint16 bindPort = 0)
65 m_socket.reset(
new QUdpSocket(
this));
66 if (!m_socket->bind(bindAddress, bindPort))
69 connect(m_socket.get(), &QUdpSocket::readyRead,
this, &NtpClient::readPendingDatagrams);
70 connect(m_socket.get(), &QUdpSocket::errorOccurred,
this, [
this]
72 emit errorOccurredSignal(0);
86 return sendRequest(QHostAddress(address.
getAs<QString>()), port);
91 assert(m_socket->state() == QAbstractSocket::BoundState);
93 memset(&packet, 0,
sizeof(packet));
97 if (m_socket->writeDatagram(
reinterpret_cast<const char*
>(&packet),
sizeof(packet), address, port) < 0)
103 return m_socket.get();
117 void readPendingDatagrams()
119 while (m_socket->hasPendingDatagrams()) {
121 memset(&packet, 0,
sizeof(packet));
123 QHostAddress address;
125 if (m_socket->readDatagram(
reinterpret_cast<char*
>(&packet),
sizeof(packet), &address, &port) <
cast<qint64>(
sizeof(
NtpPacket)))
130 emit finishedSignal();
133 virtual Time serverTime()
const override
135 return m_reply.originTime();
137 virtual Time rxTime()
const override
139 return m_reply.destinationTime();
141 virtual Time txTime()
const override
143 return m_reply.transmitTime();
147 std::unique_ptr<QUdpSocket> m_socket;
Definition NtpClient.h:23
bool bind(const QHostAddress &bindAddress=QHostAddress::Any, quint16 bindPort=0)
Definition NtpClient.h:63
bool sendRequest(const QHostAddress &address, quint16 port)
Definition NtpClient.h:89
NtpClient(const String &host, uint02 bind_port, uint02 remote_port)
Definition NtpClient.h:26
const QUdpSocket * socket() const
Definition NtpClient.h:102
virtual ~NtpClient()
Definition NtpClient.h:56
bool sendRequest(const String &address, quint16 port)
Definition NtpClient.h:84
void replyReceived(const QHostAddress &address, quint16 port, const NtpReply &reply)
The core String class for the software.
Definition String.h:47
t_type getAs() const
Definition String.h:341
static NDEVR_BASE_API Time SystemTime()
Retrieves the current system time.
Definition ItemDownloader.h:70
@ ClientMode
Definition NtpEnums.h:21
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:379
uint16_t uint02
-Defines an alias representing a 2 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:86
Definition NtpPacket.h:79
unsigned char versionNumber
Definition NtpPacket.h:16
unsigned char mode
Definition NtpPacket.h:13
Definition NtpPacket.h:28
NtpTimestamp transmitTimestamp
Definition NtpPacket.h:60
NtpPacketFlags flags
Definition NtpPacket.h:30
Definition NtpTimestamp.h:13