-
Notifications
You must be signed in to change notification settings - Fork 0
/
ntp.h
55 lines (47 loc) · 1.11 KB
/
ntp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#ifndef NTP_H
#define NTP_H
#include <QObject>
#include <QUdpSocket>
#include <QDateTime>
#include "global.h"
#include "myfunctions.h"
struct NtpTimestamp{
unsigned int seconds; // Текущее время в UNIX time
unsigned int fraction; // Доли секунды
};
struct NtpPacket{
unsigned char mode: 3;
unsigned char versionNumber: 3;
unsigned char leapIndicator: 2;
unsigned char stratum;
unsigned char poll;
unsigned char precision;
unsigned int rootDelay;
unsigned int rootDispersion;
unsigned char referenceID[4];
NtpTimestamp referenceTimestamp;
NtpTimestamp originateTimestamp;
NtpTimestamp receiveTimestamp;
NtpTimestamp transmitTimestamp;
};
class NtpClient : public QObject
{
Q_OBJECT
public:
explicit NtpClient(QObject *parent = nullptr);
bool init();
void sync();
bool isError(){ return ( m_errorCount > 0 ) ? true : false; }
uint32_t getTime(){ return m_dateTime.toTime_t(); }
signals:
void signal_error();
void signal_getTime();
private slots:
void slot_readyRead();
private:
QUdpSocket* m_pSocket;
bool m_init;
QDateTime m_dateTime;
uint8_t m_errorCount;
};
#endif // NTP_H