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

A WebSocket client over plain TCP (no TLS); derive from it and override on_message. More...

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

Inheritance diagram for algotradelib::network::WsSession:
Collaboration diagram for algotradelib::network::WsSession:

Public Member Functions

 WsSession (net::io_context &ioc, std::string host, std::string port, std::string target="/", std::map< std::string, std::string > headers={})
 Constructs a session.
Public Member Functions inherited from algotradelib::network::WebSocketSessionBase< WsSession, websocket::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< WsSession, websocket::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< WsSession > 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< WsSession, websocket::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< 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 plain TCP (no TLS); derive from it and override on_message.

See WebSocketSessionBase for its use.

Definition at line 12 of file ws_session.hpp.

Constructor & Destructor Documentation

◆ WsSession()

algotradelib::network::WsSession::WsSession ( net::io_context & ioc,
std::string host,
std::string port,
std::string target = "/",
std::map< std::string, std::string > headers = {} )
inline

Constructs a session.

Parameters
iocThe I/O context.
hostThe host.
portThe port.
targetThe path of the upgrade request.
headersExtra headers sent with the upgrade request.

Definition at line 22 of file ws_session.hpp.

◆ WebSocketSessionBase

template<typename, typename>
friend class WebSocketSessionBase
friend

Definition at line 38 of file ws_session.hpp.

◆ WebSocketSessionTestAccess

friend struct WebSocketSessionTestAccess
friend

Definition at line 39 of file ws_session.hpp.


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