Template Class base_connector

Class Documentation

template<typename Stream>
class stream_client::connector::base_connector

Connector class to obtain new streams (sessions) to desired remote endpoint.

This class incorporates stream_client::resolver::base_resolver to provide DNS updates. Address to connect will be chosen randomly if DNS resolved into multiple records.

Note

Thread-safe. Single instance support concurrent operation.

tparam Stream

Type of stream to create upon new_session()

Public Types

using stream_type = typename std::remove_reference<Stream>::type
using protocol_type = typename stream_type::protocol_type
using endpoint_type = typename stream_type::endpoint_type
using endpoint_container_type = std::vector<endpoint_type>
using resolver_type = ::stream_client::resolver::base_resolver<protocol_type>
using resolve_flags_type = typename resolver_type::resolve_flags_type
using resolver_endpoint_iterator_type = typename resolver_type::iterator_type
using clock_type = typename stream_type::clock_type
using time_duration_type = typename stream_type::time_duration_type
using time_point_type = typename stream_type::time_point_type

Public Functions

base_connector(const std::string &host, const std::string &port, time_duration_type resolve_timeout, time_duration_type connect_timeout, time_duration_type operation_timeout, ::stream_client::resolver::ip_family ip_family = resolver_type::kDefaultIPFamily, resolve_flags_type resolve_flags = resolver_type::kDefaultFlags)

Parametrized constructor.

Creates connector with desired settings. This operation starts background resolving thread to obtain list of endpoints. Created connector does not establish any connections by itself, to do it use new_session().

Parameters
  • [in] host – Remote hostname.

  • [in] port – Remote port.

  • [in] resolve_timeout – DNS resolve timeout, used by internal resolve_routine().

  • [in] connect_timeout – Default timeout for connecting operation, used by new_session().

  • [in] operation_timeout – Timeout for I/O operations on established sessions.

  • [in] ip_family – Family of IP protocol to resolve (ipv4, ipv6 or any).

  • [in] resolve_flags – A set of flags that determine how name resolution should be performed.

base_connector(const base_connector<Stream> &other) = delete

Copy constructor is not permitted.

base_connector<Stream> &operator=(const base_connector<Stream> &other) = delete

Copy assignment is not permitted.

base_connector(base_connector<Stream> &&other) = delete

Move constructor is not permitted.

base_connector<Stream> &operator=(base_connector<Stream> &&other) = delete

Move assignment is not permitted.

virtual ~base_connector()

Destructor.

std::unique_ptr<stream_type> new_session(boost::system::error_code &ec, const time_point_type &deadline)

Establish new session.

Returns new connected stream. The call will block until one of the following conditions is true:

  • Successfully connected to remote.

  • An error or timeout occurred. In this case internal DNS resolver triggered to update list of endpoints.

Parameters
  • [in] deadline – Expiration time-point.

  • [out] ec – Set to indicate what error occurred, if any.

Returns

New session wrapped in std::unique_ptr or nullptr.

inline std::unique_ptr<stream_type> new_session(boost::system::error_code &ec, const time_duration_type &timeout)

Establish new session.

Returns new connected stream. The call will block until one of the following conditions is true:

  • Successfully connected to remote.

  • An error or timeout occurred. In this case internal DNS resolver triggered to update list of endpoints.

Parameters
  • [in] timeout – Expiration duration.

  • [out] ec – Set to indicate what error occurred, if any.

Returns

New session wrapped in std::unique_ptr or nullptr.

inline std::unique_ptr<stream_type> new_session(boost::system::error_code &ec)

Establish new session.

Returns new connected stream. Uses connect_timeout passed on construction as timeout value. The call will block until one of the following conditions is true:

  • Successfully connected to remote.

  • An error or timeout occurred. In this case internal DNS resolver triggered to update list of endpoints.

Parameters

[out] ec – Set to indicate what error occurred, if any.

Returns

New session wrapped in std::unique_ptr or nullptr.

inline std::unique_ptr<stream_type> new_session()

Establish new session.

Returns new connected stream. Uses connect_timeout passed on construction as timeout value. The call will block until one of the following conditions is true:

  • Successfully connected to remote.

  • An error or timeout occurred. In this case internal DNS resolver triggered to update list of endpoints.

Note

A successful call to this function is guaranteed to return valid pointer.

Throws

boost::system::system_error – Thrown on failure.

Returns

New session wrapped in std::unique_ptr. Guaranteed to return valid pointer.

inline std::string get_host() const

Get remote hostname.

Returns

A string representing remote host.

inline std::string get_port() const

Get remote port.

Returns

A string representing remote port.

inline std::string get_target() const

Get remote target in format ‘host:port’.

Returns

A string representing remote endpoint.

inline const time_duration_type &get_resolve_timeout() const

Get resolve operations timeout.

This function used to get current timeout for DNS resolve operations.

Returns

A const reference to the timeout.

inline const time_duration_type &get_connect_timeout() const

Get connect operations timeout.

This function used to get current timeout used in new_session() by default.

Returns

A const reference to the timeout.

inline const time_duration_type &get_operation_timeout() const

Get I/O operations timeout on result streams.

This function used to get I/O timeout used in new sessions returned by new_session().

Returns

A const reference to the timeout.

Protected Functions

virtual std::unique_ptr<stream_type> connect_until(const endpoint_type &peer_endpoint, const time_point_type &until_time) const

Try to connect to selected endpoint until deadline is not met.

void resolve_routine()

Background routine used to obtain and update remote endpoints.

inline void update_endpoints(resolver_endpoint_iterator_type &&resolved_endpoints)

Thread-safe setter for endpoints_.

inline endpoint_container_type get_endpoints()

Thread-safe getter for endpoints_.

inline void set_resolve_error(const boost::system::error_code &err)

Thread-safe setter for resolve_error_.

inline boost::system::error_code get_resolve_error()

Thread-safe getter for resolve_error_.

inline void notify_resolve_needed()

Trigger resolve_routine() to update endpoints_.

inline void notify_resolve_done()

Notify from resolve_routine() that endpoints_ updated.

inline std::unique_ptr<::stream_client::tcp_client> connect_until(const endpoint_type &peer_endpoint, const time_point_type &until_time) const
inline std::unique_ptr<::stream_client::udp_client> connect_until(const endpoint_type &peer_endpoint, const time_point_type &until_time) const
inline std::unique_ptr<::stream_client::ssl::ssl_client> connect_until(const endpoint_type &peer_endpoint, const time_point_type &until_time) const
inline std::unique_ptr<::stream_client::http::http_client> connect_until(const endpoint_type &peer_endpoint, const time_point_type &until_time) const
inline std::unique_ptr<::stream_client::http::https_client> connect_until(const endpoint_type &peer_endpoint, const time_point_type &until_time) const