algotradelib
0.0.0.9000
C++ header-only library for algorithmic trading
Toggle main menu visibility
Loading...
Searching...
No Matches
wss_session.hpp
1
#pragma once
2
3
#include "./ws_session_base.hpp"
4
5
#include <boost/asio/ssl.hpp>
6
#include <boost/beast/websocket/ssl.hpp>
7
8
namespace
algotradelib
{
9
10
namespace
ssl = net::ssl;
11
12
namespace
network
{
13
15
using
WssSessionOptions
=
WebSocketSessionOptions
;
16
25
class
WssSession
:
public
WebSocketSessionBase<WssSession, websocket::stream<ssl::stream<beast::tcp_stream>>> {
26
public
:
36
WssSession
(
37
net::io_context& ioc, ssl::context& context, std::string host, std::string port,
WssSessionOptions
options = {}
38
)
39
:
WebSocketSessionBase
(ioc, std::move(host), std::move(port), std::move(options)), context_(context) {}
40
51
WssSession
(
52
net::io_context& ioc, ssl::context& context, std::string host, std::string port, std::string target,
53
std::map<std::string, std::string> headers = {}
54
)
55
:
WssSession
(
56
ioc, context, std::move(host), std::move(port),
WssSessionOptions
{std::move(target), std::move(headers)}
57
) {}
58
59
private
:
60
template
<
typename
,
typename
>
61
friend
class
WebSocketSessionBase;
62
friend
struct
WebSocketSessionTestAccess;
63
using
Stream = websocket::stream<ssl::stream<beast::tcp_stream>>;
64
65
ssl::context& context_;
66
67
void
do_connect() {
68
if
(
connected_
||
connecting_
||
closing_
||
stop_requested_
) {
69
return
;
70
}
71
ws_
= std::make_unique<Stream>(
strand_
, context_);
72
buffer_
.consume(
buffer_
.size());
73
if
(!SSL_set_tlsext_host_name(
ws_
->next_layer().native_handle(),
host_
.c_str())) {
74
fail
(
"sni"
, beast::error_code(
static_cast<
int
>
(::ERR_get_error()), net::error::get_ssl_category()));
75
return
;
76
}
77
ws_
->next_layer().set_verify_callback(ssl::host_name_verification(
host_
));
78
start_resolve
();
79
}
80
81
void
on_connect(beast::error_code error, tcp::resolver::results_type::endpoint_type endpoint) {
82
if
(error) {
83
fail
(
"connect"
, error);
84
return
;
85
}
86
if
(
abandon_if_closing
()) {
87
return
;
88
}
89
host_header_
=
host_
+
":"
+ std::to_string(endpoint.port());
90
beast::get_lowest_layer(*ws_).expires_after(
options_
.
operation_timeout_
);
91
ws_
->next_layer().async_handshake(
92
ssl::stream_base::client, beast::bind_front_handler(&WssSession::on_ssl_handshake,
shared_derived
())
93
);
94
}
95
96
void
on_ssl_handshake(beast::error_code error) {
97
if
(error) {
98
fail
(
"ssl_handshake"
, error);
99
return
;
100
}
101
if
(
abandon_if_closing
()) {
102
return
;
103
}
104
start_ws_handshake
();
105
}
106
};
107
108
}
// namespace network
109
}
// namespace algotradelib
algotradelib::network::WebSocketSessionBase
The base of the WebSocket clients WsSession and WssSession: connecting, sending, receiving,...
Definition
ws_session_base.hpp:72
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::host_
std::string host_
Definition
ws_session_base.hpp:186
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::abandon_if_closing
bool abandon_if_closing()
Definition
ws_session_base.hpp:252
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::start_ws_handshake
void start_ws_handshake()
Definition
ws_session_base.hpp:228
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::options_
WebSocketSessionOptions options_
Definition
ws_session_base.hpp:192
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::fail
void fail(std::string_view stage, beast::error_code error)
Definition
ws_session_base.hpp:272
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::stop_requested_
bool stop_requested_
Definition
ws_session_base.hpp:202
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::host_header_
std::string host_header_
Definition
ws_session_base.hpp:190
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::connected_
bool connected_
Definition
ws_session_base.hpp:194
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::start_resolve
void start_resolve()
Definition
ws_session_base.hpp:217
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::connecting_
bool connecting_
Definition
ws_session_base.hpp:196
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::buffer_
beast::flat_buffer buffer_
Definition
ws_session_base.hpp:182
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::closing_
bool closing_
Definition
ws_session_base.hpp:198
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::ws_
std::unique_ptr< websocket::stream< ssl::stream< beast::tcp_stream > > > ws_
Definition
ws_session_base.hpp:180
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::shared_derived
std::shared_ptr< WssSession > shared_derived()
Definition
ws_session_base.hpp:212
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::strand_
net::strand< net::io_context::executor_type > strand_
Definition
ws_session_base.hpp:174
algotradelib::network::WssSession
A WebSocket client over TLS; derive from it and override on_message.
Definition
wss_session.hpp:25
algotradelib::network::WssSession::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={})
Constructs a session with default options except the target and headers.
Definition
wss_session.hpp:51
algotradelib::network::WssSession::WssSession
WssSession(net::io_context &ioc, ssl::context &context, std::string host, std::string port, WssSessionOptions options={})
Constructs a session.
Definition
wss_session.hpp:36
algotradelib::network
HTTP and WebSocket transport, OAuth tokens, and request pacing.
Definition
calendar_cache.hpp:10
algotradelib::network::WssSessionOptions
WebSocketSessionOptions WssSessionOptions
Options for a WssSession.
Definition
wss_session.hpp:15
algotradelib
The library: broker-neutral types (core), and the modules below.
Definition
conversion.hpp:13
algotradelib::network::WebSocketSessionOptions
Options for a WebSocket session (WsSession, WssSession).
Definition
ws_session_base.hpp:40
algotradelib::network::WebSocketSessionOptions::operation_timeout_
std::chrono::seconds operation_timeout_
The deadline for the TCP connect and (for WssSession) the TLS handshake. The DNS lookup has no deadli...
Definition
ws_session_base.hpp:51
algotradelib
src
network
wss_session.hpp
Generated by
1.18.0