algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
algotradelib::network::WebSocketSessionBase< Derived, Stream > Class Template Referenceabstract

The base of the WebSocket clients WsSession and WssSession: connecting, sending, receiving, closing and reconnecting. More...

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

Inheritance diagram for algotradelib::network::WebSocketSessionBase< Derived, Stream >:
Collaboration diagram for algotradelib::network::WebSocketSessionBase< Derived, Stream >:

Public Member Functions

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.

Protected Member Functions

 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< Derived > 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

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< 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_ {false}
 Whether the session is connected.
bool connecting_ {false}
 Whether a connect is in progress.
bool closing_ {false}
 Whether a close is in progress.
bool writing_ {false}
 Whether a write is in progress.
bool stop_requested_ {false}
 Whether close() was called (no reconnection).
unsigned int reconnect_attempt_ {0}
 Reconnect attempts since the last successful connect.
std::minstd_rand reconnect_rng_
 The random source for reconnect jitter.

Friends

struct WebSocketSessionTestAccess

Detailed Description

template<typename Derived, typename Stream>
class algotradelib::network::WebSocketSessionBase< Derived, Stream >

The base of the WebSocket clients WsSession and WssSession: connecting, sending, receiving, closing and reconnecting.

To use a session, derive from WsSession or WssSession, override on_message (and optionally on_open, on_error, on_closed), create it with std::make_shared, and call connect(). All handlers run on the session's strand.

Template Parameters
DerivedThe transport class (WsSession or WssSession).
StreamThe WebSocket stream type.

Definition at line 72 of file ws_session_base.hpp.

Constructor & Destructor Documentation

◆ WebSocketSessionBase()

template<typename Derived, typename Stream>
algotradelib::network::WebSocketSessionBase< Derived, Stream >::WebSocketSessionBase ( net::io_context & ioc,
std::string host,
std::string port,
WebSocketSessionOptions options )
inlineprotected

Constructs the session; nothing connects until connect().

Parameters
iocThe I/O context.
hostThe host.
portThe port.
optionsThe options.

Definition at line 139 of file ws_session_base.hpp.

Member Function Documentation

◆ abandon_if_closing()

template<typename Derived, typename Stream>
bool algotradelib::network::WebSocketSessionBase< Derived, Stream >::abandon_if_closing ( )
inlineprotected

Ends a connect that close() interrupted, if it did; called first by each connect stage that completed without an error.

close() cancels the stage in progress, but a stage whose completion was already queued arrives without an error. Going on would open a session whose close() is spent: on_closed would never be called, and a later close() would return early because one is in progress.

Returns
true if the session was closing: it is torn down and on_closed has been called.

Definition at line 255 of file ws_session_base.hpp.

◆ close()

template<typename Derived, typename Stream>
void algotradelib::network::WebSocketSessionBase< Derived, Stream >::close ( )
inline

Closes the session asynchronously after any write in progress, and disables reconnection; on_closed is called when done.

Definition at line 106 of file ws_session_base.hpp.

◆ connect()

template<typename Derived, typename Stream>
void algotradelib::network::WebSocketSessionBase< Derived, Stream >::connect ( )
inline

Connects asynchronously (does nothing if connected, connecting or closing), and re-enables reconnection after a close().

Definition at line 78 of file ws_session_base.hpp.

◆ fail()

template<typename Derived, typename Stream>
void algotradelib::network::WebSocketSessionBase< Derived, Stream >::fail ( std::string_view stage,
beast::error_code error )
inlineprotected

Tears the session down after a failure, calls on_error (and on_closed if closing), and schedules a reconnect if configured.

Parameters
stageThe stage that failed.
errorThe error.

Definition at line 275 of file ws_session_base.hpp.

◆ on_closed()

template<typename Derived, typename Stream>
virtual void algotradelib::network::WebSocketSessionBase< Derived, Stream >::on_closed ( )
inlineprotectedvirtual

Called when the session has closed (by close() or by the server).

