Xclox
C++11 header-only cross-platform date and time library with an asynchronous NTP client
xclox::ntp::Client Class Reference

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].
 

Detailed Description

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:

  • NTP server name as it was provided by the caller
  • IP address if the server name is successfully resolved or an empty string otherwise
  • Client::Status flag indicating the final status of the query
  • Packet object representing the server's reply on success or a null packet otherwise
  • Elapsed time since sending the packet to the server

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.

See also
The unit tests in client.h for further details.

Constructor & Destructor Documentation

◆ Client()

xclox::ntp::Client::Client ( Callback  callable)
inlineexplicit

Default constructor.

Parameters
callableis for reporting the result of each placed query back to the caller.

Member Function Documentation

◆ query()

void xclox::ntp::Client::query ( const std::string &  server,
const std::chrono::milliseconds &  timeout = std::chrono::milliseconds(DefaultTimeout::ms) 
)
inline

Place a NTP query [thread-safe].

Parameters
serveris a domain name or an IP address, optionally along with a custom port number in the form "host[:port]". The default port is "123".
timeoutis the total time after which the query is cancelled if it is not completed.

The documentation for this class was generated from the following file: