![]() |
algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
|
The base of the WebSocket clients WsSession and WssSession: connecting, sending, receiving, closing and reconnecting. More...
#include <algotradelib/src/network/ws_session_base.hpp>
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 |
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.
| Derived | The transport class (WsSession or WssSession). |
| Stream | The WebSocket stream type. |
Definition at line 72 of file ws_session_base.hpp.
|
inlineprotected |
Constructs the session; nothing connects until connect().
| ioc | The I/O context. |
| host | The host. |
| port | The port. |
| options | The options. |
Definition at line 139 of file ws_session_base.hpp.
|
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.
Definition at line 255 of file ws_session_base.hpp.
|
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.
|
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.
|
inlineprotected |
Tears the session down after a failure, calls on_error (and on_closed if closing), and schedules a reconnect if configured.
| stage | The stage that failed. |
| error | The error. |
Definition at line 275 of file ws_session_base.hpp.
|
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().
|
inlineprotectedvirtual |
Called after a failed stage has torn the session down; it then reconnects if automatic_reconnect_ is set and close() was not called.
| stage | The stage, such as "connect", "handshake", "read" or "write". |
| error | The error. |
Definition at line 167 of file ws_session_base.hpp.
Referenced by fail().
|
protectedpure virtual |
Called with each complete message received.
| message | The message; valid only during the call. |
|
inlineprotectedvirtual |
Called when the session is connected (after each successful upgrade, reconnects included).
Definition at line 159 of file ws_session_base.hpp.
|
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.
| message | The message. |
Definition at line 93 of file ws_session_base.hpp.
|
inlineprotected |
Returns a shared pointer to this session as the transport type.
Definition at line 213 of file ws_session_base.hpp.
Referenced by close(), connect(), send(), start_resolve(), and start_ws_handshake().
|
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.
|
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.
|
friend |
Definition at line 293 of file ws_session_base.hpp.
|
protected |
The read buffer.
Definition at line 183 of file ws_session_base.hpp.
|
protected |
Whether a close is in progress.
Definition at line 199 of file ws_session_base.hpp.
Referenced by abandon_if_closing(), and fail().
|
protected |
Whether the session is connected.
Definition at line 195 of file ws_session_base.hpp.
Referenced by fail().
|
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().
|
protected |
The host.
Definition at line 187 of file ws_session_base.hpp.
Referenced by start_resolve(), and WebSocketSessionBase().
|
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().
|
protected |
The options.
Definition at line 193 of file ws_session_base.hpp.
Referenced by start_ws_handshake(), and WebSocketSessionBase().
|
protected |
The port.
Definition at line 189 of file ws_session_base.hpp.
Referenced by start_resolve(), and WebSocketSessionBase().
|
protected |
Reconnect attempts since the last successful connect.
Definition at line 205 of file ws_session_base.hpp.
|
protected |
The random source for reconnect jitter.
Definition at line 207 of file ws_session_base.hpp.
Referenced by WebSocketSessionBase().
|
protected |
The reconnect timer.
Definition at line 179 of file ws_session_base.hpp.
Referenced by WebSocketSessionBase().
|
protected |
The resolver.
Definition at line 177 of file ws_session_base.hpp.
Referenced by start_resolve(), and WebSocketSessionBase().
|
protected |
Whether close() was called (no reconnection).
Definition at line 203 of file ws_session_base.hpp.
|
protected |
The strand every handler runs on.
Definition at line 175 of file ws_session_base.hpp.
Referenced by close(), connect(), send(), and WebSocketSessionBase().
|
protected |
Messages waiting to be sent; the front one may be being written.
Definition at line 185 of file ws_session_base.hpp.
|
protected |
Whether a write is in progress.
Definition at line 201 of file ws_session_base.hpp.
Referenced by fail().
|
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().