Xclox
C++11 header-only cross-platform date and time library with an asynchronous NTP client
|
Client is an asynchronous multi-query NTP client. More...
#include <client.hpp>
Public Types | |
Aliases | |
using | Callback = Query::Callback |
Type of query callback. | |
using | Status = Query::Status |
Type of query status. | |
using | DefaultTimeout = Query::DefaultTimeout |
Type of query timeout holder. | |
Public Member Functions | |
Client (Callback callable) | |
Default constructor. More... | |
~Client () | |
Default destructor. | |
void | query (const std::string &server, const std::chrono::milliseconds &timeout=std::chrono::milliseconds(DefaultTimeout::ms)) |
Place a NTP query [thread-safe]. More... | |
void | setCallback (Callback callable) |
Register a callable for reporting the result of the query back to the caller. | |
void | cancel () |
Cancel all current queries [thread-safe]. | |
Client is an asynchronous multi-query NTP client.
Typically, a Client object is constructed by passing a callable of type Client::Callback to its constructor. User NTP query requests can be placed via query(). Once the query is finished, the registered callable is called back with the following details:
A default-constructed Client ignores any queries made on it if there is no registered callback. So, before issuing any query requests on such a Client, a callback has to be registered via setCallback().
Client first tries to resolve the server name; if resolving fails, Client::Status::ResolveError is reported. Otherwise, Client starts querying the resolved addresses one at a time until success or all addresses are queried.
Client awaits all pending queries until completion upon destruction. If you need to destruct a Client object as soon as possible, use cancel() to cancel all queries.
By default, each placed query has a total timeout period of 5 seconds to complete. If a query timed out, it is cancelled and reported to the caller with Client::Status::TimeoutError.
|
inlineexplicit |
Default constructor.
callable | is for reporting the result of each placed query back to the caller. |
|
inline |
Place a NTP query [thread-safe].
server | is a domain name or an IP address, optionally along with a custom port number in the form "host[:port]". The default port is "123". |
timeout | is the total time after which the query is cancelled if it is not completed. |