Definition at line 172 of file ws_session_base.hpp.

Referenced by abandon_if_closing(), and fail().

◆ on_error()

template<typename Derived, typename Stream>
virtual void algotradelib::network::WebSocketSessionBase< Derived, Stream >::on_error ( std::string_view stage,
beast::error_code error )
inlineprotectedvirtual

Called after a failed stage has torn the session down; it then reconnects if automatic_reconnect_ is set and close() was not called.

Parameters
stageThe stage, such as "connect", "handshake", "read" or "write".
errorThe error.

Definition at line 167 of file ws_session_base.hpp.

Referenced by fail().

◆ on_message()

template<typename Derived, typename Stream>
virtual void algotradelib::network::WebSocketSessionBase< Derived, Stream >::on_message ( std::string_view message)
protectedpure virtual

Called with each complete message received.

Parameters
messageThe message; valid only during the call.

◆ on_open()

template<typename Derived, typename Stream>
virtual void algotradelib::network::WebSocketSessionBase< Derived, Stream >::on_open ( )
inlineprotectedvirtual

Called when the session is connected (after each successful upgrade, reconnects included).

Definition at line 159 of file ws_session_base.hpp.

◆ send()

template<typename Derived, typename Stream>
void algotradelib::network::WebSocketSessionBase< Derived, Stream >::send ( std::string message)
inline

Queues a message; messages are sent in order once connected.

A message whose write failed stays queued and is sent again after a reconnect, so the server may receive it twice. close() does not clear the queue.

Parameters
messageThe message.

Definition at line 93 of file ws_session_base.hpp.

◆ shared_derived()

template<typename Derived, typename Stream>
std::shared_ptr< Derived > algotradelib::network::WebSocketSessionBase< Derived, Stream >::shared_derived ( )
inlineprotected

Returns a shared pointer to this session as the transport type.

Returns
The pointer.

Definition at line 213 of file ws_session_base.hpp.

Referenced by close(), connect(), send(), start_resolve(), and start_ws_handshake().

◆ start_resolve()

template<typename Derived, typename Stream>
void algotradelib::network::WebSocketSessionBase< Derived, Stream >::start_resolve ( )
inlineprotected

Starts the DNS lookup; called by the transport class's do_connect() once ws_ is ready.

Definition at line 218 of file ws_session_base.hpp.

◆ start_ws_handshake()

template<typename Derived, typename Stream>
void algotradelib::network::WebSocketSessionBase< Derived, Stream >::start_ws_handshake ( )
inlineprotected

Sends the upgrade request; called once connected (WsSession) or after the TLS handshake (WssSession).

Definition at line 229 of file ws_session_base.hpp.

◆ WebSocketSessionTestAccess

template<typename Derived, typename Stream>
friend struct WebSocketSessionTestAccess
friend

Definition at line 293 of file ws_session_base.hpp.

Member Data Documentation

◆ buffer_

template<typename Derived, typename Stream>
beast::flat_buffer algotradelib::network::WebSocketSessionBase< Derived, Stream >::buffer_
protected

The read buffer.

Definition at line 183 of file ws_session_base.hpp.

◆ closing_

template<typename Derived, typename Stream>
bool algotradelib::network::WebSocketSessionBase< Derived, Stream >::closing_ {false}
protected

Whether a close is in progress.

Definition at line 199 of file ws_session_base.hpp.

Referenced by abandon_if_closing(), and fail().

◆ connected_

template<typename Derived, typename Stream>
bool algotradelib::network::WebSocketSessionBase< Derived, Stream >::connected_ {false}
protected

Whether the session is connected.

Definition at line 195 of file ws_session_base.hpp.

Referenced by fail().

◆ connecting_

template<typename Derived, typename Stream>
bool algotradelib::network::WebSocketSessionBase< Derived, Stream >::connecting_ {false}
protected

Whether a connect is in progress.

Definition at line 197 of file ws_session_base.hpp.

Referenced by abandon_if_closing(), fail(), and start_resolve().

◆ host_

template<typename Derived, typename Stream>
std::string algotradelib::network::WebSocketSessionBase< Derived, Stream >::host_
protected

The host.

Definition at line 187 of file ws_session_base.hpp.

Referenced by start_resolve(), and WebSocketSessionBase().

◆ host_header_

template<typename Derived, typename Stream>
std::string algotradelib::network::WebSocketSessionBase< Derived, Stream >::host_header_
protected

The Host header value (host:port), set on connect.

Definition at line 191 of file ws_session_base.hpp.

Referenced by start_ws_handshake().

◆ options_

template<typename Derived, typename Stream>
WebSocketSessionOptions algotradelib::network::WebSocketSessionBase< Derived, Stream >::options_
protected

The options.

Definition at line 193 of file ws_session_base.hpp.

Referenced by start_ws_handshake(), and WebSocketSessionBase().

◆ port_

template<typename Derived, typename Stream>
std::string algotradelib::network::WebSocketSessionBase< Derived, Stream >::port_
protected

The port.

Definition at line 189 of file ws_session_base.hpp.

Referenced by start_resolve(), and WebSocketSessionBase().

◆ reconnect_attempt_

template<typename Derived, typename Stream>
unsigned int algotradelib::network::WebSocketSessionBase< Derived, Stream >::reconnect_attempt_ {0}
protected

Reconnect attempts since the last successful connect.

Definition at line 205 of file ws_session_base.hpp.

◆ reconnect_rng_

template<typename Derived, typename Stream>
std::minstd_rand algotradelib::network::WebSocketSessionBase< Derived, Stream >::reconnect_rng_
protected

The random source for reconnect jitter.

Definition at line 207 of file ws_session_base.hpp.

Referenced by WebSocketSessionBase().

◆ reconnect_timer_

template<typename Derived, typename Stream>
net::steady_timer algotradelib::network::WebSocketSessionBase< Derived, Stream >::reconnect_timer_
protected

The reconnect timer.

Definition at line 179 of file ws_session_base.hpp.

Referenced by WebSocketSessionBase().

◆ resolver_

template<typename Derived, typename Stream>
tcp::resolver algotradelib::network::WebSocketSessionBase< Derived, Stream >::resolver_
protected

The resolver.

Definition at line 177 of file ws_session_base.hpp.

Referenced by start_resolve(), and WebSocketSessionBase().

◆ stop_requested_

template<typename Derived, typename Stream>
bool algotradelib::network::WebSocketSessionBase< Derived, Stream >::stop_requested_ {false}
protected

Whether close() was called (no reconnection).

Definition at line 203 of file ws_session_base.hpp.

◆ strand_

template<typename Derived, typename Stream>
net::strand<net::io_context::executor_type> algotradelib::network::WebSocketSessionBase< Derived, Stream >::strand_
protected

The strand every handler runs on.

Definition at line 175 of file ws_session_base.hpp.

Referenced by close(), connect(), send(), and WebSocketSessionBase().

◆ write_queue_

template<typename Derived, typename Stream>
std::deque<std::string> algotradelib::network::WebSocketSessionBase< Derived, Stream >::write_queue_
protected

Messages waiting to be sent; the front one may be being written.

Definition at line 185 of file ws_session_base.hpp.

◆ writing_

template<typename Derived, typename Stream>
bool algotradelib::network::WebSocketSessionBase< Derived, Stream >::writing_ {false}
protected

Whether a write is in progress.

Definition at line 201 of file ws_session_base.hpp.

Referenced by fail().

◆ ws_

template<typename Derived, typename Stream>
std::unique_ptr<Stream> algotradelib::network::WebSocketSessionBase< Derived, Stream >::ws_
protected

The stream, created by the transport class on each connect.

Definition at line 181 of file ws_session_base.hpp.

Referenced by abandon_if_closing(), fail(), and start_ws_handshake().


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