algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
algotradelib::network::WssSession Class Reference

A WebSocket client over TLS; derive from it and override on_message. More...

#include <algotradelib/src/network/wss_session.hpp>

Inheritance diagram for algotradelib::network::WssSession:
Collaboration diagram for algotradelib::network::WssSession:

Public Member Functions

 WssSession (net::io_context &ioc, ssl::context &context, std::string host, std::string port, WssSessionOptions options={})
 Constructs a session.
 WssSession (net::io_context &ioc, ssl::context &context, std::string host, std::string port, std::string target, std::map< std::string, std::string > headers={})
 Constructs a session with default options except the target and headers.
Public Member Functions inherited from algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >
void connect ()
 Connects asynchronously (does nothing if connected, connecting or closing), and re-enables reconnection after a close().
void send (std::string message)
 Queues a message; messages are sent in order once connected.
void close ()
 Closes the session asynchronously after any write in progress, and disables reconnection; on_closed is called when done.

Friends

template<typename, typename>
class WebSocketSessionBase
struct WebSocketSessionTestAccess

Additional Inherited Members

Protected Member Functions inherited from algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >
 WebSocketSessionBase (net::io_context &ioc, std::string host, std::string port, WebSocketSessionOptions options)
 Constructs the session; nothing connects until connect().
virtual ~WebSocketSessionBase ()=default
 Destroys the session.
virtual void on_message (std::string_view message)=0
 Called with each complete message received.
virtual void on_open ()
 Called when the session is connected (after each successful upgrade, reconnects included).
virtual void on_error (std::string_view stage, beast::error_code error)
 Called after a failed stage has torn the session down; it then reconnects if automatic_reconnect_ is set and close() was not called.
virtual void on_closed ()
 Called when the session has closed (by close() or by the server).
std::shared_ptr< WssSession > shared_derived ()
 Returns a shared pointer to this session as the transport type.
void start_resolve ()
 Starts the DNS lookup; called by the transport class's do_connect() once ws_ is ready.
void start_ws_handshake ()
 Sends the upgrade request; called once connected (WsSession) or after the TLS handshake (WssSession).
bool abandon_if_closing ()
 Ends a connect that close() interrupted, if it did; called first by each connect stage that completed without an error.
void fail (std::string_view stage, beast::error_code error)
 Tears the session down after a failure, calls on_error (and on_closed if closing), and schedules a reconnect if configured.
Protected Attributes inherited from algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >
net::strand< net::io_context::executor_type > strand_
 The strand every handler runs on.
tcp::resolver resolver_
 The resolver.
net::steady_timer reconnect_timer_
 The reconnect timer.
std::unique_ptr< websocket::stream< ssl::stream< beast::tcp_stream > > > ws_
 The stream, created by the transport class on each connect.
beast::flat_buffer buffer_
 The read buffer.
std::deque< std::string > write_queue_
 Messages waiting to be sent; the front one may be being written.
std::string host_
 The host.
std::string port_
 The port.
std::string host_header_
 The Host header value (host:port), set on connect.
WebSocketSessionOptions options_
 The options.
bool connected_
 Whether the session is connected.
bool connecting_
 Whether a connect is in progress.
bool closing_
 Whether a close is in progress.
bool writing_
 Whether a write is in progress.
bool stop_requested_
 Whether close() was called (no reconnection).
unsigned int reconnect_attempt_
 Reconnect attempts since the last successful connect.
std::minstd_rand reconnect_rng_
 The random source for reconnect jitter.

Detailed Description

A WebSocket client over TLS; derive from it and override on_message.

It installs SNI and a host-name check. For the peer to be verified, the TLS context must enable ssl::verify_peer and hold trusted roots.

See WebSocketSessionBase for its use.

Definition at line 25 of file wss_session.hpp.

Constructor & Destructor Documentation

◆ WssSession() [1/2]

algotradelib::network::WssSession::WssSession ( net::io_context & ioc,
ssl::context & context,
std::string host,
std::string port,
WssSessionOptions options = {} )
inline

Constructs a session.

Parameters
iocThe I/O context.
contextThe TLS context; borrowed, and must outlive the session (see the class description on peer verification).
hostThe host.
portThe port.
optionsThe options.

Definition at line 36 of file wss_session.hpp.

◆ WssSession() [2/2]

algotradelib::network::WssSession::WssSession ( net::io_context & ioc,
ssl::context & context,
std::string host,
std::string port,
std::string target,
std::map< std::string, std::string > headers = {} )
inline

Constructs a session with default options except the target and headers.

Parameters
iocThe I/O context.
contextThe TLS context; borrowed, and must outlive the session (see the class description on peer verification).
hostThe host.
portThe port.
targetThe path of the upgrade request.
headersExtra headers sent with the upgrade request.

Definition at line 56 of file wss_session.hpp.

◆ WebSocketSessionBase

template<typename, typename>
friend class WebSocketSessionBase
friend

Definition at line 74 of file wss_session.hpp.

◆ WebSocketSessionTestAccess

friend struct WebSocketSessionTestAccess
friend

Definition at line 75 of file wss_session.hpp.


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