Simple Windows-only NTP client for measuring time offset.
More...
#include <ntp_client.hpp>
|
| | NtpClient (std::string server="pool.ntp.org", int port=123) |
| | Constructs NTP client with specified host and port.
|
| |
| bool | query () |
| | Queries the NTP server and updates the local offset.
|
| |
| bool | success () const noexcept |
| | Returns whether the last NTP query was successful.
|
| |
| int64_t | get_offset_us () const noexcept |
| | Returns the last measured offset in microseconds.
|
| |
| int64_t | get_utc_time_us () const noexcept |
| | Returns current UTC time in microseconds based on last NTP offset.
|
| |
| int64_t | get_utc_time_ms () const noexcept |
| | Returns current UTC time in milliseconds based on last NTP offset.
|
| |
| time_t | get_utc_time () const noexcept |
| | Returns current UTC time as time_t (seconds since Unix epoch).
|
| |
| int | get_last_error_code () const noexcept |
| | Returns last WinSock error code (if any).
|
| |
| | NtpClient () |
| |
|
| static constexpr int64_t | NTP_TIMESTAMP_DELTA = 2208988800ll |
| | Seconds between 1900 and 1970 epochs.
|
| |
Simple Windows-only NTP client for measuring time offset.
Placeholder used when NTP client is disabled.
Definition at line 34 of file ntp_client.hpp.
◆ NtpClient() [1/2]
| time_shield::NtpClient::NtpClient |
( |
std::string | server = "pool.ntp.org", |
|
|
int | port = 123 ) |
|
inline |
Constructs NTP client with specified host and port.
Definition at line 37 of file ntp_client.hpp.
◆ NtpClient() [2/2]
| time_shield::NtpClient::NtpClient |
( |
| ) |
|
|
inline |
◆ fill_packet()
| void time_shield::NtpClient::fill_packet |
( |
ntp_packet & | pkt | ) |
const |
|
inlineprivate |
Converts local time to NTP timestamp format.
Definition at line 184 of file ntp_client.hpp.
◆ get_last_error_code()
| int time_shield::NtpClient::get_last_error_code |
( |
| ) |
const |
|
inlinenoexcept |
Returns last WinSock error code (if any).
Definition at line 144 of file ntp_client.hpp.
◆ get_offset_us()
| int64_t time_shield::NtpClient::get_offset_us |
( |
| ) |
const |
|
inlinenoexcept |
Returns the last measured offset in microseconds.
Definition at line 120 of file ntp_client.hpp.
◆ get_utc_time()
| time_t time_shield::NtpClient::get_utc_time |
( |
| ) |
const |
|
inlinenoexcept |
Returns current UTC time as time_t (seconds since Unix epoch).
- Returns
- UTC time in seconds.
Definition at line 139 of file ntp_client.hpp.
◆ get_utc_time_ms()
| int64_t time_shield::NtpClient::get_utc_time_ms |
( |
| ) |
const |
|
inlinenoexcept |
Returns current UTC time in milliseconds based on last NTP offset.
- Returns
- Current UTC time in ms.
Definition at line 133 of file ntp_client.hpp.
◆ get_utc_time_us()
| int64_t time_shield::NtpClient::get_utc_time_us |
( |
| ) |
const |
|
inlinenoexcept |
Returns current UTC time in microseconds based on last NTP offset.
- Returns
- Current UTC time in µs.
Definition at line 126 of file ntp_client.hpp.
◆ last_error_code_slot()
| static int & time_shield::NtpClient::last_error_code_slot |
( |
| ) |
|
|
inlinestaticprivatenoexcept |
◆ parse_packet()
| bool time_shield::NtpClient::parse_packet |
( |
const ntp_packet & | pkt, |
|
|
int64_t & | result_offset_us ) const |
|
inlineprivate |
Parses response and calculates offset.
Definition at line 197 of file ntp_client.hpp.
◆ query()
| bool time_shield::NtpClient::query |
( |
| ) |
|
|
inline |
Queries the NTP server and updates the local offset.
- Returns
- true if successful.
Definition at line 44 of file ntp_client.hpp.
◆ success()
| bool time_shield::NtpClient::success |
( |
| ) |
const |
|
inlinenoexcept |
Returns whether the last NTP query was successful.
- Returns
- True if the last offset measurement succeeded.
Definition at line 115 of file ntp_client.hpp.
◆ m_host
| std::string time_shield::NtpClient::m_host |
|
private |
◆ m_is_success
| std::atomic<bool> time_shield::NtpClient::m_is_success {false} |
|
private |
◆ m_offset_us
| std::atomic<int64_t> time_shield::NtpClient::m_offset_us {0} |
|
private |
◆ m_port
| int time_shield::NtpClient::m_port = 123 |
|
private |
◆ NTP_TIMESTAMP_DELTA
| int64_t time_shield::NtpClient::NTP_TIMESTAMP_DELTA = 2208988800ll |
|
staticconstexprprivate |
The documentation for this class was generated from the following file: