FastMM documentation
FastMM C++ API 0.6.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
connection_slot.hpp
Go to the documentation of this file.
1#pragma once
2// ConnectionSlot<Handler>: owns either a plain-TCP or a TLS net::Connection depending on the
3// URL scheme (ws:// for the local simulator, wss:// for the testnets) and forwards the
4// control operations. The stream type is a template parameter of net::Connection, so this
5// is the one place a runtime scheme choice is made; the handler callbacks stay statically
6// dispatched.
8
9#include <memory>
10#include <string_view>
11#include <utility>
12
13namespace fastmm::venues {
14
15template <net::ConnectionHandler Handler>
16class ConnectionSlot {
17 public:
21 ConnectionSlot() = default;
22 ConnectionSlot(const ConnectionSlot&) = delete;
25 // Throws std::invalid_argument on a bad URL (from validate_connection_config).
26 void open(net::Reactor& reactor, net::ConnectionConfig cfg, Handler& handler) {
27 reset();
28 const auto url = net::Url::parse(cfg.url);
29 if (url && url->tls) {
30 tls_ = std::make_unique<Tls>(reactor, std::move(cfg), handler);
31 } else {
32 plain_ = std::make_unique<Plain>(reactor, std::move(cfg), handler);
33 }
34 }
35 // Never call from inside the connection's own callbacks (net contract): the venue posts
36 // the reset to the reactor instead.
37 void reset() noexcept {
38 plain_.reset();
39 tls_.reset();
40 }
41 [[nodiscard]] bool opened() const noexcept { return plain_ || tls_; }
42
43 void connect() {
44 if (plain_) plain_->connect();
45 if (tls_) tls_->connect();
46 }
47 void close() {
48 if (plain_) plain_->close();
49 if (tls_) tls_->close();
50 }
51 bool send_text(std::string_view t) noexcept {
52 if (plain_) return plain_->send_text(t);
53 if (tls_) return tls_->send_text(t);
54 return false;
55 }
56 void cork() noexcept {
57 if (plain_) plain_->cork();
58 if (tls_) tls_->cork();
59 }
60 bool uncork() noexcept {
61 if (plain_) return plain_->uncork();
62 if (tls_) return tls_->uncork();
63 return true;
64 }
65 void auth_done() {
66 if (plain_) plain_->auth_done();
67 if (tls_) tls_->auth_done();
68 }
69 void subscribe_done() {
70 if (plain_) plain_->subscribe_done();
71 if (tls_) tls_->subscribe_done();
72 }
73 [[nodiscard]] net::ConnState state() const noexcept {
74 if (plain_) return plain_->state();
75 if (tls_) return tls_->state();
77 }
78 [[nodiscard]] bool is_live() const noexcept {
79 if (plain_) return plain_->is_live();
80 if (tls_) return tls_->is_live();
81 return false;
82 }
83 [[nodiscard]] net::ConnectionStats stats() const noexcept {
84 if (plain_) return plain_->stats();
85 if (tls_) return tls_->stats();
86 return {};
87 }
88 [[nodiscard]] std::int64_t last_rx_ns() const noexcept {
89 if (plain_) return plain_->last_rx_ns();
90 if (tls_) return tls_->last_rx_ns();
91 return 0;
92 }
93
94 private:
95 std::unique_ptr<Plain> plain_;
96 std::unique_ptr<Tls> tls_;
97};
98
99} // namespace fastmm::venues
Definition connection.hpp:116
Definition reactor.hpp:72
Definition connection_slot.hpp:18
net::ConnectionStats stats() const noexcept
Definition connection_slot.hpp:85
void subscribe_done()
Definition connection_slot.hpp:71
void open(net::Reactor &reactor, net::ConnectionConfig cfg, Handler &handler)
Throws std::invalid_argument on a bad URL (from validate_connection_config).
Definition connection_slot.hpp:28
ConnectionSlot & operator=(const ConnectionSlot &)=delete
bool opened() const noexcept
Definition connection_slot.hpp:43
void connect()
Definition connection_slot.hpp:45
bool uncork() noexcept
Definition connection_slot.hpp:62
bool is_live() const noexcept
Definition connection_slot.hpp:80
void cork() noexcept
Definition connection_slot.hpp:58
void reset() noexcept
Never call from inside the connection's own callbacks (net contract): the venue posts the reset to th...
Definition connection_slot.hpp:39
net::ConnState state() const noexcept
Definition connection_slot.hpp:75
std::int64_t last_rx_ns() const noexcept
Definition connection_slot.hpp:90
void auth_done()
Definition connection_slot.hpp:67
void close()
Definition connection_slot.hpp:49
bool send_text(std::string_view t) noexcept
Definition connection_slot.hpp:53
Resilient WebSocket connection: DNS -> TCP -> TLS -> WS upgrade -> auth -> subscribe -> Live,...
ConnState
Definition connection.hpp:48
Definition venue.hpp:38
Definition connection.hpp:64
std::string url
ws:// or wss://host[:port]/path?query
Definition connection.hpp:65
Definition connection.hpp:102
static std::optional< Url > parse(std::string_view s) noexcept
Accepts scheme://host[:port][/path][?query].
Definition url.hpp:38