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,
38
ssl::context& context,
39
std::string host,
40
std::string port,
41
WssSessionOptions
options = {}
42
)
43
:
WebSocketSessionBase
(ioc, std::move(host), std::move(port), std::move(options)),
44
context_(context) {}
45
56
WssSession
(
57
net::io_context& ioc,
58
ssl::context& context,
59
std::string host,
60
std::string port,
61
std::string target,
62
std::map<std::string, std::string> headers = {}
63
)
64
:
WssSession
(
65
ioc,
66
context,
67
std::move(host),
68
std::move(port),
69
WssSessionOptions
{std::move(target), std::move(headers)}
70
) {}
71
72
private
:
73
template
<
typename
,
typename
>
74
friend
class
WebSocketSessionBase;
75
friend
struct
WebSocketSessionTestAccess;
76
using
Stream = websocket::stream<ssl::stream<beast::tcp_stream>>;
77
78
ssl::context& context_;
79
80
void
do_connect() {
81
if
(
connected_
||
connecting_
||
closing_
||
stop_requested_
) {
82
return
;
83
}
84
ws_
= std::make_unique<Stream>(
strand_
, context_);
85
buffer_
.consume(
buffer_
.size());
86
if
(!SSL_set_tlsext_host_name(
ws_
->next_layer().native_handle(),
host_
.c_str())) {
87
fail
(
88
"sni"
,
89
beast::error_code(
static_cast<
int
>
(::ERR_get_error()), net::error::get_ssl_category())
90
);
91
return
;
92
}
93
ws_
->next_layer().set_verify_callback(ssl::host_name_verification(
host_
));
94
start_resolve
();
95
}
96
97
void
on_connect(beast::error_code error, tcp::resolver::results_type::endpoint_type endpoint) {
98
if
(error) {
99
fail
(
"connect"
, error);
100
return
;
101
}
102
if
(
abandon_if_closing
()) {
103
return
;
104
}
105
host_header_
=
host_
+
":"
+ std::to_string(endpoint.port());
106
beast::get_lowest_layer(*ws_).expires_after(
options_
.
operation_timeout_
);
107
ws_
->next_layer().async_handshake(
108
ssl::stream_base::client,
109
beast::bind_front_handler(&WssSession::on_ssl_handshake,
shared_derived
())
110
);
111
}
112
113
void
on_ssl_handshake(beast::error_code error) {
114
if
(error) {
115
fail
(
"ssl_handshake"
, error);
116
return
;
117
}
118
if
(
abandon_if_closing
()) {
119
return
;
120
}
121
start_ws_handshake
();
122
}
123
};
124
125
}
// namespace network
126
}
// 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:187
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::abandon_if_closing
bool abandon_if_closing()
Definition
ws_session_base.hpp:255
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::start_ws_handshake
void start_ws_handshake()
Definition
ws_session_base.hpp:229
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::options_
WebSocketSessionOptions options_
Definition
ws_session_base.hpp:193
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:275
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::stop_requested_
bool stop_requested_
Definition
ws_session_base.hpp:203
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::host_header_
std::string host_header_
Definition
ws_session_base.hpp:191
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::connected_
bool connected_
Definition
ws_session_base.hpp:195
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::start_resolve
void start_resolve()
Definition
ws_session_base.hpp:218
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::connecting_
bool connecting_
Definition
ws_session_base.hpp:197
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::buffer_
beast::flat_buffer buffer_
Definition
ws_session_base.hpp:183
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::closing_
bool closing_
Definition
ws_session_base.hpp:199
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:181
algotradelib::network::WebSocketSessionBase< WssSession, websocket::stream< ssl::stream< beast::tcp_stream > > >::shared_derived
std::shared_ptr< WssSession > shared_derived()
Definition
ws_session_base.hpp:213
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:175
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:56
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
http_client.hpp:45
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