3#ifndef _TIME_SHIELD_NTP_CLIENT_HPP_INCLUDED
4#define _TIME_SHIELD_NTP_CLIENT_HPP_INCLUDED
14#if TIME_SHIELD_ENABLE_NTP_CLIENT
21#if TIME_SHIELD_PLATFORM_WINDOWS
23#elif TIME_SHIELD_PLATFORM_UNIX
33#if TIME_SHIELD_PLATFORM_WINDOWS
35#elif TIME_SHIELD_PLATFORM_UNIX
39#if TIME_SHIELD_PLATFORM_WINDOWS || TIME_SHIELD_PLATFORM_UNIX
48 NtpClient(std::string server =
"pool.ntp.org",
int port = 123)
49 :
m_host(std::move(server))
64#if TIME_SHIELD_PLATFORM_WINDOWS
80 const bool ok = core.
query(
149 static TIME_SHIELD_THREAD_LOCAL
int value = 0;
159 static_assert(
sizeof(
void*) == 0,
"NtpClient is disabled by configuration.");
173 static_assert(
sizeof(
void*) == 0,
"NtpClient is disabled by configuration.");
NTP client for measuring time offset.
int64_t offset_us() const noexcept
Returns the last measured offset in microseconds.
int64_t utc_time_ms() const noexcept
Returns current UTC time in milliseconds based on last NTP offset.
static const int k_default_timeout_ms
bool success() const noexcept
Returns whether the last NTP query was successful.
NtpClient(std::string server="pool.ntp.org", int port=123)
Constructs NTP client with specified host and port.
std::atomic< int64_t > m_delay_us
time_t utc_time_sec() const noexcept
Returns current UTC time as time_t (seconds since Unix epoch).
std::atomic< int > m_stratum
int last_error_code() const noexcept
Returns last socket error code (if any).
int64_t utc_time_us() const noexcept
Returns current UTC time in microseconds based on last NTP offset.
static int & last_error_code_slot() noexcept
std::atomic< int64_t > m_offset_us
int64_t delay_us() const noexcept
Returns the last measured delay in microseconds.
std::atomic< bool > m_is_success
bool query()
Queries the NTP server and updates the local offset.
int stratum() const noexcept
Returns the last received stratum value.
int ret_code() const noexcept
Returns the result code from WSAStartup.
static const WsaGuard & instance()
Returns the singleton instance, initializing WSA if needed.
Core NTP query logic that parses packets and computes offsets.
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.
Windows UDP transport for NTP queries.
Configuration macros for the library.
int64_t now_realtime_us()
Get current real time in microseconds using a platform-specific method.
UdpTransportWin PlatformUdpTransport
Main namespace for the Time Shield library.
Header file with time-related utility functions.