Port ws client over from cme-websocket-connector

Need this to implement execution at crypto exchanges (beginning with
Binance and OKX)
This commit is contained in:
John Colvin committed 2024-12-04 16:56:46 +00:00
1 parent 50d26f0060
commit ee4c28ed41
8 files changed
+506

No files matched your search

+10
View File
@@ -53,6 +53,8 @@ constexpr bool operator<(TimeSpan lhs, TimeSpan rhs) noexcept;
constexpr bool operator<=(TimeSpan lhs, TimeSpan rhs) noexcept;
constexpr bool operator>(TimeSpan lhs, TimeSpan rhs) noexcept;
constexpr bool operator>=(TimeSpan lhs, TimeSpan rhs) noexcept;
constexpr TimeSpan operator-(TimeSpan lhs, TimeSpan rhs) noexcept;
constexpr TimeSpan operator+(TimeSpan lhs, TimeSpan rhs) noexcept;
std::ostream &operator<<(std::ostream &out, const TimeSpan &ts) noexcept;
@@ -130,6 +132,14 @@ constexpr bool operator>=(TimeSpan lhs, TimeSpan rhs) noexcept {
return lhs.nanos() >= rhs.nanos();
}
constexpr TimeSpan operator-(TimeSpan lhs, TimeSpan rhs) noexcept {
return TimeSpan{lhs.nanos() - rhs.nanos()};
}
constexpr TimeSpan operator+(TimeSpan lhs, TimeSpan rhs) noexcept {
return TimeSpan{lhs.nanos() + rhs.nanos()};
}
} // namespace bts::core
FMT_IMPL(bts::core::TimeSpan, t, "{}", t.to_iso_string());
+383
View File
@@ -0,0 +1,383 @@
#include "websocket_client.h"
#include "lib/core/TimeSpan.h"
#include "lib/core/TimeStamp.h"
#include "lib/io/websocket/websocket_client_state.h"
#include "lib/io/websocket/websocket_close_status.h"
#include "lib/io/websocket/websocket_error_code.h"
#include "lib/io/websocket/websocketpp_exception_formatter.h"
#include "lib/logging/logging.h"
#include "lib/utils/formatters/exception_formatter.h"
#include <atomic>
#include <exception>
#include <fmt/format.h>
#include <mutex>
#include <nlohmann/json.hpp>
#include <optional>
#include <stdexcept>
#include <string>
#include <thread>
#include <websocketpp/close.hpp>
#include <websocketpp/common/connection_hdl.hpp>
#include <websocketpp/common/memory.hpp>
#include <websocketpp/common/system_error.hpp>
#include <websocketpp/common/thread.hpp>
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/config/core_client.hpp>
#include <websocketpp/roles/client_endpoint.hpp>
namespace bts::websocket {
struct WebsocketClientSnapshot {
int client_id;
WebsocketClientState state;
int version;
};
} // namespace bts::websocket
FMT_IMPL(
bts::websocket::WebsocketClientSnapshot,
t,
"id: {}; state: {}; version: {}",
t.client_id,
t.state,
t.version);
namespace bts::websocket {
using namespace std::chrono_literals;
using bts::core::TimeSpan;
using bts::core::TimeStamp;
using nlohmann::json;
using std::lock_guard;
using std::map;
using std::mutex;
using std::optional;
using std::size_t;
using std::string;
using std::thread;
using std::uint32_t;
using websocketpp::config::asio_tls_client;
using WebsocketppClient =
websocketpp::client<websocketpp::config::asio_tls_client>;
using ConnectionHdl = websocketpp::connection_hdl;
using ConnectionPtr = WebsocketppClient::connection_ptr;
using MessageTypePtr = websocketpp::config::core_client::message_type::ptr;
using SslContext = boost::asio::ssl::context;
using State = WebsocketClientState;
// ws endpoint http://markets.api.uat.cmegroup.com/marketdatastream/v1
#define _ASSERT_MSGF(condition, ...) \
bts_assert_msgf(condition, "WebsocketClient::" __VA_ARGS__)
#define DIAG(...) LOG(Gen, Diag, __VA_ARGS__)
#define DIAG_FN(...) LOG(Gen, Diag, "WebsocketClient::" __VA_ARGS__)
#define INFO(...) LOG(Gen, Info, __VA_ARGS__)
#define INFO_FN(...) LOG(Gen, Info, "WebsocketClient::" __VA_ARGS__)
#define WARN(...) LOG(Gen, Warn, __VA_ARGS__)
#define WARN_FN(...) LOG(Gen, Warn, "WebsocketClient::" __VA_ARGS__)
#define ERROR(...) LOG(Gen, Error, __VA_ARGS__)
#define ERROR_FN(...) LOG(Gen, Error, "WebsocketClient::" __VA_ARGS__)
struct WebsocketClient::PImpl {
string url;
WebsocketppClient client;
uint32_t timeout_secs;
WebsocketppClient::connection_ptr con;
optional<MessageHandler> message_handler;
size_t sub_id_counter{0};
string server_name;
optional<thread> io_thread;
int client_id{client_id_counter++};
mutex instance_mx;
State state;
int version = 0;
~PImpl() {
if (io_thread) {
io_thread->join();
}
}
int set_state(State state) {
if (this->state == state) {
return version;
}
version++;
INFO(
"State changing from {} -> {}. id: {}; version: {}", this->state,
state, client_id, version);
this->state = state;
return version;
}
constexpr WebsocketClientSnapshot snapshot() const {
return {client_id, state, version};
}
void close(websocketpp::close::status::value code, std::string reason) {
lock_guard<mutex> lock{instance_mx};
INFO_FN(
"close. {}; code: {}; reason: {}", snapshot(),
WebsocketCloseStatus{code}, reason);
switch (state.value) {
case State::OPENING:
WARN_FN(
"close. Cannot close while websocket is still opening. {}",
snapshot());
// Wait until we get the 'open' callback, then initiate the close
set_state(State::WAITING_TO_CLOSE);
return;
case State::OPEN:
set_state(State::CLOSING);
break;
default:
return;
}
websocketpp::lib::error_code ec;
client.close(con->get_handle(), code, reason, ec);
if (ec) {
ERROR_FN(
"close. {}; code: {}; reason: {}; error: {}", snapshot(),
WebsocketCloseStatus{code}, reason, ec);
}
}
websocketpp::lib::error_code send_text(const string &text) {
INFO_FN("send_text. {}; text: {}", snapshot(), text);
websocketpp::lib::error_code ec;
client.send(con, text, websocketpp::frame::opcode::value::TEXT, ec);
if (ec) {
ERROR_FN("send_text. {}; text: {}; err: {}", snapshot(), text, ec);
}
return ec;
}
/**
* Connects to the websocket server
*/
void connect(const map<string, string> &headers) {
connect_in_background(headers);
// Wait for connect
if (timeout_secs > 0) {
wait_for_connect(TimeSpan::from_seconds(timeout_secs));
}
}
/**
* Connects to the websocket server in the background.
*
* @note Doesn't block while waiting for the connection to be established
*/
void connect_in_background(const map<string, string> &headers) {
lock_guard<mutex> lock{instance_mx};
INFO_FN("connect_no_timeout. {}", snapshot());
switch (state.value) {
case State::IDLE:
break;
default:
WARN_FN("connect_no_timeout. Unexpected state. {}", snapshot());
return;
}
set_state(State::OPENING);
try {
init_logging();
init_handlers();
// Get connection
websocketpp::lib::error_code ec;
con = client.get_connection(url, ec);
if (ec) {
ERROR_FN("connect_no_timeout. {}; err: {}", snapshot(), ec);
return;
}
// Add headers
for (const auto &[k, v] : headers) {
con->append_header(k, v);
}
// Connect
client.connect(con);
bts_assert_msgf(
!io_thread, "Websocket io_thread already instantiated. {}",
snapshot());
io_thread = std::thread{[this]() {
INFO("Websocket io thread begin. {}", snapshot());
try {
client.run();
} catch (const websocketpp::exception &ex) {
ERROR_FN(
"io_thread. Websocket io threw websocketpp::exception. {}; "
"err: {}",
snapshot(), ex);
} catch (const std::exception &ex) {
ERROR_FN(
"io_thread. Websocket io threw std::exception. {}; err: {}",
snapshot(), ex);
} catch (...) {
bts_assert_msgf(
false,
"io_thread. Websocket io threw unknown exception. Could "
"this have been thrown by a callback delegate? {}",
snapshot());
}
INFO("Websocket 'run' thread end. {}", snapshot());
}};
} catch (const std::exception &ex) {
ERROR_FN(
"connect_no_timeout. Failed to connect. {}; err: {}", snapshot(),
ex);
set_state(State::IDLE);
// Don't wait for connect
return;
}
}
void wait_for_connect(const TimeSpan timeout) {
INFO_FN("wait_for_connect. {}; timeout: {}", snapshot(), timeout);
const auto start = TimeStamp::now();
while (state == State::OPENING) {
const auto elapsed = TimeStamp::now() - start;
const auto rem = timeout - elapsed;
if (rem < TimeSpan::zero()) {
throw std::runtime_error{fmt::format(
"Timed out waiting for connection after {} secs. {}",
elapsed.total_seconds(), snapshot())};
}
WARN_FN(
"wait_for_connect. Still waiting. {}; timeout: {}; rem: {}",
snapshot(), timeout, rem);
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
private:
static std::atomic<int> client_id_counter;
auto on_tls_init(ConnectionHdl /*hdl*/) {
INFO_FN("on_tls_init. state: {}; version: {}", state, version);
auto result =
websocketpp::lib::make_shared<SslContext>(SslContext::sslv23_client);
result->set_verify_mode(boost::asio::ssl::verify_none);
return result;
}
void init_handlers() {
INFO_FN("init_handlers. state: {}; version: {}", state, version);
bts_assert(!io_thread);
client.init_asio();
client.set_message_handler(
[this](auto hdl, auto msg) { on_message(hdl, msg); });
client.set_open_handler([this](auto hdl) { on_open(hdl); });
client.set_tls_init_handler(
[this](auto hdl) { return on_tls_init(hdl); });
client.set_close_handler([this](auto hdl) { on_close(hdl); });
client.set_fail_handler([this](auto hdl) {
const auto ec = client.get_con_from_hdl(hdl)->get_ec();
ERROR_FN("fail_handler. {}; err: {}", snapshot(), ec.message());
});
}
void init_logging() {
INFO_FN("init_logging. {}", snapshot());
client.set_access_channels(websocketpp::log::alevel::all);
client.clear_access_channels(websocketpp::log::alevel::frame_payload);
client.set_error_channels(websocketpp::log::elevel::all);
}
void on_open(ConnectionHdl hdl) {
lock_guard<mutex> lock{instance_mx};
INFO_FN("on_open. {}", snapshot());
const auto ec = client.get_con_from_hdl(hdl)->get_ec();
if (ec) {
ERROR_FN("on_open. {}; err: {}", snapshot(), ec.message());
}
switch (state.value) {
case State::OPENING:
break;
default:
WARN_FN("on_open. Unexpected state. {}", snapshot());
return;
}
set_state(State::OPEN);
}
void on_message(ConnectionHdl /*hdl*/, MessageTypePtr msg) {
DIAG("on_message. {}", snapshot());
const auto &payload = msg->get_payload();
if (message_handler) {
(*message_handler)(payload);
return;
}
ERROR_FN(
"on_message. Message handler is null. {}; payload: \n{}", snapshot(),
payload);
}
void on_close(ConnectionHdl hdl) {
lock_guard<mutex> lock{instance_mx};
INFO_FN("on_close. {}", snapshot());
switch (state.value) {
case State::CLOSING:
break;
default:
WARN_FN("on_close. Unexpected state. {}", snapshot());
break;
}
set_state(State::IDLE);
const auto con = client.get_con_from_hdl(hdl);
const auto ec = con->get_ec();
if (ec) {
ERROR_FN(
"on_close. {}; local_code: {}; "
"local_reason: {}; remote_code: {}; remote_reason: {}; err: {}",
snapshot(), con->get_local_close_code(),
con->get_local_close_reason(), con->get_remote_close_code(),
con->get_remote_close_reason(), ec.message());
}
}
};
WebsocketClient::WebsocketClient(std::string url, std::uint32_t timeout_secs)
: me_(std::make_unique<PImpl>()) {
me_->url = std::move(url);
me_->timeout_secs = timeout_secs;
}
WebsocketClient::~WebsocketClient() {
me_->close(websocketpp::close::status::going_away, "");
}
std::atomic<int> WebsocketClient::PImpl::client_id_counter = 0;
WebsocketClient::MessageHandler &WebsocketClient::message_handler() {
if (!me_->message_handler) {
me_->message_handler.emplace();
}
return *me_->message_handler;
}
void WebsocketClient::connect(
const std::map<std::string, std::string> &headers) {
me_->connect(headers);
}
websocketpp::lib::error_code WebsocketClient::send_text(const string &text) {
return me_->send_text(text);
}
void WebsocketClient::disconnect() {
me_->close(
websocketpp::close::status::going_away,
"WebsocketClient::disconnect was called");
}
bool WebsocketClient::is_connected() const {
lock_guard<mutex> lock{me_->instance_mx};
return me_->state == State::OPEN;
}
} // namespace bts::websocket
+32
View File
@@ -0,0 +1,32 @@
#pragma once
#include "lib/utils/Delegate.h"
#include <memory>
#include <string>
#include <websocketpp/config/core_client.hpp>
#include <websocketpp/message_buffer/message.hpp>
namespace bts::websocket {
class WebsocketClientConfig;
class WebsocketClient {
public:
WebsocketClient(std::string url, std::uint32_t timeout_secs = 20);
~WebsocketClient();
using MessageHandler = bts::core::Delegate<void(const std::string &)>;
void connect(const std::map<std::string, std::string> &headers = {});
void disconnect();
bool is_connected() const;
MessageHandler &message_handler();
websocketpp::lib::error_code send_text(const std::string &text);
private:
struct PImpl;
std::unique_ptr<PImpl> me_;
};
} // namespace bts::websocket
@@ -0,0 +1,25 @@
#pragma once
#include "lib/core/EnumMacros.h"
#include "lib/core/FMT_IMPL.h"
#include <nlohmann/json.hpp>
namespace bts::websocket {
// clang-format off
#define FOR_ALL_VALUES(op) \
op(WebsocketClientState, IDLE) \
op(WebsocketClientState, OPENING) \
op(WebsocketClientState, OPEN) \
op(WebsocketClientState, WAITING_TO_CLOSE) \
op(WebsocketClientState, CLOSING)
// clang-format on
ENUM_CLASS_DEF(WebsocketClientState, FOR_ALL_VALUES);
#undef FOR_ALL_VALUES
} // namespace bts::websocket
FMT_IMPL(bts::websocket::WebsocketClientState, s, "{}", s.name());
@@ -0,0 +1,24 @@
#pragma once
#include "lib/core/FMT_IMPL.h"
#include "lib/utils/ValueWrapper.h"
#include <websocketpp/close.hpp>
namespace bts::websocket {
/**
* Simple wrapper to provide a means implement a custom formatter for
* `websocketpp::close::status::value` (an alias for a primitive numeric type)
*/
using WebsocketCloseStatus = utils::ValueWrapper<
struct WebsocketCloseStatusTag,
websocketpp::close::status::value>;
} // namespace bts::websocket
FMT_IMPL(
bts::websocket::WebsocketCloseStatus,
s,
"{}",
websocketpp::close::status::get_string(*s));
@@ -0,0 +1,7 @@
#pragma once
#include "lib/core/FMT_IMPL.h"
#include <websocketpp/common/system_error.hpp>
FMT_IMPL(websocketpp::lib::error_code, ec, "{}", ec.message());
@@ -0,0 +1,7 @@
#pragma once
#include "lib/core/FMT_IMPL.h"
#include <websocketpp/error.hpp>
FMT_IMPL(websocketpp::exception, e, "{}", std::string{e.what()});
@@ -0,0 +1,18 @@
#pragma once
#include <exception>
#include <fmt/format.h>
#include <string>
template <>
struct fmt::formatter<std::exception> {
constexpr auto parse(fmt::format_parse_context &ctx) -> decltype(ctx.end()) {
return ctx.end();
}
template <typename FormatContext>
auto format(const std::exception &e, FormatContext &ctx) const
-> decltype(ctx.out()) {
return fmt ::format_to(ctx.out(), "{}", std ::string{e.what()});
}
};