algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
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
8namespace algotradelib {
9
10namespace ssl = net::ssl;
11
12namespace network {
13
16
25class WssSession : public WebSocketSessionBase<WssSession, websocket::stream<ssl::stream<beast::tcp_stream>>> {
26public:
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
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
72private:
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() {
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_));
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 }
122 }
123};
124
125} // namespace network
126} // namespace algotradelib
The base of the WebSocket clients WsSession and WssSession: connecting, sending, receiving,...
A WebSocket client over TLS; derive from it and override on_message.
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.
WssSession(net::io_context &ioc, ssl::context &context, std::string host, std::string port, WssSessionOptions options={})
Constructs a session.
HTTP and WebSocket transport, OAuth tokens, and request pacing.
WebSocketSessionOptions WssSessionOptions
Options for a WssSession.
The library: broker-neutral types (core), and the modules below.
Options for a WebSocket session (WsSession, WssSession).
std::chrono::seconds operation_timeout_
The deadline for the TCP connect and (for WssSession) the TLS handshake. The DNS lookup has no deadli...