Template Class base_socket

Nested Relationships

Inheritance Relationships

Base Type

Class Documentation

template<typename Socket>
class stream_client::base_socket : public stream_client::detail::timed_base<Clock>

Socket client for arbitrary plain (no encryption) protocol.

This class wraps boost::asio::basic_socket to make it timeout-blocked: all read/write operations has timeout/deadline option.

Note

Not thread-safe, multi-thread rw access may mess up your data stream and/or timeout handling.

tparam Socket

Type of underlying socket to use, see boost::asio::basic_socket.

Public Types

using next_layer_type = Socket
using protocol_type = typename next_layer_type::protocol_type
using lowest_layer_type = typename next_layer_type::lowest_layer_type
using native_handle_type = typename next_layer_type::native_handle_type
using endpoint_type = typename protocol_type::endpoint

Public Functions

base_socket(const endpoint_type &peer_endpoint, time_duration_type connect_timeout, time_duration_type operation_timeout)

Parametrized constructor.

Constructs socket connected to peer_endpoint. This operation blocks until socket is constructed or connect_timeout time elapsed.

Parameters
  • [in] peer_endpoint – Remote endpoint to connect.

  • [in] connect_timeout – Timeout for connection operation.

  • [in] operation_timeout – Subsequent I/O operation default timeout.

Throws

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

base_socket(const config &cfg)

Single-argument constructor. Needed to use it as base for boost::asio::ssl::stream.

base_socket(const base_socket<Socket> &other) = delete

Copy constructor is not permitted.

base_socket<Socket> &operator=(const base_socket<Socket> &other) = delete

Copy assignment is not permitted.

base_socket(base_socket<Socket> &&other) = default

Move constructor.

base_socket<Socket> &operator=(base_socket<Socket> &&other) = default

Move assignment.

virtual ~base_socket()

Destructor.

boost::system::error_code close()

Close the socket.

Wraps boost::asio::basic_socket::close. This function is used to close the socket. Any send or receive operations will be canceled immediately, and will complete with the boost::asio::error::operation_aborted error.

Returns

What error occurred, if any.

inline void close(boost::system::error_code &ec)

Close the socket.

Wraps boost::asio::basic_socket::close. This function is used to close the socket. Any send or receive operations will be canceled immediately, and will complete with the boost::asio::error::operation_aborted error.

Parameters

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

template<typename SettableSocketOption>
void set_option(const SettableSocketOption &option)

Set an option on the socket.

Template Parameters

SettableSocketOption – Type of option, see boost::asio ‘Socket Options’.

Parameters

[in] option – The new option value to be set on the socket.

Throws

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

template<typename SettableSocketOption>
inline boost::system::error_code set_option(const SettableSocketOption &option, boost::system::error_code &ec)

Set an option on the socket.

Template Parameters

SettableSocketOption – Type of option, see boost::asio ‘Socket Options’.

Parameters
  • [in] option – The new option value to be set on the socket.

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

Returns

Value of the error occurred, if any (same as ec).

template<typename GettableSocketOption>
void get_option(GettableSocketOption &option) const

Get an option from the socket.

Template Parameters

GettableSocketOption – Type of option, see boost::asio ‘Socket Options’.

Parameters

[in] option – The option value to be obtained from the socket.

Throws

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

template<typename GettableSocketOption>
inline boost::system::error_code get_option(GettableSocketOption &option, boost::system::error_code &ec) const

Get an option from the socket.

Template Parameters

GettableSocketOption – Type of option, see boost::asio ‘Socket Options’.

Parameters
  • [in] option – The option value to be obtained from the socket.

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

Returns

Value of the error occurred, if any (same as ec).

endpoint_type local_endpoint() const

Get the local endpoint of the socket.

Throws

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

Returns

An object that represents the local endpoint of the socket.

inline endpoint_type local_endpoint(boost::system::error_code &ec) const

Get the local endpoint of the socket.

Parameters

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

Returns

An object that represents the local endpoint of the socket.

endpoint_type remote_endpoint() const

Get the remote endpoint of the socket.

Throws

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

Returns

An object that represents the remote endpoint of the socket. Returns a default-constructed endpoint object if an error occurred.

inline endpoint_type remote_endpoint(boost::system::error_code &ec) const

Get the remote endpoint of the socket.

Parameters

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

Returns

An object that represents the remote endpoint of the socket. Returns a default-constructed endpoint object if an error occurred.

inline const next_layer_type &next_layer() const

Get a const reference to the underlying socket.

Returns

This function returns a const reference to the underlying socket.

inline next_layer_type &next_layer()

Get a reference to the underlying socket.

Returns

This function returns a reference to the underlying socket.

inline const lowest_layer_type &lowest_layer() const

Get a const reference to the lowest layer.

This function returns a reference to the lowest layer in a stack of layers. Since a boost::asio::basic_socket cannot contain any further layers, it simply returns a reference to underlying socket.

Returns

A const reference to the lowest layer in the stack of layers.

inline lowest_layer_type &lowest_layer()

Get a reference to the lowest layer.

This function returns a reference to the lowest layer in a stack of layers. Since a boost::asio::basic_socket cannot contain any further layers, it simply returns a reference to underlying socket.

Returns

A reference to the lowest layer in the stack of layers.

inline const time_duration_type &connection_timeout() const

Get connection timeout.

This function used to get connection timeout used at construction.

Returns

A const reference to the connection timeout.

inline const time_duration_type &io_timeout() const

Get I/O operations timeout.

This function used to get current value for I/O timeout.

Returns

A const reference to the timeout.

inline time_duration_type io_timeout(time_duration_type new_io_timeout)

Set I/O operations timeout.

This function used to set new value for I/O timeout.

Returns

Previous timeout value.

inline bool io_timeout_enabled() const

Get current status of I/O timeout.

This function used to check if I/O timeout enabled or not.

Returns

A boolean state of the timeout.

inline bool io_timeout_enabled(bool new_mode)

Set current status of I/O timeout.

This function used to enable or disable I/O timeout. If it’s disabled, rw functions without implicit timeout value will use indefinite timeout (blocking mode).

Returns

Previous state of the timeout.

inline bool is_open() const

Determine whether the underlying socket is open.

Protected Functions

virtual void deadline_actor() override

Timeout expiration handler which closes current socket.

template<typename ConstBufferSequence, typename WriteHandler, typename Time>
void async_send(const ConstBufferSequence &buffers, const Time &timeout_or_deadline, WriteHandler &&handler, bool setup_expiration)

Start an asynchronous send with timeout on the underlying socket.

template<typename MutableBufferSequence, typename ReadHandler, typename Time>
void async_receive(const MutableBufferSequence &buffers, const Time &timeout_or_deadline, ReadHandler &&handler, bool setup_expiration)

Start an asynchronous receive with timeout on the underlying socket.

Protected Attributes

next_layer_type socket_

Underlying socket to perform I/O operations.

time_duration_type connection_timeout_

Connection timeout value used upon construction.

time_duration_type io_operation_timeout_

Current I/O timeout value.

bool io_timeouted_

Flag to track current I/O timeout state.

struct config

Configuration parameters wrapped into single struct.

Public Members

const endpoint_type &peer_endpoint

Endpoint to connect.

time_duration_type connect_timeout

Connection timeout.

time_duration_type operation_timeout

Any rw function timeout.