Time Shield Library
C++ library for working with time
Toggle main menu visibility
Loading...
Searching...
No Matches
ntp_client_core.hpp
Go to the documentation of this file.
1
// SPDX-License-Identifier: MIT
2
#pragma once
3
#ifndef TIME_SHIELD_HEADER_TIME_SHIELD_NTP_CLIENT_NTP_CLIENT_CORE_HPP_INCLUDED
4
#define TIME_SHIELD_HEADER_TIME_SHIELD_NTP_CLIENT_NTP_CLIENT_CORE_HPP_INCLUDED
5
6
#include "
ntp_packet.hpp
"
7
#include "
udp_transport.hpp
"
8
9
#include <cstdint>
10
#include <string>
11
12
namespace
time_shield
{
13
namespace
detail
{
14
16
class
NtpClientCore
{
17
public
:
19
bool
query
(
IUdpTransport
& transport,
20
const
std::string& host,
21
int
port,
22
int
timeout_ms,
23
int
& out_error_code,
24
int64_t& out_offset_us,
25
int64_t& out_delay_us,
26
int
& out_stratum)
noexcept
{
27
out_error_code = 0;
28
out_offset_us = 0;
29
out_delay_us = 0;
30
out_stratum = -1;
31
32
uint64_t now_us = 0;
33
if
(!
get_now_us
(now_us)) {
34
out_error_code = -1;
35
return
false
;
36
}
37
38
NtpPacket
pkt{};
39
fill_client_packet
(pkt, now_us);
40
41
NtpPacket
reply{};
42
UdpRequest
req;
43
req.
host
= host;
44
req.
port
= port;
45
req.
send_data
= &pkt;
46
req.
send_size
=
sizeof
(pkt);
47
req.
recv_data
= &reply;
48
req.
recv_size
=
sizeof
(reply);
49
req.
timeout_ms
= timeout_ms;
50
51
if
(!transport.transact(req, out_error_code)) {
52
if
(out_error_code == 0) {
53
out_error_code = -1;
54
}
55
return
false
;
56
}
57
58
uint64_t arrival_us = 0;
59
if
(!
get_now_us
(arrival_us)) {
60
out_error_code = -1;
61
return
false
;
62
}
63
64
if
(!
parse_server_packet
(reply, arrival_us, out_offset_us, out_delay_us, out_stratum, out_error_code)) {
65
if
(out_error_code == 0) {
66
out_error_code = -1;
67
}
68
return
false
;
69
}
70
71
return
true
;
72
}
73
74
private
:
76
static
bool
get_now_us
(uint64_t& out)
noexcept
{
77
const
int64_t v =
time_shield::now_realtime_us
();
78
if
(v < 0)
return
false
;
79
out =
static_cast<
uint64_t
>
(v);
80
return
true
;
81
}
82
};
83
84
}
// namespace detail
85
}
// namespace time_shield
86
87
#endif
// TIME_SHIELD_HEADER_TIME_SHIELD_NTP_CLIENT_NTP_CLIENT_CORE_HPP_INCLUDED
time_shield::detail::IUdpTransport
Abstract UDP transport interface for NTP queries.
Definition
udp_transport.hpp:24
time_shield::detail::NtpClientCore
Core NTP query logic that parses packets and computes offsets.
Definition
ntp_client_core.hpp:16
time_shield::detail::NtpClientCore::get_now_us
static bool get_now_us(uint64_t &out) noexcept
Read current realtime clock in microseconds.
Definition
ntp_client_core.hpp:76
time_shield::detail::NtpClientCore::query
bool query(IUdpTransport &transport, const std::string &host, int port, int timeout_ms, int &out_error_code, int64_t &out_offset_us, int64_t &out_delay_us, int &out_stratum) noexcept
Perform one NTP transaction using a UDP transport.
Definition
ntp_client_core.hpp:19
time_shield::now_realtime_us
int64_t now_realtime_us()
Get current real time in microseconds using a platform-specific method.
Definition
time_utils.hpp:61
time_shield::detail
Definition
fast_date.hpp:14
time_shield::detail::parse_server_packet
static bool parse_server_packet(const NtpPacket &pkt, uint64_t arrival_us, int64_t &offset_us, int64_t &delay_us, int &stratum, int &out_error_code) noexcept
Parse server response and compute offset and delay.
Definition
ntp_packet.hpp:95
time_shield::detail::fill_client_packet
static void fill_client_packet(NtpPacket &pkt, uint64_t now_us)
Fill an NTP client request packet using local time.
Definition
ntp_packet.hpp:83
time_shield
Main namespace for the Time Shield library.
ntp_packet.hpp
time_shield::detail::NtpPacket
NTP packet layout (48 bytes).
Definition
ntp_packet.hpp:20
time_shield::detail::UdpRequest
UDP request parameters for NTP transactions.
Definition
udp_transport.hpp:13
time_shield::detail::UdpRequest::send_size
std::size_t send_size
Outgoing payload size in bytes.
Definition
udp_transport.hpp:17
time_shield::detail::UdpRequest::timeout_ms
int timeout_ms
Receive timeout in milliseconds.
Definition
udp_transport.hpp:20
time_shield::detail::UdpRequest::host
std::string host
Target host name or IP address.
Definition
udp_transport.hpp:14
time_shield::detail::UdpRequest::recv_size
std::size_t recv_size
Receive buffer size in bytes.
Definition
udp_transport.hpp:19
time_shield::detail::UdpRequest::port
int port
Target port.
Definition
udp_transport.hpp:15
time_shield::detail::UdpRequest::send_data
const void * send_data
Pointer to outgoing payload.
Definition
udp_transport.hpp:16
time_shield::detail::UdpRequest::recv_data
void * recv_data
Pointer to receive buffer.
Definition
udp_transport.hpp:18
udp_transport.hpp
include
time_shield
ntp_client
ntp_client_core.hpp
Generated by
1.17.0