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, 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
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
59private:
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() {
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_));
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 }
105 }
106};
107
108} // namespace network
109} // 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...