pretty damn good, really

This commit is contained in:
John Colvin committed 2023-11-03 23:12:58 -04:00
1 parent f18c952ab8
commit addff775e9
76 files changed
+2693 -1438

No files matched your search

+2 -3
View File
@@ -113,10 +113,9 @@ else()
# subdirs
if (BUILD_SPARKD)
add_subdirectory(src/lib_bts-system)
add_subdirectory(src/lib_bts-json)
add_subdirectory(src/lib_bts-http)
add_subdirectory(src/lib_bts-http-client)
add_subdirectory(src/app_bts-websocket-listener)
add_subdirectory(src/lib_bts-websocket)
add_subdirectory(src/app_bts-cme-websocket-listener)
add_subdirectory(src/server/sparkd)
add_subdirectory(src/data_files)
endif()
@@ -1,18 +1,5 @@
cmake_minimum_required(VERSION 3.23)
project(bts-websocket-listener)
FetchContent_MakeAvailable(curlpp)
FetchContent_GetProperties(
curlpp
SOURCE_DIR CURLPP_SOURCE_DIR
BINARY_DIR CURLPP_BINARY_DIR)
message("CMAKE_BUILD_RPATH before: ${CMAKE_BUILD_RPATH}")
message("CMAKE_INSTALL_RPATH before: ${CMAKE_INSTALL_RPATH}")
list(APPEND CMAKE_BUILD_RPATH ${curlpp_BINARY_DIR})
# set(CMAKE_BUILD_RPATH "${CMAKE_BUILD_RPATH};${curlpp_BINARY_DIR}")
message("CMAKE_BUILD_RPATH after : ${CMAKE_BUILD_RPATH}")
message("CMAKE_INSTALL_RPATH after : ${CMAKE_INSTALL_RPATH}")
project(bts-cme-websocket-listener)
file(
GLOB_RECURSE ${PROJECT_NAME}_Sources
@@ -20,9 +7,7 @@ file(
${PROJECT_NAME}/*.cpp)
add_executable(${PROJECT_NAME} ${${PROJECT_NAME}_Sources})
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(${PROJECT_NAME} PUBLIC bts-system bts-http-client curl)
target_link_libraries(${PROJECT_NAME} PUBLIC crypto curlpp ssl)
target_link_libraries(${PROJECT_NAME} PUBLIC bts-system bts-http bts-websocket)
# # cli11
# FetchContent_Declare(
@@ -0,0 +1,30 @@
#pragma once
#include "bts-cme-websocket-listener/messages/cme_message_header.h"
#include <nlohmann/detail/macro_scope.hpp>
#include <nlohmann/json.hpp>
namespace bts::websocket {
template <class Payload = nlohmann::json>
class CmeMessageT {
public:
CmeMessageHeader header;
Payload payload;
};
using CmeMessage = CmeMessageT<nlohmann::json>;
template <class Payload>
inline void to_json(nlohmann::json &j, const CmeMessageT<Payload> &msg) {
j["header"] = msg.header;
j["payload"] = msg.payload;
}
template <class Payload>
inline void from_json(const nlohmann::json &j, CmeMessageT<Payload> &msg) {
j.at("header").get_to(msg.header);
j.at("payload").get_to(msg.payload);
};
} // namespace bts::websocket
@@ -0,0 +1,91 @@
#include "cme_message_demuxer.h"
#include "bts-cme-websocket-listener/cme_message.h"
#include "bts-cme-websocket-listener/cme_message_handler.h"
#include "bts-cme-websocket-listener/messages/cme_message_header.h"
#include "bts-cme-websocket-listener/messages/top_of_book.h"
#include "bts-cme-websocket-listener/messages/trade_summary_payload.h"
#include "bts-system/json/json_dumper.h"
#include "bts-system/json/json_reader.h"
#include <string>
#include <unordered_map>
#include <websocketpp/common/connection_hdl.hpp>
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/roles/client_endpoint.hpp>
namespace bts::websocket {
using nlohmann::json;
using std::string;
using std::vector;
#define _ASSERT_MSGF(condition, ...) \
bts_assert_msgf(condition, "CmeMessageDemuxer::" __VA_ARGS__)
#define _LOG(level, ...) LOG(md, level, "CmeMessageDemuxer::" __VA_ARGS__)
#define _DIAG(...) _LOG(Diag, __VA_ARGS__)
#define _INFO(...) _LOG(Info, __VA_ARGS__)
#define _WARN(...) _LOG(Warn, __VA_ARGS__)
#define _ERROR(...) _LOG(Error, __VA_ARGS__)
struct CmeMessageDemuxer_Handler {
vector<CmeMessage> queue;
CmeMessageDemuxer::Callback callback;
CmeMessageHandler *handler;
void flush() {
if (!queue.empty()) {
callback(queue);
queue.clear();
}
}
};
struct CmeMessageDemuxer::PImpl {
std::unordered_map<string, CmeMessageDemuxer_Handler> handlers;
};
CmeMessageDemuxer::CmeMessageDemuxer() : me_(std::make_unique<PImpl>()) {}
CmeMessageDemuxer::~CmeMessageDemuxer() = default;
CmeMessageDemuxer::Callback &CmeMessageDemuxer::add_handler(
const std::string &msg_type) {
auto [it, is_new] = me_->handlers.try_emplace(msg_type);
if (!is_new) {
throw std::runtime_error{fmt::format(
"add_handler. Already added handler for message type '{}'",
msg_type)};
}
return it->second.callback;
}
void CmeMessageDemuxer::remove_handler(const string &msg_type) {
me_->handlers.erase(msg_type);
}
void CmeMessageDemuxer::add_handler(const string &msg_type, CmeMessageHandler* handler) {
auto [it, is_new] = me_->handlers.try_emplace(msg_type);
if (!is_new) {
throw std::runtime_error{fmt::format(
"add_handler. Already added handler for message type '{}'",
msg_type)};
}
it->second.handler = handler;
}
void CmeMessageDemuxer::on_message(const string &msg_str) {
_INFO("on_message\n{}", msg_str);
CmeMessage msg;
try {
nlohmann::json::parse(msg_str).get_to(msg);
} catch (const std::exception &ex) {
_ERROR("on_message. Failed to parse json. {}\n{}", ex.what(), msg_str);
return;
}
// Pass msg to handler for this msg type
const auto hnd = me_->handlers.find(msg.header.messageType);
if (hnd != me_->handlers.end()) {
hnd->second.queue.push_back(msg);
}
}
} // namespace bts::websocket
@@ -0,0 +1,45 @@
#pragma once
#include "bts-cme-websocket-listener/cme_message.h"
#include "bts-system/delegate.h"
#include <cstdlib>
#include <memory>
#include <nlohmann/json.hpp>
#include <vector>
namespace bts::websocket {
class CmeMessageHandler;
class CmeMessageDemuxer {
public:
CmeMessageDemuxer();
~CmeMessageDemuxer();
using Callback =
bts::system::Delegate<void(const std::vector<CmeMessage> &)>;
void on_message(const std::string &msg);
/**
* Adds a handler for the specified \p msg_type
*
* @return A bindable delegate, which will be invoked with messages of the
* specified \p msg_type
*/
Callback &add_handler(const std::string &msg_type);
void add_handler(const std::string &msg_type, CmeMessageHandler *handler);
/**
* @brief Removes the handler for the specified \p msg_type
*/
void remove_handler(const std::string &msg_type);
private:
struct PImpl;
std::unique_ptr<PImpl> me_;
};
} // namespace bts::websocket
@@ -0,0 +1,22 @@
#pragma once
#include "bts-cme-websocket-listener/cme_message.h"
#include "bts-cme-websocket-listener/messages/top_of_book.h"
#include "bts-cme-websocket-listener/messages/trade_summary_message.h"
#include <string>
namespace bts::websocket {
class CmeMessageHandler {
public:
virtual void on_messages(const std::vector<CmeMessage> &payload);
};
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline void CmeMessageHandler::on_messages(const std::vector<CmeMessage> &) {}
} // namespace bts::websocket
@@ -0,0 +1,20 @@
#pragma once
#include "bts-cme-websocket-listener/cme_message.h"
#include "bts-cme-websocket-listener/cme_message_handler.h"
#include "bts-system/json/json_dumper.h"
#include <nlohmann/json.hpp>
#include <vector>
namespace bts::websocket {
struct CmeMessageLogger : public bts::websocket::CmeMessageHandler {
void on_messages(const std::vector<CmeMessage> &msgs) override {
nlohmann::json wrapped = msgs;
LOG(Md, Info, "CmeMessageLogger::on_messages.\n{}",
bts::system::json::JsonDumper::dump(wrapped));
}
};
} // namespace bts::websocket
@@ -0,0 +1,10 @@
#pragma once
#include "bts-system/value_wrapper.h"
namespace bts::websocket {
using CmeSubscriptionId =
system::ValueWrapper<struct SubscriptionTag, std::uint32_t>;
} // namespace bts::websocket
@@ -0,0 +1,210 @@
#include "cme_websocket_session.h"
#include "bts-cme-websocket-listener/cme_message.h"
#include "bts-cme-websocket-listener/cme_message_demuxer.h"
#include "bts-cme-websocket-listener/cme_message_handler.h"
#include "bts-cme-websocket-listener/cme_subscription_id.h"
#include "bts-cme-websocket-listener/cme_websocket_session_config.h"
#include "bts-cme-websocket-listener/messages/channel.h"
#include "bts-cme-websocket-listener/messages/cme_message_header.h"
#include "bts-cme-websocket-listener/messages/cme_subscription.h"
#include "bts-cme-websocket-listener/messages/cme_subscription_payload.h"
#include "bts-cme-websocket-listener/messages/top_of_book.h"
#include "bts-cme-websocket-listener/messages/trade_summary_message.h"
#include "bts-cme-websocket-listener/messages/trade_summary_payload.h"
#include "bts-cme-websocket-listener/ssl_certificate_verifier.h"
#include "bts-cme-websocket-listener/tls_initializer.h"
#include "bts-http/QueryParams.h"
#include "bts-http/URL.h"
#include "bts-http/curl_requestor.h"
#include "bts-http/http_client.h"
#include "bts-http/http_request.h"
#include "bts-http/http_response.h"
#include "bts-http/oauth_request.h"
#include "bts-http/oauth_requestor.h"
#include "bts-http/oauth_response.h"
#include "bts-system/TimeSpan.h"
#include "bts-system/TimeStamp.h"
#include "bts-system/UNUSED.h"
#include "bts-system/bts_assert.h"
#include "bts-system/json/json_dumper.h"
#include "bts-system/json/json_reader.h"
#include "bts-system/overloaded.h"
#include "bts-websocket/websocket_client.h"
#include <atomic>
#include <curl/curl.h>
#include <curl/easy.h>
#include <fmt/format.h>
#include <exception>
#include <functional>
#include <optional>
#include <stdexcept>
#include <string>
#include <thread>
#include <type_traits>
#include <unordered_map>
#include <variant>
#include <vector>
#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 {
using namespace std::chrono_literals;
using http::OauthRequestor;
using nlohmann::json;
using std::optional;
using std::string;
using std::unordered_map;
using std::vector;
using system::json::JsonDumper;
using system::json::JsonReader;
using websocketpp::config::asio_tls_client;
// using WebsocketClient =
// websocketpp::client<websocketpp::config::asio_tls_client>;
// using ConnectionHdl = websocketpp::connection_hdl;
// using ConnectionPtr = websocketpp::client<websocketpp::config::asio_tls_client>::connection_ptr;
using MessageTypePtr = websocketpp::config::core_client::message_type::ptr;
using SslContext = boost::asio::ssl::context;
// ws endpoint http://markets.api.uat.cmegroup.com/marketdatastream/v1
#define _ASSERT_MSGF(condition, ...) \
bts_assert_msgf(condition, "CmeMessageDemuxer::" __VA_ARGS__)
#define _LOG(level, ...) LOG(md, level, "CmeMessageDemuxer::" __VA_ARGS__)
#define _DIAG(...) _LOG(Diag, __VA_ARGS__)
#define _INFO(...) _LOG(Info, __VA_ARGS__)
#define _WARN(...) _LOG(Warn, __VA_ARGS__)
#define _ERROR(...) _LOG(Error, __VA_ARGS__)
struct CmeWebsocketSession::PImpl {
PImpl(const CmeWebsocketSessionConfig &cfg)
: cfg(cfg), client(cfg.websocket) {}
CmeWebsocketSessionConfig cfg;
WebsocketClient client;
CmeMessageDemuxer demuxer;
CmeSubscriptionId sub_id_counter{0};
string server_name;
std::optional<std::thread> run_thread;
int session_id{session_id_counter++};
bool client_is_open = false;
~PImpl() {
if (run_thread) {
run_thread->join();
}
}
void on_login_status(const vector<CmeMessage> &msgs) {
// If there is an error, log it and return.
if (msgs.size() != 1) {
_ERROR(
"on_login_status. Expected exactly 1 message in json payload:\n{}",
JsonDumper::dump(json{msgs}));
return;
}
auto &msg = msgs.at(0);
_INFO("on_login_status:\n{}", JsonDumper::dump(msg));
if (msg.payload.at("status").get<string>() != "authenticated") {
_ERROR(
"on_login_status. status not authenticated:\n{}",
JsonDumper::dump(json{msgs}));
return;
}
}
void on_subscription_status(const vector<json> &msgs) {
// If there is an error, log it and return.
#define __ERROR \
_ERROR( \
"on_subscription_status. Unexpected response:\n{}", \
JsonDumper::dump(json{msgs})); \
return
if (msgs.size() != 1) {
__ERROR;
}
auto &msg = msgs.at(0);
_INFO("on_subscription_status:\n{}", JsonDumper::dump(msg));
JSON_READ_ELSE(msg, payload, json, __ERROR);
JSON_READ_ELSE(payload, status, string, __ERROR);
if (status != "authenticated") {
__ERROR;
}
#undef __ERROR
}
void on_ts(const vector<CmeMessage> &msgs) {
// If there is an error, log it and return.
#define __ERROR \
_ERROR("on_ts. Unexpected response:\n{}", JsonDumper::dump(json{msgs})); \
return
if (msgs.size() != 1) {
__ERROR;
}
auto &msg = msgs.at(0);
_INFO("on_ts:\n{}", JsonDumper::dump(msg));
// JSON_READ_ELSE(msg, payload, json, __ERROR);
// JSON_READ_ELSE(payload, status, string, __ERROR);
// if (status != "authenticated") {
// __ERROR;
// }
#undef __ERROR
}
void on_message(const string &payload) { demuxer.on_message(payload); }
private:
static std::atomic_int session_id_counter;
};
CmeWebsocketSession::CmeWebsocketSession(CmeWebsocketSessionConfig cfg)
: me_(std::make_unique<PImpl>(std::move(cfg))) {
// Incoming messages are demuxed per 'messageType' (a json field assumed to
// be present in every message).
// Bind the demuxer directly to the websocket client (so it will receive raw
// messages from the server)
me_->client.message_handler().bind<&CmeMessageDemuxer::on_message>(
me_->demuxer);
// Bind 'messageType' handlers to the demuxer
me_->demuxer.add_handler("loginStatus").bind<&PImpl::on_login_status>(*me_);
me_->demuxer.add_handler("subscriptionStatus")
.bind<&PImpl::on_login_status>(*me_);
me_->demuxer.add_handler("TS").bind<&PImpl::on_ts>(*me_);
}
CmeWebsocketSession::~CmeWebsocketSession() = default;
void CmeWebsocketSession::start() { me_->client.connect(); }
void CmeWebsocketSession::subscribe(
const std::vector<Channel> &channels,
const std::vector<std::string> &data_types,
CmeMessageHandler *handler) {
CmeSubscriptionId sub_id{(*me_->sub_id_counter)++};
CmeSubscription sub{
CmeMessageHeader{"subscribe", std::to_string(sub_id.value())},
CmeSubscriptionPayload{data_types, channels}};
for (const auto &dt : data_types) {
me_->demuxer.add_handler(dt).bind<&CmeMessageHandler::on_messages>(
*handler);
}
me_->client.send_text(JsonDumper::dump(sub));
}
std::atomic_int CmeWebsocketSession::PImpl::session_id_counter = 0;
} // namespace bts::websocket
@@ -0,0 +1,49 @@
#pragma once
#include "bts-cme-websocket-listener/cme_message_handler.h"
#include "bts-cme-websocket-listener/cme_subscription_id.h"
#include "bts-cme-websocket-listener/messages/channel.h"
#include "bts-http/URL.h"
#include "bts-system/delegate.h"
#include "bts-system/value_wrapper.h"
#include <cstddef>
#include <curl/curl.h>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <iostream>
#include <memory>
#include <nlohmann/json.hpp>
#include <string>
#include <vector>
namespace bts::websocket {
class CmeOauthConfig;
class CmeWebsocketSessionConfig;
class CmeWebsocketSession {
public:
explicit CmeWebsocketSession(CmeWebsocketSessionConfig cfg);
~CmeWebsocketSession();
void start();
/**
* @brief Subscribes to the specified channel specified \p channel
* @return A bindable delegate, which will be invoked with data associated
* with the specified \p channel
*/
void subscribe(
const std::vector<Channel> &channels,
const std::vector<std::string> &data_types,
CmeMessageHandler *handler);
private:
struct PImpl;
std::unique_ptr<PImpl> me_;
};
} // namespace bts::websocket
@@ -0,0 +1,38 @@
#pragma once
#include "bts-cme-websocket-listener/messages/cme_subscription_payload.h"
#include "bts-http/URL.h"
#include "bts-http/oauth_request.h"
#include "bts-system/json/json_reader.h"
#include "bts-system/threading/thread_config.h"
#include "bts-websocket/websocket_client_config.h"
#include <nlohmann/json.hpp>
#include <string>
#include <vector>
namespace bts::websocket {
class CmeWebsocketSessionConfig {
public:
// Needed to obtain the access token (ignored if access token is non-empty)
websocket::WebsocketClientConfig websocket;
std::vector<CmeSubscriptionPayload> subscription_payloads;
};
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline void to_json(nlohmann::json &j, const CmeWebsocketSessionConfig &cfg) {
j["websocket"] = cfg.websocket;
j["subscription_payloads"] = cfg.subscription_payloads;
}
inline void from_json(const nlohmann::json &j, CmeWebsocketSessionConfig &cfg) {
system::json::JsonReader jr{&j};
jr.read("websocket", cfg.websocket);
jr.read("subscription_payloads", cfg.subscription_payloads);
};
} // namespace bts::websocket
@@ -0,0 +1,66 @@
#include "bts-cme-websocket-listener/cme_message_handler.h"
#include "bts-cme-websocket-listener/cme_websocket_session.h"
#include "bts-cme-websocket-listener/cme_websocket_session_config.h"
#include "bts-cme-websocket-listener/messages/channel.h"
#include "bts-system/UNUSED.h"
#include "bts-system/json/json_dumper.h"
#include "bts-system/json/json_reader.h"
#include "bts-websocket/websocket_client.h"
#include "bts-cme-websocket-listener/cme_message_logger.h"
#include <CLI11/CLI11.hpp>
#include <boost/asio.hpp>
#include <boost/beast.hpp>
#include <curlpp/cURLpp.hpp>
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
#include <nlohmann/json_fwd.hpp>
#include <string>
#include <vector>
using bts::system::json::JsonReader;
using bts::websocket::CmeWebsocketSessionConfig;
using CLI::App;
using std::ifstream;
using std::string;
static CmeWebsocketSessionConfig parse_config(const string &config_arg) {
ifstream cfg_ifs{config_arg};
const auto cfg_j = nlohmann::json::parse(cfg_ifs);
JsonReader cfg_jr{&cfg_j};
CmeWebsocketSessionConfig cfg;
bts_assert(cfg_jr.read(cfg));
cfg.websocket.timeout = bts::core::TimeSpan::from_seconds(20);
return cfg;
}
static void run(const std::string &config_arg) {
const auto cfg = parse_config(config_arg);
struct CurlInit {
CurlInit() { curlpp::initialize(); }
~CurlInit() { curlpp::terminate(); }
} _CURL_INIT;
bts::websocket::CmeMessageLogger cme_handler;
bts::websocket::CmeWebsocketSession session{cfg};
session.start();
session.subscribe(
std::vector<bts::websocket::Channel>{{"XCME", "FUT", "BTC"}},
std::vector<std::string>{"TRD"}, &cme_handler);
static const int sleep_secs = 3;
while (true) {
std::this_thread::sleep_for(std::chrono::seconds(sleep_secs));
}
}
int main(int argc, char **argv) {
App app{"Cme websocket listener", "bts-websocket-listener"};
string config_arg;
app.add_option(
"--config", config_arg, "Path to CmeWebsocketConfig json file")
->required();
app.callback([&config_arg]() { run(config_arg); });
CLI11_PARSE(app, argc, argv);
}
@@ -0,0 +1,18 @@
#pragma once
#include <nlohmann/json.hpp>
#include <string>
namespace bts::websocket {
class Channel {
public:
std::string exchangeMic;
std::string productType;
std::string productCode;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
Channel, exchangeMic, productType, productCode);
} // namespace bts::websocket
@@ -0,0 +1,33 @@
#pragma once
#include "bts-system/TimeStamp.h"
#include <nlohmann/detail/macro_scope.hpp>
#include <nlohmann/json.hpp>
namespace bts::websocket {
class CmeBookLevel {
public:
core::TimeStamp lastUpdateTime;
std::uint32_t orderCnt;
double price;
double qty;
};
inline void to_json(nlohmann ::json &j, const CmeBookLevel &l) {
j["lastUpdateTime"] = l.lastUpdateTime.to_iso_string();
j["orderCnt"] = l.orderCnt;
j["price"] = l.price;
j["qty"] = l.qty;
}
inline void from_json(const nlohmann ::json &j, CmeBookLevel &l) {
l.lastUpdateTime = core::TimeStamp::from_iso_string(
j.at("lastUpdateTime").get<std::string>());
j.at("orderCnt").get_to(l.orderCnt);
j.at("price").get_to(l.price);
j.at("qty").get_to(l.qty);
};
} // namespace bts::websocket
@@ -0,0 +1,90 @@
#pragma once
#include "bts-system/TimeStamp.h"
#include <nlohmann/json.hpp>
#include <optional>
#include <string>
namespace bts::websocket {
class CmeInstrument {
public:
std::string guid;
std::int64_t guidInt;
std::int64_t productGuidInt;
std::string instrumentName;
std::optional<std::string> globexSecurityId;
std::string globexSymbol;
core::TimeStamp positionRemovalDate;
core::TimeStamp lastUpdated;
core::TimeStamp lastTradeDate;
std::optional<core::TimeStamp> lastNoticeDate;
core::TimeStamp lastDeliveryDate;
std::optional<core::TimeStamp> globexLastTradeDate;
std::optional<core::TimeStamp> globexFirstTradeDate;
core::TimeStamp firstTradeDate;
std::optional<core::TimeStamp> firstNoticeDate;
core::TimeStamp firstDeliveryDate;
core::TimeStamp finalSettlementDate;
std::optional<core::TimeStamp> initialInventoryDueDate;
std::optional<core::TimeStamp> lastInventoryDueDate;
std::optional<core::TimeStamp> lastEfpDate;
std::optional<double> strikePxCcy;
std::optional<std::string> putCallIndicator;
std::optional<double> priceBandD1;
std::optional<double> priceBand;
std::optional<std::string> zeroPriceEligible;
std::optional<double> originalContractSize;
core::TimeStamp contractMonth;
std::string flexIndicator;
// TODO: like 40 more fields :()!
template <class T>
static void write_opt(
const std::optional<T> &dest,
nlohmann::json &j,
const std::string &field) {
if (dest) {
j.at(field) = *dest;
}
}
template <class T>
static void read_opt(
nlohmann::json &j, const std::string &field, std::optional<T> &dest) {
auto it = j.find(field);
if (it != j.end()) {
it->get_to(dest.emplace());
}
}
};
inline void to_json(nlohmann ::json &j, const CmeInstrument &i) {
j["guid"] = i.guid;
j["guidInt"] = i.guidInt;
j["productGuidInt"] = i.productGuidInt;
j["instrumentName"] = i.instrumentName;
if (i.globexSecurityId) {
j["globexSecurityId"] = *i.globexSecurityId;
}
j["globexSymbol"] = i.globexSymbol;
if (i.positionRemovalDate) {
// TODO
// j["positionRemovalDate"] = {};
}
j["lastUpdated"] = i.lastUpdated;
j["lastTradeDate"] = i.lastTradeDate;
}
inline void from_json(const nlohmann ::json &j, CmeInstrument &i) {
j.at("guid").get_to(i.guid);
j.at("guidInt").get_to(i.guidInt);
j.at("productGuidInt").get_to(i.productGuidInt);
j.at("instrumentName").get_to(i.instrumentName);
j.at("globexSecurityId").get_to(i.globexSecurityId);
j.at("globexSymbol").get_to(i.globexSymbol);
j.at("positionRemovalDate").get_to(i.positionRemovalDate);
j.at("lastUpdated").get_to(i.lastUpdated);
j.at("lastTradeDate").get_to(i.lastTradeDate);
};
} // namespace bts::websocket
@@ -0,0 +1,33 @@
#pragma once
#include <nlohmann/json.hpp>
#include <string>
namespace bts::websocket {
class CmeInstrumentMetadata {
public:
std::string definitionSource;
std::string exchangeMic;
std::int64_t id;
std::uint32_t marketSegmentId;
std::uint32_t periodCode;
std::string productCode;
std::string productType;
std::string productGroup;
std::string symbol;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
CmeInstrumentMetadata,
definitionSource,
exchangeMic,
id,
marketSegmentId,
periodCode,
productCode,
productType,
productGroup,
symbol);
} // namespace bts::websocket
@@ -0,0 +1,53 @@
#pragma once
#include "bts-system/TimeStamp.h"
#include "bts-system/json/json_reader.h"
#include "bts-cme-websocket-listener/cme_subscription_id.h"
#include <nlohmann/json.hpp>
#include <nlohmann/json_fwd.hpp>
#include <optional>
#include <string>
namespace bts::websocket {
class CmeMessageHeader {
public:
std::string messageType{};
std::optional<std::string> requestId{};
std::optional<core::TimeStamp> sentTime{};
std::string token{};
std::optional<std::string> version{};
};
inline void to_json(nlohmann ::json &j, const CmeMessageHeader &h) {
j["messageType"] = h.messageType;
if (h.sentTime) {
j["sentTime"] = h.sentTime->to_iso_string();
}
if (h.version) {
j["version"] = *h.version;
}
if (h.requestId) {
j["requestId"] = *h.requestId;
}
}
inline void from_json(const nlohmann ::json &j, CmeMessageHeader &h) {
j.at("messageType").get_to(h.messageType);
std::string sentTime;
h.sentTime =
system::json::JsonReader{&j}.read("sentTime", sentTime)
? decltype(h.sentTime){core::TimeStamp::from_iso_string(sentTime)}
: std::nullopt;
std::string version;
h.version = system::json::JsonReader{&j}.read("version", version)
? decltype(h.version){version}
: std::nullopt;
std::string requestId;
h.requestId = system::json::JsonReader{&j}.read("requestId", requestId)
? decltype(h.requestId){requestId}
: std::nullopt;
};
} // namespace bts::websocket
@@ -0,0 +1,17 @@
#pragma once
#include "bts-cme-websocket-listener/messages/cme_subscription_payload.h"
#include "bts-cme-websocket-listener/messages/cme_message_header.h"
#include <nlohmann/json.hpp>
namespace bts::websocket {
class CmeSubscription {
public:
CmeMessageHeader header{};
CmeSubscriptionPayload payload{};
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(CmeSubscription, header, payload);
} // namespace bts::websocket
@@ -0,0 +1,20 @@
#pragma once
#include <nlohmann/json.hpp>
#include <string>
#include <vector>
#include "bts-cme-websocket-listener/messages/channel.h"
namespace bts::websocket {
class CmeSubscriptionPayload {
public:
std::vector<std::string> subscriptionMessageTypes;
std::vector<Channel> subscriptions;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
CmeSubscriptionPayload, subscriptionMessageTypes, subscriptions);
} // namespace bts::websocket
@@ -0,0 +1,22 @@
#pragma once
#include "bts-cme-websocket-listener/messages/cme_book_level.h"
#include "bts-cme-websocket-listener/messages/cme_instrument_metadata.h"
#include <nlohmann/detail/macro_scope.hpp>
#include <string>
#include <vector>
namespace bts::websocket {
class TopOfBookPayload {
public:
std::string tradingStatus;
CmeInstrumentMetadata instrument;
std::vector<CmeBookLevel> askLevel;
std::vector<CmeBookLevel> bidLevel;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
TopOfBookPayload, tradingStatus, instrument, askLevel, bidLevel);
} // namespace bts::websocket
@@ -1,7 +1,6 @@
#pragma once
#include <cstdint>
#include <nlohmann/detail/macro_scope.hpp>
#include <nlohmann/json.hpp>
#include <string>
@@ -17,7 +16,13 @@ class TradeSummary {
std::string tradeUpdateAction;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(TradeSummary, aggressorSide, mdTradeEntryId, tradePrice, tradeQty, tradeOrderCount,
tradeUpdateAction)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
TradeSummary,
aggressorSide,
mdTradeEntryId,
tradePrice,
tradeQty,
tradeOrderCount,
tradeUpdateAction)
} // namespace bts::websocket
} // namespace bts::websocket
@@ -0,0 +1,19 @@
#pragma once
#include "bts-cme-websocket-listener/messages/cme_message_header.h"
#include "bts-cme-websocket-listener/messages/trade_summary_payload.h"
#include <nlohmann/json.hpp>
#include <string>
namespace bts::websocket {
class TradeSummaryMessage {
public:
CmeMessageHeader header;
TradeSummaryPayload payload;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(TradeSummaryMessage, header, payload)
} // namespace bts::websocket
@@ -0,0 +1,19 @@
#pragma once
#include "bts-cme-websocket-listener/messages/trade_summary.h"
#include <nlohmann/json.hpp>
#include <string>
namespace bts::websocket {
class TradeSummaryPayload {
public:
std::int64_t lastUpdateTime;
TradeSummary tradeSummary;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
TradeSummaryPayload, lastUpdateTime, tradeSummary)
} // namespace bts::websocket
@@ -1,5 +1,5 @@
#include "tls_initializer.h"
#include "bts-websocket-listener/ssl_certificate_verifier.h"
#include "bts-cme-websocket-listener/ssl_certificate_verifier.h"
#include <websocketpp/common/connection_hdl.hpp>
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/roles/client_endpoint.hpp>
@@ -11,5 +11,19 @@
"endpoint": "as/token.oauth2",
"client_id": "api_thinkalpha321",
"client_secret": "4T39A4t_G#7erR$TYgrJ9hmT"
}
},
"subscription_payloads": [
{
"subscriptionMessageTypes": [
"TRD"
],
"subscriptions": [
{
"exchangeMic": "XCME",
"productType": "FUT",
"productCode": "BTC"
}
]
}
]
}
@@ -1 +0,0 @@
#include "cme_connection_metadata.h"
@@ -1,112 +0,0 @@
#pragma once
#include "bts-system/logging.h"
#include "scratch_client.h"
#include <websocketpp/client.hpp>
#include <websocketpp/common/system_error.hpp>
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/common/memory.hpp>
#include <websocketpp/common/thread.hpp>
#include <cstdlib>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
namespace bts::websocket {
class CmeConnectionMetadata {
public:
typedef websocketpp::lib::shared_ptr<CmeConnectionMetadata> ptr;
CmeConnectionMetadata(
int id, websocketpp::connection_hdl hdl, std::string uri)
: m_id(id), m_hdl(hdl), m_status("Connecting"), m_uri(uri),
m_server("N/A") {}
void on_open(client *c, websocketpp::connection_hdl hdl) {
LOG(Md, Info, "CmeConnectionMetadata::on_open {}", std::string{"nice"});
std::string text{R"({
"header": {
"messageType": "subscribe",
"sentTime": "",
"version": "",
"requestId": "1",
"token": ""
},
"payload": {
"subscriptionMessageTypes": ["TRD"],
"subscriptions": [
{
"exchangeMic": "XCME",
"productType": "FUT",
"productCode": "BTC"
}
]
}
})"};
m_status = "Open";
std::cout << "SEND IT!!!!!!" << std::endl;
websocketpp::lib::error_code ec;
c->send(hdl, text, websocketpp::frame::opcode::value::TEXT, ec);
if (ec) {
std::cout << "DAMNIT!!!!" << std::endl;
}
client::connection_ptr con = c->get_con_from_hdl(hdl);
m_server = con->get_response_header("Server");
}
void on_fail(client *c, websocketpp::connection_hdl hdl) {
m_status = "Failed";
client::connection_ptr con = c->get_con_from_hdl(hdl);
m_server = con->get_response_header("Server");
m_error_reason = con->get_ec().message();
}
void on_close(client *c, websocketpp::connection_hdl hdl) {
m_status = "Closed";
client::connection_ptr con = c->get_con_from_hdl(hdl);
std::stringstream s;
s << "close code: " << con->get_remote_close_code() << " ("
<< websocketpp::close::status::get_string(con->get_remote_close_code())
<< "), close reason: " << con->get_remote_close_reason();
m_error_reason = s.str();
}
websocketpp::connection_hdl get_hdl() const { return m_hdl; }
int get_id() const { return m_id; }
std::string get_status() const { return m_status; }
friend std::ostream &operator<<(
std::ostream &out, CmeConnectionMetadata const &data);
private:
int m_id;
websocketpp::connection_hdl m_hdl;
std::string m_status;
std::string m_uri;
std::string m_server;
std::string m_error_reason;
};
inline std::ostream &operator<<(
std::ostream &out, CmeConnectionMetadata const &data) {
out << "> URI: " << data.m_uri << "\n"
<< "> Status: " << data.m_status << "\n"
<< "> Remote Server: "
<< (data.m_server.empty() ? "None Specified" : data.m_server) << "\n"
<< "> Error/close reason: "
<< (data.m_error_reason.empty() ? "N/A" : data.m_error_reason);
return out;
}
} // namespace bts::websocket
@@ -1,44 +0,0 @@
#pragma once
#include <nlohmann/detail/macro_scope.hpp>
#include <nlohmann/json.hpp>
#include <string>
#include "bts-http/URL.h"
namespace bts::websocket {
class CmeOauthConfig {
public:
std::string protocol{"https"};
std::string host;
std::uint16_t port{443};
std::string endpoint;
std::string client_id;
std::string client_secret;
io::URL get_url() const;
void get_url(io::URL &dest) const;
};
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
CmeOauthConfig, protocol, host, port, endpoint, client_id, client_secret);
inline io::URL CmeOauthConfig::get_url() const {
io::URL url;
get_url(url);
return url;
}
inline void CmeOauthConfig::get_url(io::URL &dest) const {
dest.fill_host(host);
dest.fill_path(endpoint);
dest.fill_port(port);
dest.fill_protocol("https");
}
} // namespace bts::websocket
@@ -1,45 +0,0 @@
#pragma once
#include <nlohmann/detail/macro_scope.hpp>
#include <string>
#include "bts-http/URL.h"
#include "bts-websocket-listener/cme_oauth_config.h"
namespace bts::websocket {
class CmeWebsocketConfig {
public:
// Needed to obtain the access token (ignored if access token is non-empty)
CmeOauthConfig oauth;
std::string access_token;
std::string protocol{"wss"};
std::string host;
std::uint16_t port{443};
std::string endpoint;
io::URL get_url() const;
void get_url(io::URL &dest) const;
};
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
CmeWebsocketConfig, oauth, access_token, protocol, host, port, endpoint);
inline io::URL CmeWebsocketConfig::get_url() const {
io::URL url;
get_url(url);
return url;
}
inline void CmeWebsocketConfig::get_url(io::URL &dest) const {
dest.fill_host(host);
dest.fill_path(endpoint);
dest.fill_port(port);
dest.fill_protocol(protocol);
}
} // namespace bts::websocket
@@ -1,257 +0,0 @@
#include "cme_websocket_session.h"
#include <curl/curl.h>
#include <curl/easy.h>
#include <fmt/format.h>
#include <exception>
#include <functional>
#include <stdexcept>
#include <string>
#include <thread>
#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/roles/client_endpoint.hpp>
#include "bts-http-client/curl_requestor.h"
#include "bts-http-client/http_client.h"
#include "bts-http/QueryParams.h"
#include "bts-http/URL.h"
#include "bts-http/http_request.h"
#include "bts-http/http_response.h"
#include "bts-http/oauth_response.h"
#include "bts-json/JsonDumper.h"
#include "bts-json/JsonReader.h"
#include "bts-system/UNUSED.h"
#include "bts-system/bts_assert.h"
#include "bts-websocket-listener/cme_connection_metadata.h"
#include "bts-websocket-listener/cme_oauth_config.h"
#include "bts-websocket-listener/cme_websocket_config.h"
#include "bts-websocket-listener/ssl_certificate_verifier.h"
#include "bts-websocket-listener/tls_initializer.h"
typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
typedef websocketpp::lib::shared_ptr<websocketpp::lib::asio::ssl::context>
context_ptr;
typedef std::map<int, bts::websocket::CmeConnectionMetadata::ptr> con_list;
using websocketpp::lib::bind;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
namespace bts::websocket {
using namespace std::chrono_literals;
namespace beast = boost::beast;
using bts::http::HTTPClient;
using bts::http::HTTPRequest;
using bts::http::HTTPResponse;
using std::string;
// ws endpoint http://markets.api.uat.cmegroup.com/marketdatastream/v1
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
static HTTPRequest build_oauth_request(const CmeOauthConfig &cfg) {
HTTPRequest http_req;
http_req.url = cfg.get_url().to_string();
io::QueryParams qp;
qp["grant_type"] = "client_credentials";
qp["client_id"] = cfg.client_id;
qp["client_secret"] = cfg.client_secret;
http_req.body = qp.to_string();
return http_req;
}
static OauthResponse request_oauth_token(const CmeOauthConfig &cfg) {
HTTPRequest req = build_oauth_request(cfg);
LOG(Gen, Info, "Requesting oauth:\n{}", io::JsonDumper::dump(req));
HTTPClient client;
HTTPResponse res = client.post(req);
LOG(Gen, Info, "Received oauth response:\n{}", io::JsonDumper::dump(res));
const auto j = nlohmann::json::parse(res.body());
return io::JsonReader{&j}.read<OauthResponse>();
}
struct CmeWebsocketSession::PImpl {
CmeWebsocketConfig cfg;
CmeConnectionMetadata::ptr conn_state;
client m_endpoint;
bool client_is_open = false;
void connect() {
const auto url = cfg.get_url();
string path = url.path();
if (path.starts_with('/')) {
path.erase(path.begin());
}
const auto url_str = fmt::format("wss://{}/{}", url.host(), url.path());
try {
// Set up verbose logging
m_endpoint.set_access_channels(websocketpp::log::alevel::all);
m_endpoint.clear_access_channels(
websocketpp::log::alevel::frame_payload);
m_endpoint.set_error_channels(websocketpp::log::elevel::all);
// Set up handlers
m_endpoint.init_asio();
m_endpoint.set_message_handler([&](auto hdl, auto msg) {
(void)hdl;
std::cout << "CmeWebsocketSession::on_message " << std::endl;
std::cout << msg->get_payload() << std::endl;
LOG(Md, Info, "CmeWebsocketSession::on_message {}",
msg->get_payload());
});
m_endpoint.set_open_handler([this](auto hdl) {
(void)(hdl);
LOG(gen, Info, "on_open {}", string{"yay!"});
client_is_open = true;
});
m_endpoint.set_tls_init_handler([](auto) {
auto result =
websocketpp::lib::make_shared<boost::asio::ssl::context>(
boost::asio::ssl::context::sslv23_client);
result->set_verify_mode(boost::asio::ssl::verify_none);
return result;
});
m_endpoint.set_close_handler([this](auto hdl) {
client_is_open = false;
auto con = m_endpoint.get_con_from_hdl(hdl);
std::cout << "close handler: " << con->get_ec().message()
<< std::endl;
});
m_endpoint.set_fail_handler([this](auto hdl) {
auto con = m_endpoint.get_con_from_hdl(hdl);
std::cout << "fail handler: " << con->get_ec().message()
<< std::endl;
});
websocketpp::lib::error_code ec;
websocketpp::client<
websocketpp::config::asio_tls_client>::connection_ptr con =
m_endpoint.get_connection(url_str, ec);
if (ec) {
bts_assert_msgf(
false, "Failed to get_connection: {}", ec.message());
}
// Set authorization header
con->append_header("Authorization", "Bearer " + cfg.access_token);
m_endpoint.connect(con);
auto run_thread = std::thread{[this]() { m_endpoint.run(); }};
while (!client_is_open) {
sleep(1); //TODO: use an mutex instead
}
int heartbeat_interval = 2000; //in ms: TODO extract from message
bool subscribed = false;
while (true) {
std::this_thread::sleep_for(
std::chrono::milliseconds(heartbeat_interval));
if (subscribed) {
continue;
}
subscribed = true;
string text{R"({
"header": {
"messageType": "subscribe",
"sentTime": "",
"version": "",
"requestId": "1",
"token": ""
},
"payload": {
"subscriptionMessageTypes": ["TRD"],
"subscriptions": [
{
"exchangeMic": "XCME",
"productType": "FUT",
"productCode": "BTC"
}
]
}
})"};
websocketpp::lib::error_code write_ec;
m_endpoint.send(
con, text, websocketpp::frame::opcode::value::TEXT, write_ec);
std::cout << "write_ec: " << write_ec.message() << std::endl;
// client.send(
// con, "{\"op\":1, \"d\":null}",
// websocketpp::frame::opcode::text);
}
run_thread.join();
// Connect
m_endpoint.connect(con);
m_endpoint.run();
} catch (const websocketpp::exception &ex) {
LOG(Md, Error, "Failed to connect to cme websocket server: {}",
string{ex.what()});
}
}
void close(websocketpp::close::status::value code, std::string reason) {
websocketpp::lib::error_code ec;
LOG(Md, Warn, "Closing connection {}...", this->conn_state->get_id());
m_endpoint.close(conn_state->get_hdl(), code, reason, ec);
if (ec) {
LOG(Md, Error, "Error initiating close: {}", ec.message());
}
}
};
CmeWebsocketSession::CmeWebsocketSession(CmeWebsocketConfig cfg)
: me_(std::make_unique<PImpl>()) {
me_->cfg = std::move(cfg);
me_->connect();
string text{R"({
"header": {
"messageType": "subscribe",
"sentTime": "",
"version": "",
"requestId": "1",
"token": ""
},
"payload": {
"subscriptionMessageTypes": ["TRD"],
"subscriptions": [
{
"exchangeMic": "XCME",
"productType": "FUT",
"productCode": "BTC"
}
]
}
})"};
// BoostExample3::run(
// me_->cfg.host, std::to_string(me_->cfg.port), me_->cfg.endpoint, text,
// me_->cfg.access_token);
}
CmeWebsocketSession::~CmeWebsocketSession() = default;
CmeWebsocketSession CmeWebsocketSession::create(CmeWebsocketConfig cfg) {
std::cout << io::JsonDumper::dump(cfg) << std::endl;
// bts_assert(false);
if (cfg.access_token.empty()) {
auto oauth_res = request_oauth_token(cfg.oauth);
const auto oauth_res_str = io::JsonDumper::dump(oauth_res);
cfg.access_token = oauth_res.access_token;
if (cfg.access_token.empty()) {
throw std::runtime_error{"Failed to obtain oauth access token"};
}
}
return CmeWebsocketSession{std::move(cfg)};
}
int CmeWebsocketSession::run(const CmeWebsocketConfig &cfg) {
auto session = create(cfg);
return 0;
}
} // namespace bts::websocket
@@ -1,37 +0,0 @@
#pragma once
#include <curl/curl.h>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <iostream>
#include <memory>
#include <string>
#include "bts-http/URL.h"
#include "bts-websocket-listener/cme_connection_metadata.h"
namespace bts::websocket {
class CmeOauthConfig;
class CmeWebsocketConfig;
class CmeWebsocketSession {
public:
explicit CmeWebsocketSession(CmeWebsocketConfig cfg);
~CmeWebsocketSession();
static CmeWebsocketSession create(CmeWebsocketConfig cfg);
static int run(const CmeWebsocketConfig &cfg);
void connect();
void close();
CmeConnectionMetadata::ptr get_metadata() const;
private:
struct PImpl;
std::unique_ptr<PImpl> me_;
};
} // namespace bts::websocket
@@ -1,47 +0,0 @@
#include <boost/asio.hpp>
#include <boost/beast.hpp>
#include <curlpp/cURLpp.hpp>
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
#include <string>
#include <CLI11/CLI11.hpp>
#include "bts-json/JsonDumper.h"
#include "bts-json/JsonReader.h"
#include "bts-system/UNUSED.h"
#include "bts-websocket-listener/cme_websocket_config.h"
#include "bts-websocket-listener/cme_websocket_session.h"
using bts::io::JsonReader;
using bts::websocket::CmeWebsocketConfig;
using bts::websocket::CmeWebsocketSession;
using CLI::App;
using std::ifstream;
using std::string;
static CmeWebsocketConfig parse_config(const string &config_arg) {
ifstream cfg_ifs{config_arg};
const auto cfg_j = nlohmann::json::parse(cfg_ifs);
JsonReader cfg_jr{&cfg_j};
CmeWebsocketConfig cfg;
bts_assert(cfg_jr.read(cfg));
return cfg;
}
static void run(const std::string &config_arg) {
const auto cfg = parse_config(config_arg);
curlpp::initialize();
CmeWebsocketSession::run(cfg);
curlpp::terminate();
}
int main(int argc, char **argv) {
App app{"Cme websocket listener", "bts-websocket-listener"};
string config_arg;
app.add_option(
"--config", config_arg, "Path to CmeWebsocketConfig json file")
->required();
app.callback([&config_arg]() { run(config_arg); });
CLI11_PARSE(app, argc, argv);
}
@@ -1,17 +0,0 @@
#pragma once
#include "bts-system/TimeStamp.h"
#include <optional>
#include <string>
namespace bts::websocket {
class MessageHeader {
public:
const std::string messageType;
const std::optional<core::TimeStamp> sentTime;
const std::int64_t version;
};
} // namespace bts::websocket
@@ -1,20 +0,0 @@
#pragma once
#include "bts-websocket-listener/messages/message_header.h"
#include "bts-websocket-listener/messages/trade_summary_payload.h"
#include <nlohmann/detail/macro_scope.hpp>
#include <nlohmann/json.hpp>
#include <string>
namespace bts::websocket {
class TradeSummaryMessage {
public:
const MessageHeader header;
const TradeSummaryPayload payload;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(TradeSummaryMessage, header, payload)
} // namespace bts::websocket
@@ -1,19 +0,0 @@
#pragma once
#include "bts-websocket-listener/messages/trade_summary.h"
#include <nlohmann/detail/macro_scope.hpp>
#include <nlohmann/json.hpp>
#include <string>
namespace bts::websocket {
class TradeSummaryPayload {
public:
const std::int64_t lastUpdateTime;
const TradeSummary tradeSummary;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(TradeSummaryPayload, lastUpdateTime, tradeSummary)
} // namespace bts::websocket
@@ -1,179 +0,0 @@
#include "scratch_client.h"
#include "bts-websocket-listener/cme_connection_metadata.h"
#include <websocketpp/client.hpp>
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/common/memory.hpp>
#include <websocketpp/common/thread.hpp>
#include <cstdlib>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
// typedef websocketpp::client<websocketpp::config::asio_client> client;
namespace bts::websocket {
using connection_metadata = CmeConnectionMetadata;
class websocket_endpoint {
public:
websocket_endpoint() : m_next_id(0) {
m_endpoint.clear_access_channels(websocketpp::log::alevel::all);
m_endpoint.clear_error_channels(websocketpp::log::elevel::all);
m_endpoint.init_asio();
m_endpoint.start_perpetual();
m_thread.reset(new websocketpp::lib::thread(&client::run, &m_endpoint));
}
~websocket_endpoint() {
m_endpoint.stop_perpetual();
for (con_list::const_iterator it = m_connection_list.begin();
it != m_connection_list.end(); ++it) {
if (it->second->get_status() != "Open") {
// Only close open connections
continue;
}
std::cout << "> Closing connection " << it->second->get_id()
<< std::endl;
websocketpp::lib::error_code ec;
m_endpoint.close(
it->second->get_hdl(), websocketpp::close::status::going_away, "",
ec);
if (ec) {
std::cout << "> Error closing connection " << it->second->get_id()
<< ": " << ec.message() << std::endl;
}
}
m_thread->join();
}
int connect(std::string const &uri) {
websocketpp::lib::error_code ec;
client::connection_ptr con = m_endpoint.get_connection(uri, ec);
if (ec) {
std::cout << "> Connect initialization error: " << ec.message()
<< std::endl;
return -1;
}
int new_id = m_next_id++;
connection_metadata::ptr metadata_ptr(
new connection_metadata(new_id, con->get_handle(), uri));
m_connection_list[new_id] = metadata_ptr;
con->set_open_handler(websocketpp::lib::bind(
&connection_metadata::on_open, metadata_ptr, &m_endpoint,
websocketpp::lib::placeholders::_1));
con->set_fail_handler(websocketpp::lib::bind(
&connection_metadata::on_fail, metadata_ptr, &m_endpoint,
websocketpp::lib::placeholders::_1));
con->set_close_handler(websocketpp::lib::bind(
&connection_metadata::on_close, metadata_ptr, &m_endpoint,
websocketpp::lib::placeholders::_1));
m_endpoint.connect(con);
return new_id;
}
void close(
int id, websocketpp::close::status::value code, std::string reason) {
websocketpp::lib::error_code ec;
con_list::iterator metadata_it = m_connection_list.find(id);
if (metadata_it == m_connection_list.end()) {
std::cout << "> No connection found with id " << id << std::endl;
return;
}
m_endpoint.close(metadata_it->second->get_hdl(), code, reason, ec);
if (ec) {
std::cout << "> Error initiating close: " << ec.message() << std::endl;
}
}
connection_metadata::ptr get_metadata(int id) const {
con_list::const_iterator metadata_it = m_connection_list.find(id);
if (metadata_it == m_connection_list.end()) {
return connection_metadata::ptr();
} else {
return metadata_it->second;
}
}
private:
typedef std::map<int, connection_metadata::ptr> con_list;
client m_endpoint;
websocketpp::lib::shared_ptr<websocketpp::lib::thread> m_thread;
con_list m_connection_list;
int m_next_id;
};
int ScratchClient::run() {
bool done = false;
std::string input;
websocket_endpoint endpoint;
while (!done) {
std::cout << "Enter Command: ";
std::getline(std::cin, input);
if (input == "quit") {
done = true;
} else if (input == "help") {
std::cout << "\nCommand List:\n"
<< "connect <ws uri>\n"
<< "close <connection id> [<close code:default=1000>] "
"[<close reason>]\n"
<< "show <connection id>\n"
<< "help: Display this help text\n"
<< "quit: Exit the program\n"
<< std::endl;
} else if (input.substr(0, 7) == "connect") {
int id = endpoint.connect(input.substr(8));
if (id != -1) {
std::cout << "> Created connection with id " << id << std::endl;
}
} else if (input.substr(0, 5) == "close") {
std::stringstream ss(input);
std::string cmd;
int id;
int close_code = websocketpp::close::status::normal;
std::string reason;
ss >> cmd >> id >> close_code;
std::getline(ss, reason);
endpoint.close(id, close_code, reason);
} else if (input.substr(0, 4) == "show") {
int id = atoi(input.substr(5).c_str());
connection_metadata::ptr metadata = endpoint.get_metadata(id);
if (metadata) {
std::cout << *metadata << std::endl;
} else {
std::cout << "> Unknown connection id " << id << std::endl;
}
} else {
std::cout << "> Unrecognized Command" << std::endl;
}
}
return 0;
}
} // namespace bts::websocket
@@ -1,10 +0,0 @@
#pragma once
namespace bts::websocket {
class ScratchClient {
public:
static int run();
};
} // namespace bts::websocket
@@ -0,0 +1,21 @@
{
"header": {
"messageType": "subscriptionStatus",
"requestId": 0,
"sentTime": "2023-11-01T15:21:12.376000000Z",
"version": "1.0",
"sequenceNumber": "2"
},
"payload": {
"status": "subscribed",
"subscriptionMessageTypes": [
"TRD"
],
"subscriptions": [
{
"productType": "FUT",
"productCode": "BTC"
}
]
}
}
+15 -12
View File
@@ -1,15 +1,18 @@
{
"protocol": "wss",
"host": "markets.api.uat.cmegroup.com",
"port": 0,
"endpoint": "marketdatastream/v1",
"access_token": "",
"oauth": {
"protocol": "https",
"host": "authnr.cmegroup.com",
"port": 0,
"endpoint": "as/token.oauth2",
"client_id": "api_thinkalpha321",
"client_secret": "4T39A4t_G#7erR$TYgrJ9hmT"
"websocket": {
"url": "wss://markets.api.uat.cmegroup.com/marketdatastream/v1",
"auth": {
"oauth": {
"url": "https://authnr.cmegroup.com/as/token.oauth2",
"client_id": "api_thinkalpha321",
"client_secret": "4T39A4t_G#7erR$TYgrJ9hmT"
}
}
},
"subscription_payloads": [],
"thread": {
"name_rx": "websocket_thread",
"exclude": false,
"cores": [1]
}
}
-23
View File
@@ -1,23 +0,0 @@
cmake_minimum_required(VERSION 3.23)
project(bts-http-client)
include(FetchContent)
include (GNUInstallDirs)
file(
GLOB_RECURSE ${PROJECT_NAME}_Sources
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
${PROJECT_NAME}/*.cpp)
add_library(${PROJECT_NAME} ${${PROJECT_NAME}_Sources})
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(${PROJECT_NAME} PUBLIC bts-http bts-json bts-system)
# curlpp
FetchContent_Declare(
curlpp
GIT_REPOSITORY git@github.com:jpbarrette/curlpp.git
GIT_TAG v0.8.1
SYSTEM
OVERRIDE_FIND_PACKAGE)
FetchContent_MakeAvailable(curlpp)
target_include_directories(${PROJECT_NAME} PUBLIC ${curlpp_SOURCE_DIR}/include)
target_link_directories(${PROJECT_NAME} PUBLIC ${curlpp_BINARY_DIR})
+12 -1
View File
@@ -7,4 +7,15 @@ file(
${PROJECT_NAME}/*.cpp)
add_library(${PROJECT_NAME} ${${PROJECT_NAME}_Sources})
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(${PROJECT_NAME} PUBLIC bts-json bts-system)
target_link_libraries(${PROJECT_NAME} PUBLIC bts-system)
# curlpp
FetchContent_Declare(
curlpp
GIT_REPOSITORY git@github.com:jpbarrette/curlpp.git
GIT_TAG v0.8.1
SYSTEM
OVERRIDE_FIND_PACKAGE)
FetchContent_MakeAvailable(curlpp)
target_include_directories(${PROJECT_NAME} PUBLIC ${curlpp_SOURCE_DIR}/include)
target_link_directories(${PROJECT_NAME} PUBLIC ${curlpp_BINARY_DIR})
@@ -2,7 +2,7 @@
#include <curlpp/Options.hpp>
#include "bts-http-client/curl_requestor.h"
#include "bts-http/curl_requestor.h"
#include "bts-http/http_request.h"
#include "bts-http/http_response.h"
@@ -7,7 +7,14 @@ class HTTPResponse;
class HTTPClient {
public:
/**
* Performs an http GET request
*/
HTTPResponse get(const HTTPRequest &request);
/**
* Performs an http POST request
*/
HTTPResponse post(const HTTPRequest &request);
};
@@ -0,0 +1,18 @@
#include "oauth_request.h"
namespace bts::http {
// std::string OauthRequest::perform() {
// switch (cfg.websocket.auth.type().value) {
// case WebsocketAuthorizationType::None:
// break;
// case WebsocketAuthorizationType::AccessToken:
// break;
// case WebsocketAuthorizationType::OauthRequest:
// break;
// default:
// break;
// }
// }
} // namespace bts::http
+24
View File
@@ -0,0 +1,24 @@
#pragma once
#include "bts-http/URL.h"
#include <nlohmann/detail/macro_scope.hpp>
#include <nlohmann/json.hpp>
#include <string>
namespace bts::http {
class OauthRequest {
public:
std::string url;
std::string client_id;
std::string client_secret;
};
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(OauthRequest, url, client_id, client_secret);
} // namespace bts::http
@@ -0,0 +1,70 @@
#pragma once
#include "bts-http/http_client.h"
#include "bts-http/http_headers.h"
#include "bts-http/http_request.h"
#include "bts-http/http_response.h"
#include "bts-http/oauth_request.h"
#include "bts-http/oauth_response.h"
#include "bts-system/json/json_dumper.h"
#include "bts-system/json/json_reader.h"
#include "bts-system/logging.h"
namespace bts::http {
// Be sure to undef macros at end of file (this is a header)
#define _LOG(level, ...) LOG(md, level, "OauthRequestor::" __VA_ARGS__)
#define _INFO(...) _LOG(Info, __VA_ARGS__)
#define _ERROR(...) _LOG(Error, __VA_ARGS__)
class OauthRequestor {
public:
/**
* Requests access token.
* @param cfg Configuration for the request
* @param require_success Whether or not to throw an exception if the
* request fails (i.e., access token is empty or undefined)
*/
static OauthResponse request(
const OauthRequest &cfg, bool require_success = true);
};
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline OauthResponse OauthRequestor::request(
const OauthRequest &cfg, bool require_success) {
using namespace bts::http;
using namespace bts::io;
_INFO("request. \n{}", system::json::JsonDumper::dump(cfg));
HTTPRequest req;
req.url = cfg.url;
QueryParams qp;
qp["grant_type"] = "client_credentials";
qp["client_id"] = cfg.client_id;
qp["client_secret"] = cfg.client_secret;
req.body = qp.to_string();
// _INFO("CmeOauthRequestor::request. Sending\n{}", JsonDumper::dump(req));
HTTPClient client;
HTTPResponse res = client.post(req);
// _INFO("CmeOauthRequestor::request. Received\n{}", JsonDumper::dump(res));
const auto j = nlohmann::json::parse(res.body());
auto oauth = system::json::JsonReader{&j}.read<OauthResponse>();
if ((*oauth.access_token).empty()) {
// _ERROR("request. Request failed (access token was undefined or empty)");
if (require_success) {
throw std::runtime_error{"Failed to obtain oauth access token"};
}
}
return oauth;
}
#undef _ASSERT_MSGF
#undef _LOG
#undef _DIAG
#undef _INFO
#undef _WARN
#undef _ERROR
} // namespace bts::http
+5 -4
View File
@@ -1,14 +1,15 @@
#pragma once
#include "bts-websocket/websocket_access_token.h"
#include <nlohmann/detail/macro_scope.hpp>
#include <nlohmann/json.hpp>
#include <string>
namespace bts::websocket {
namespace bts::http {
class OauthResponse {
public:
std::string access_token;
public:
websocket::WebsocketAccessToken access_token;
std::string token_type;
/**
* The number of seconds until the token expires
@@ -19,4 +20,4 @@ class OauthResponse {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
OauthResponse, access_token, token_type, expires_in);
} // namespace bts::websocket
} // namespace bts::http
-28
View File
@@ -1,28 +0,0 @@
cmake_minimum_required(VERSION 3.23)
project(bts-json)
include(FetchContent)
file(
GLOB_RECURSE ${PROJECT_NAME}_Sources
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
${PROJECT_NAME}/*.cpp)
add_library(${PROJECT_NAME} ${${PROJECT_NAME}_Sources})
target_link_libraries(${PROJECT_NAME} PUBLIC
bts-system
debug fmtd
optimized fmt)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
# nlohmann json
FetchContent_Declare(
nlohmann_json
GIT_REPOSITORY git@github.com:nlohmann/json.git
GIT_TAG v3.11.1
CMAKE_ARGS
-DJSON_BuildTests=OFF
-DJSON_SystemInclude=ON
SYSTEM
OVERRIDE_FIND_PACKAGE)
FetchContent_MakeAvailable(nlohmann_json)
find_package(nlohmann_json REQUIRED)
target_link_libraries(${PROJECT_NAME} PUBLIC nlohmann_json)
-519
View File
@@ -1,519 +0,0 @@
#include "JsonReader.h"
#include <boost/date_time/posix_time/ptime.hpp>
#include <exception>
#include <limits>
#include <string>
#include "bts-system/TryCatch.h"
#include "bts-system/datetime.h"
namespace bts::io {
using core::TimeStamp;
using nlohmann::json;
using std::int32_t;
using std::int64_t;
using std::numeric_limits;
using std::stoull;
using std::string;
using std::uint32_t;
using std::uint64_t;
// 'read' fn bodies should be wrapped between BEGIN/END_READ_JSON tags.
// These macros abstract out repetative exception handling code (and a nullptr check).
#define BEGIN_READ_JSON \
if (json_ == nullptr) { \
return false; \
} \
try
#define END_READ_JSON(type_name) \
catch (const std::exception &e) { \
LOG(Gen, Diag, "Failed to parse json to '{}'. Exception: {}", \
std::string{type_name}, std::string{e.what()}); \
} \
catch (...) { \
bts_assert_msgf( \
false, "Failed to parse json to '{}'. Exception: unknown", \
std::string{type_name}); \
} \
return false;
// The idea here is we parse numbers to wide types (either int64, uint64, or double),
// then cast that value to the (potentially narrower) output type. This macro evaluates
// to true if 'wide_val' is within the bounds of the result type (otherwise false).
#define NUM_WITHIN_BOUNDS(wide_val, result_t) \
(wide_val >= \
static_cast<decltype(wide_val)>(numeric_limits<result_t>::min()) && \
wide_val <= \
static_cast<decltype(wide_val)>(numeric_limits<result_t>::max()))
static const nlohmann::json *get_field_json(
const nlohmann::json *json, const std::string &name) {
if (!json || !json->is_object()) {
return nullptr;
}
auto it = json->find(name);
if (it == json->end()) {
return nullptr;
}
return &it.value();
}
JsonReader::JsonReader(const nlohmann::json *json) : json_(json) {}
JsonReader::JsonReader(const nlohmann::json *json, const std::string &name)
: json_(get_field_json(json, name)) {}
bool JsonReader::try_parse(
const std::string &str, nlohmann::json &result, std::string *err) {
try {
result = nlohmann::json::parse(str);
return true;
}
CATCH_AND_HANDLE_MSG([err](const auto &msg) {
if (err != nullptr) {
*err = msg;
}
})
return false;
}
bool JsonReader::read(const string &name, string &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read(const string &name, int32_t &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read(const string &name, uint32_t &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read(const string &name, int64_t &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read(const string &name, uint64_t &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read(const string &name, float &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read(const string &name, double &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read(const string &name, TimeStamp &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read_epoch_nanos(const string &name, TimeStamp &value) const {
return JsonReader{json_, name}.read_epoch_nanos(value);
}
bool JsonReader::read_epoch_micros(const string &name, TimeStamp &value) const {
return JsonReader{json_, name}.read_epoch_micros(value);
}
bool JsonReader::read_epoch_millis(const string &name, TimeStamp &value) const {
return JsonReader{json_, name}.read_epoch_millis(value);
}
bool JsonReader::read(const string &name, nlohmann::json &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read(int32_t &result) const {
BEGIN_READ_JSON {
switch (json_->type()) {
case nlohmann::json::value_t::number_float: {
const auto d = json_->get<double>();
result = static_cast<int32_t>(d);
if (result != d) {
LOG(Gen, Warn,
"Potential loss of data converting field of type "
"'number_float' ({}) to int32 ({})",
d, result);
}
return true;
}
case nlohmann::json::value_t::number_integer: {
const auto il = json_->get<int64_t>();
if (!NUM_WITHIN_BOUNDS(il, int32_t)) {
LOG(Gen, Error,
"Failed to convert field of type 'number_integer' ({}) to "
"uint32: value is negative.",
il);
}
result = static_cast<int32_t>(il);
return true;
}
case nlohmann::json::value_t::number_unsigned: {
const auto ul = json_->get<uint64_t>();
if (ul > static_cast<uint64_t>(numeric_limits<int32_t>::max())) {
LOG(Gen, Error,
"Failed to convert field of type 'number_unsigned' ({}) to "
"int32: value is too large.",
ul);
return false;
}
result = static_cast<int32_t>(ul);
return true;
}
case nlohmann::json::value_t::string: {
const auto str = json_->get<string>();
if (!str.empty()) {
result = std::stoi(str);
return true;
}
break;
}
default:
return false;
}
}
END_READ_JSON("int32")
}
bool JsonReader::read(uint32_t &result) const {
BEGIN_READ_JSON {
switch (json_->type()) {
case nlohmann::json::value_t::number_float: {
const auto d = json_->get<double>();
result = static_cast<uint32_t>(d);
if (result != d) {
LOG(Gen, Warn,
"Potential loss of data converting field of type "
"'number_float' ({}) to uint32 ({})",
d, result);
}
return true;
}
case nlohmann::json::value_t::number_integer: {
const auto i = json_->get<int64_t>();
if (!NUM_WITHIN_BOUNDS(i, uint32_t)) {
LOG(Gen, Error,
"Failed to convert field of type 'number_integer' ({}) to "
"uint32: value is negative.",
i);
return false;
}
result = static_cast<uint32_t>(i);
return true;
}
case nlohmann::json::value_t::number_unsigned: {
const auto ul = json_->get<uint64_t>();
if (ul > static_cast<uint64_t>(numeric_limits<int32_t>::max())) {
LOG(Gen, Error,
"Failed to convert field of type 'number_unsigned' ({}) to "
"int32: value is too large.",
ul);
return false;
}
result = static_cast<int32_t>(ul);
return true;
}
case nlohmann::json::value_t::string: {
const auto str = json_->get<string>();
if (!str.empty()) {
result = std::stoul(str);
return true;
}
break;
}
default:
return false;
}
}
END_READ_JSON("uint32")
}
bool JsonReader::read(int64_t &result) const {
BEGIN_READ_JSON {
switch (json_->type()) {
case nlohmann::json::value_t::number_float: {
const auto d = json_->get<double>();
if (result != d) {
LOG(Gen, Warn,
"Potential loss of data converting field of type "
"'number_float' ({}) to int64 ({})",
d, result);
}
result = static_cast<int64_t>(d);
return true;
}
case nlohmann::json::value_t::number_integer: {
result = json_->get<int64_t>();
return true;
}
case nlohmann::json::value_t::number_unsigned: {
const auto ul = json_->get<uint64_t>();
if (NUM_WITHIN_BOUNDS(ul, int64_t)) {
LOG(Gen, Error,
"Potential loss of data converting field of type "
"'number_unsigned' ({}) to int64 ({})",
ul, result);
return false;
}
result = static_cast<int64_t>(ul);
return true;
}
case nlohmann::json::value_t::string: {
const auto str = json_->get<string>();
if (!str.empty()) {
result = std::stol(str);
return true;
}
break;
}
default:
return false;
}
}
END_READ_JSON("int64")
}
bool JsonReader::read(uint64_t &result) const {
BEGIN_READ_JSON {
switch (json_->type()) {
case nlohmann::json::value_t::number_float: {
const auto d = json_->get<double>();
if (result != d) {
LOG(Gen, Warn,
"Potential loss of data converting field of type "
"'number_float' ({}) to uint64 ({})",
d, result);
}
result = static_cast<uint64_t>(d);
return true;
}
case nlohmann::json::value_t::number_integer: {
const auto il = json_->get<int64_t>();
if (NUM_WITHIN_BOUNDS(il, uint64_t)) {
LOG(Gen, Error,
"Potential loss of data converting field of type "
"'number_integer' ({}) to uint64 ({})",
il, result);
return false;
}
result = static_cast<uint64_t>(il);
return true;
}
case nlohmann::json::value_t::number_unsigned: {
result = json_->get<uint64_t>();
return true;
}
case nlohmann::json::value_t::string: {
const auto str = json_->get<string>();
if (!str.empty()) {
result = std::stoul(str);
return true;
}
break;
}
default:
return false;
}
}
END_READ_JSON("uint64")
}
bool JsonReader::read(float &result) const {
BEGIN_READ_JSON {
switch (json_->type()) {
case nlohmann::json::value_t::number_float:
case nlohmann::json::value_t::number_integer:
case nlohmann::json::value_t::number_unsigned:
json_->get_to(result);
return true;
case nlohmann::json::value_t::string: {
const auto str = json_->get<string>();
if (!str.empty()) {
result = std::stof(str);
return true;
}
break;
}
default:
return false;
}
}
END_READ_JSON("float")
}
bool JsonReader::read(double &result) const {
BEGIN_READ_JSON {
switch (json_->type()) {
case nlohmann::json::value_t::number_float:
case nlohmann::json::value_t::number_integer:
case nlohmann::json::value_t::number_unsigned:
json_->get_to(result);
return true;
case nlohmann::json::value_t::string: {
const auto str = json_->get<string>();
if (!str.empty()) {
result = std::stod(str);
return true;
}
break;
}
default:
return false;
}
}
END_READ_JSON("double")
}
bool JsonReader::read(string &result) const {
BEGIN_READ_JSON {
json_->get_to(result);
return true;
}
END_READ_JSON("string")
}
bool JsonReader::read(nlohmann::json &result) const {
BEGIN_READ_JSON {
json_->get_to(result);
return true;
}
END_READ_JSON("json-object")
}
bool JsonReader::read_flat(std::vector<nlohmann::json> &result) const {
if (json_ == nullptr) {
return false;
}
switch (json_->type()) {
case json::value_t::array:
return std::all_of(json_->begin(), json_->end(), [&](const auto &j) {
return JsonReader{&j}.read_flat(result);
});
case json::value_t::null:
return false;
default:
read(result.emplace_back());
return true;
}
}
bool JsonReader::parse_time_stamp_(const string &str, TimeStamp &value) const {
return parse_iso_time_stamp_(str, value) ||
parse_epoch_nanos_time_stamp_(str, value);
}
bool JsonReader::parse_iso_time_stamp_(
const string &str, TimeStamp &value) const {
value = TimeStamp::from_iso_string(str);
return true;
}
bool JsonReader::parse_epoch_nanos_time_stamp_(
const string &str, TimeStamp &value) const {
uint64_t nanos = stoull(str);
value = TimeStamp{nanos};
return true;
}
bool JsonReader::read(TimeStamp &result) const {
BEGIN_READ_JSON {
switch (json_->type()) {
case nlohmann::json::value_t::null:
case nlohmann::json::value_t::object:
case nlohmann::json::value_t::array:
case nlohmann::json::value_t::boolean:
case nlohmann::json::value_t::binary:
case nlohmann::json::value_t::discarded:
return false;
case nlohmann::json::value_t::string: {
const auto str = json_->get<string>();
return parse_iso_time_stamp_(str, result) ||
parse_epoch_nanos_time_stamp_(str, result);
}
case nlohmann::json::value_t::number_integer:
case nlohmann::json::value_t::number_unsigned:
case nlohmann::json::value_t::number_float:
// Maybe we could assume seconds here, idk though...when in doubt, prob
// better to be cautious than cavalier
return false;
}
}
END_READ_JSON("TimeStamp")
}
bool JsonReader::read_iso(
const std::string &name, core::TimeStamp &result) const {
return JsonReader{json_, name}.read_iso(result);
}
bool JsonReader::read_iso(core::TimeStamp &result) const {
BEGIN_READ_JSON {
switch (json_->type()) {
case nlohmann::json::value_t::string: {
return parse_iso_time_stamp_(json_->get<string>(), result);
}
default:
return false;
}
}
END_READ_JSON("TimeStamp")
}
bool JsonReader::read_epoch_nanos(TimeStamp &value) const {
int64_t nanos;
if (!read(nanos)) {
return false;
}
value = TimeStamp::from_nanos(nanos);
return true;
}
bool JsonReader::read_epoch_micros(TimeStamp &value) const {
int64_t micros;
if (!read(micros)) {
return false;
}
value = TimeStamp::from_micros(micros);
return true;
}
bool JsonReader::read_epoch_millis(TimeStamp &value) const {
int64_t millis;
if (!read(millis)) {
return false;
}
value = TimeStamp::from_millis(millis);
return true;
}
bool JsonReader::read_optional_epoch_millis(
std::optional<TimeStamp> &result) const {
if (json_ == nullptr) {
return false;
}
if (json_->is_null()) {
result = std::nullopt;
return true;
}
return read_epoch_millis(result.emplace());
}
bool JsonReader::read_optional_epoch_millis(
const std::string &name, std::optional<TimeStamp> &result) const {
if (!json_ || !json_->is_object()) {
return false;
}
auto it = json_->find(name);
if (it == json_->end()) {
return false;
}
return JsonReader{&*it}.read_epoch_millis(result.emplace());
}
} // namespace bts::io
+15
View File
@@ -1,5 +1,6 @@
cmake_minimum_required(VERSION 3.23)
project(bts-system)
include(FetchContent)
file(
GLOB_RECURSE ${PROJECT_NAME}_Sources
@@ -10,3 +11,17 @@ target_link_libraries(${PROJECT_NAME} PUBLIC
debug fmtd
optimized fmt)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
# nlohmann json
FetchContent_Declare(
nlohmann_json
GIT_REPOSITORY git@github.com:nlohmann/json.git
GIT_TAG v3.11.1
CMAKE_ARGS
-DJSON_BuildTests=OFF
-DJSON_SystemInclude=ON
SYSTEM
OVERRIDE_FIND_PACKAGE)
FetchContent_MakeAvailable(nlohmann_json)
find_package(nlohmann_json REQUIRED)
target_link_libraries(${PROJECT_NAME} PUBLIC nlohmann_json)
-28
View File
@@ -1,28 +0,0 @@
#pragma once
#define CATCH_AND_LOG(subsys, level, label) \
catch (const std::exception &ex) { \
LOG(subsys, level, label ": {}", std::string{ex.what()}); \
} \
catch (...) { \
LOG(subsys, level, label ": Unknown exception"); \
}
#define CATCH_AND_HANDLE(err_var_name, impl) \
catch (const std::exception &err_var_name##_ex) { \
const auto &err_var_name = err_var_name##_ex.what(); \
impl; \
} \
catch (...) { \
const std::string err_var_name{"Unknown exception"}; \
impl; \
}
// Normalizes 'std::exception' and '...' handlers into single callback
#define CATCH_AND_HANDLE_MSG(impl) \
catch (const std::exception &ex) { \
impl(std::string{ex.what()}); \
} \
catch (...) { \
impl(std::string{"Unknown exception"}); \
}
@@ -4,7 +4,6 @@
#include <iostream>
#include <thread>
// #include "lib/logging/logging.h"
#include "bts_debugger.h"
#ifdef _WIN32
+83
View File
@@ -0,0 +1,83 @@
#pragma once
#include <type_traits>
#include <utility>
namespace bts::system {
// Provides a very fast, very small roughly equivalent to
// std::function. Unlike std::function instances of Delegate do not
// copy the call back they are bound to.
template <typename Signature>
class Delegate;
template <typename R, typename... Arg>
class Delegate<R(Arg...)> {
private:
void *target_obj_;
R (*invoke_)(void *, Arg...);
template <class T>
struct handler_traits;
template <class T, class Re>
struct handler_traits<Re T::*> {
using HandlerType = T;
using HandlerReturnType = Re;
};
template <auto handler_member, typename Re, typename... Args>
static R invoke_member_handler(void *handler_obj, Args... arg) {
using HandlerMethod = decltype(handler_member);
static_assert(std::is_member_function_pointer<HandlerMethod>::value);
using HandlerType = typename handler_traits<HandlerMethod>::HandlerType;
return (((HandlerType *)handler_obj)->*handler_member)(
std::forward<Arg>(arg)...);
}
template <auto handler, typename Re, typename... Args>
static R invoke_static_handler(void *, Args... arg) {
return handler(std::forward<Arg>(arg)...);
}
static R null_target(Arg...) {
if constexpr (std::is_same<void, R>::value)
return;
else
return {};
}
public:
Delegate() : target_obj_(), invoke_() { bind<&Delegate::null_target>(); }
Delegate(Delegate const &d)
: target_obj_(d.target_obj), invoke_(d.invoke_) {}
Delegate &operator=(Delegate const &d) {
target_obj_ = d.target_obj_;
invoke_ = d.invoke_;
return *this;
}
template <class C>
void bind(C &handler_obj) {
target_obj_ = &handler_obj;
bind<&C::operator()>(handler_obj);
}
template <auto handler_member>
void bind(typename handler_traits<decltype(handler_member)>::HandlerType
&handler_obj) {
target_obj_ = &handler_obj;
invoke_ = invoke_member_handler<handler_member, R, Arg...>;
}
template <auto static_handler>
void bind() {
target_obj_ = nullptr;
invoke_ = invoke_static_handler<static_handler, R, Arg...>;
}
R operator()(Arg... arg) {
return invoke_(target_obj_, std::forward<Arg>(arg)...);
}
};
} // namespace bts::system
@@ -3,7 +3,7 @@
#include <sstream>
#include <string>
namespace bts::io {
namespace bts::system::json {
class JsonDumper {
public:
@@ -16,6 +16,8 @@ class JsonDumper {
Iter last,
int indentation = 2,
const char *separator = ",\n");
static std::string dump(const nlohmann::json &value, int indentation = 2);
};
//////////////////////////////////////////////////
@@ -29,6 +31,11 @@ std::string JsonDumper::dump(const T &value, int indentation) {
return j.dump(indentation);
}
inline std::string JsonDumper::dump(
const nlohmann::json &value, int indentation) {
return value.dump(indentation);
}
template <class Iter>
std::string JsonDumper::dump(
Iter first, Iter last, int indentation, const char *separator) {
@@ -45,4 +52,4 @@ std::string JsonDumper::dump(
return str.str();
}
} // namespace bts::io
} // namespace bts::system::json
@@ -0,0 +1,518 @@
#include "json_reader.h"
#include "bts-system/datetime.h"
#include <boost/date_time/posix_time/ptime.hpp>
#include <exception>
#include <limits>
#include <string>
namespace bts::system::json {
using core::TimeStamp;
using nlohmann::json;
using std::int32_t;
using std::int64_t;
using std::numeric_limits;
using std::stoull;
using std::string;
using std::uint32_t;
using std::uint64_t;
// 'read' fn bodies should be wrapped between BEGIN/END_READ_JSON tags.
// These macros abstract out repetative exception handling code (and a nullptr check).
#define BEGIN_READ_JSON \
if (json_ == nullptr) { \
return false; \
} \
try
#ifdef LOG_JSON_ERRORS
#define END_READ_JSON(type_name) \
catch (const std::exception &e) { \
LOG(Gen, Error, "Failed to parse json to '{}'. Exception: {}", \
std::string{type_name}, std::string{e.what()}); \
} \
return false;
#else
#define END_READ_JSON(type_name) \
catch (...) { \
} \
return false;
#endif
// The idea here is we parse numbers to wide types (either int64, uint64, or double),
// then cast that value to the (potentially narrower) output type. This macro evaluates
// to true if 'wide_val' is within the bounds of the result type (otherwise false).
#define NUM_WITHIN_BOUNDS(wide_val, result_t) \
(wide_val >= \
static_cast<decltype(wide_val)>(numeric_limits<result_t>::min()) && \
wide_val <= \
static_cast<decltype(wide_val)>(numeric_limits<result_t>::max()))
static const nlohmann::json *get_field_json(
const nlohmann::json *json, const std::string &name) {
if (!json || !json->is_object()) {
return nullptr;
}
auto it = json->find(name);
if (it == json->end()) {
return nullptr;
}
return &it.value();
}
JsonReader::JsonReader(const nlohmann::json *json) : json_(json) {}
JsonReader::JsonReader(const nlohmann::json *json, const std::string &name)
: json_(get_field_json(json, name)) {}
bool JsonReader::try_parse(
const std::string &str, nlohmann::json &result, std::string *err) {
try {
result = nlohmann::json::parse(str);
return true;
} catch (const std::exception &ex) {
if (err != nullptr) {
*err = ex.what();
}
}
return false;
}
bool JsonReader::read(const string &name, string &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read(const string &name, int32_t &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read(const string &name, uint32_t &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read(const string &name, int64_t &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read(const string &name, uint64_t &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read(const string &name, float &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read(const string &name, double &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read(const string &name, TimeStamp &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read_epoch_nanos(const string &name, TimeStamp &value) const {
return JsonReader{json_, name}.read_epoch_nanos(value);
}
bool JsonReader::read_epoch_micros(const string &name, TimeStamp &value) const {
return JsonReader{json_, name}.read_epoch_micros(value);
}
bool JsonReader::read_epoch_millis(const string &name, TimeStamp &value) const {
return JsonReader{json_, name}.read_epoch_millis(value);
}
bool JsonReader::read(const string &name, nlohmann::json &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read(int32_t &result) const {
BEGIN_READ_JSON {
switch (json_->type()) {
case nlohmann::json::value_t::number_float: {
const auto d = json_->get<double>();
result = static_cast<int32_t>(d);
if (result != d) {
LOG(Gen, Warn,
"Potential loss of data converting field of type "
"'number_float' ({}) to int32 ({})",
d, result);
}
return true;
}
case nlohmann::json::value_t::number_integer: {
const auto il = json_->get<int64_t>();
if (!NUM_WITHIN_BOUNDS(il, int32_t)) {
LOG(Gen, Error,
"Failed to convert field of type 'number_integer' ({}) to "
"uint32: value is negative.",
il);
}
result = static_cast<int32_t>(il);
return true;
}
case nlohmann::json::value_t::number_unsigned: {
const auto ul = json_->get<uint64_t>();
if (ul > static_cast<uint64_t>(numeric_limits<int32_t>::max())) {
LOG(Gen, Error,
"Failed to convert field of type 'number_unsigned' ({}) to "
"int32: value is too large.",
ul);
return false;
}
result = static_cast<int32_t>(ul);
return true;
}
case nlohmann::json::value_t::string: {
const auto str = json_->get<string>();
if (!str.empty()) {
result = std::stoi(str);
return true;
}
break;
}
default:
return false;
}
}
END_READ_JSON("int32")
}
bool JsonReader::read(uint32_t &result) const {
BEGIN_READ_JSON {
switch (json_->type()) {
case nlohmann::json::value_t::number_float: {
const auto d = json_->get<double>();
result = static_cast<uint32_t>(d);
if (result != d) {
LOG(Gen, Warn,
"Potential loss of data converting field of type "
"'number_float' ({}) to uint32 ({})",
d, result);
}
return true;
}
case nlohmann::json::value_t::number_integer: {
const auto i = json_->get<int64_t>();
if (!NUM_WITHIN_BOUNDS(i, uint32_t)) {
LOG(Gen, Error,
"Failed to convert field of type 'number_integer' ({}) to "
"uint32: value is negative.",
i);
return false;
}
result = static_cast<uint32_t>(i);
return true;
}
case nlohmann::json::value_t::number_unsigned: {
const auto ul = json_->get<uint64_t>();
if (ul > static_cast<uint64_t>(numeric_limits<int32_t>::max())) {
LOG(Gen, Error,
"Failed to convert field of type 'number_unsigned' ({}) to "
"int32: value is too large.",
ul);
return false;
}
result = static_cast<int32_t>(ul);
return true;
}
case nlohmann::json::value_t::string: {
const auto str = json_->get<string>();
if (!str.empty()) {
result = std::stoul(str);
return true;
}
break;
}
default:
return false;
}
}
END_READ_JSON("uint32")
}
bool JsonReader::read(int64_t &result) const {
BEGIN_READ_JSON {
switch (json_->type()) {
case nlohmann::json::value_t::number_float: {
const auto d = json_->get<double>();
if (result != d) {
LOG(Gen, Warn,
"Potential loss of data converting field of type "
"'number_float' ({}) to int64 ({})",
d, result);
}
result = static_cast<int64_t>(d);
return true;
}
case nlohmann::json::value_t::number_integer: {
result = json_->get<int64_t>();
return true;
}
case nlohmann::json::value_t::number_unsigned: {
const auto ul = json_->get<uint64_t>();
if (NUM_WITHIN_BOUNDS(ul, int64_t)) {
LOG(Gen, Error,
"Potential loss of data converting field of type "
"'number_unsigned' ({}) to int64 ({})",
ul, result);
return false;
}
result = static_cast<int64_t>(ul);
return true;
}
case nlohmann::json::value_t::string: {
const auto str = json_->get<string>();
if (!str.empty()) {
result = std::stol(str);
return true;
}
break;
}
default:
return false;
}
}
END_READ_JSON("int64")
}
bool JsonReader::read(uint64_t &result) const {
BEGIN_READ_JSON {
switch (json_->type()) {
case nlohmann::json::value_t::number_float: {
const auto d = json_->get<double>();
if (result != d) {
LOG(Gen, Warn,
"Potential loss of data converting field of type "
"'number_float' ({}) to uint64 ({})",
d, result);
}
result = static_cast<uint64_t>(d);
return true;
}
case nlohmann::json::value_t::number_integer: {
const auto il = json_->get<int64_t>();
if (NUM_WITHIN_BOUNDS(il, uint64_t)) {
LOG(Gen, Error,
"Potential loss of data converting field of type "
"'number_integer' ({}) to uint64 ({})",
il, result);
return false;
}
result = static_cast<uint64_t>(il);
return true;
}
case nlohmann::json::value_t::number_unsigned: {
result = json_->get<uint64_t>();
return true;
}
case nlohmann::json::value_t::string: {
const auto str = json_->get<string>();
if (!str.empty()) {
result = std::stoul(str);
return true;
}
break;
}
default:
return false;
}
}
END_READ_JSON("uint64")
}
bool JsonReader::read(float &result) const {
BEGIN_READ_JSON {
switch (json_->type()) {
case nlohmann::json::value_t::number_float:
case nlohmann::json::value_t::number_integer:
case nlohmann::json::value_t::number_unsigned:
json_->get_to(result);
return true;
case nlohmann::json::value_t::string: {
const auto str = json_->get<string>();
if (!str.empty()) {
result = std::stof(str);
return true;
}
break;
}
default:
return false;
}
}
END_READ_JSON("float")
}
bool JsonReader::read(double &result) const {
BEGIN_READ_JSON {
switch (json_->type()) {
case nlohmann::json::value_t::number_float:
case nlohmann::json::value_t::number_integer:
case nlohmann::json::value_t::number_unsigned:
json_->get_to(result);
return true;
case nlohmann::json::value_t::string: {
const auto str = json_->get<string>();
if (!str.empty()) {
result = std::stod(str);
return true;
}
break;
}
default:
return false;
}
}
END_READ_JSON("double")
}
bool JsonReader::read(string &result) const {
BEGIN_READ_JSON {
json_->get_to(result);
return true;
}
END_READ_JSON("string")
}
bool JsonReader::read(nlohmann::json &result) const {
BEGIN_READ_JSON {
json_->get_to(result);
return true;
}
END_READ_JSON("json-object")
}
bool JsonReader::read_flat(std::vector<nlohmann::json> &result) const {
if (json_ == nullptr) {
return false;
}
switch (json_->type()) {
case json::value_t::array:
return std::all_of(json_->begin(), json_->end(), [&](const auto &j) {
return JsonReader{&j}.read_flat(result);
});
case json::value_t::null:
return false;
default:
read(result.emplace_back());
return true;
}
}
bool JsonReader::parse_time_stamp_(const string &str, TimeStamp &value) const {
return parse_iso_time_stamp_(str, value) ||
parse_epoch_nanos_time_stamp_(str, value);
}
bool JsonReader::parse_iso_time_stamp_(
const string &str, TimeStamp &value) const {
value = TimeStamp::from_iso_string(str);
return true;
}
bool JsonReader::parse_epoch_nanos_time_stamp_(
const string &str, TimeStamp &value) const {
uint64_t nanos = stoull(str);
value = TimeStamp{nanos};
return true;
}
bool JsonReader::read(TimeStamp &result) const {
BEGIN_READ_JSON {
switch (json_->type()) {
case nlohmann::json::value_t::null:
case nlohmann::json::value_t::object:
case nlohmann::json::value_t::array:
case nlohmann::json::value_t::boolean:
case nlohmann::json::value_t::binary:
case nlohmann::json::value_t::discarded:
return false;
case nlohmann::json::value_t::string: {
const auto str = json_->get<string>();
return parse_iso_time_stamp_(str, result) ||
parse_epoch_nanos_time_stamp_(str, result);
}
case nlohmann::json::value_t::number_integer:
case nlohmann::json::value_t::number_unsigned:
case nlohmann::json::value_t::number_float:
// Maybe we could assume seconds here, idk though...when in doubt, prob
// better to be cautious than cavalier
return false;
}
}
END_READ_JSON("TimeStamp")
}
bool JsonReader::read_iso(
const std::string &name, core::TimeStamp &result) const {
return JsonReader{json_, name}.read_iso(result);
}
bool JsonReader::read_iso(core::TimeStamp &result) const {
BEGIN_READ_JSON {
switch (json_->type()) {
case nlohmann::json::value_t::string: {
return parse_iso_time_stamp_(json_->get<string>(), result);
}
default:
return false;
}
}
END_READ_JSON("TimeStamp")
}
bool JsonReader::read_epoch_nanos(TimeStamp &value) const {
int64_t nanos;
if (!read(nanos)) {
return false;
}
value = TimeStamp::from_nanos(nanos);
return true;
}
bool JsonReader::read_epoch_micros(TimeStamp &value) const {
int64_t micros;
if (!read(micros)) {
return false;
}
value = TimeStamp::from_micros(micros);
return true;
}
bool JsonReader::read_epoch_millis(TimeStamp &value) const {
int64_t millis;
if (!read(millis)) {
return false;
}
value = TimeStamp::from_millis(millis);
return true;
}
bool JsonReader::read_optional_epoch_millis(
std::optional<TimeStamp> &result) const {
if (json_ == nullptr) {
return false;
}
if (json_->is_null()) {
result = std::nullopt;
return true;
}
return read_epoch_millis(result.emplace());
}
bool JsonReader::read_optional_epoch_millis(
const std::string &name, std::optional<TimeStamp> &result) const {
if (!json_ || !json_->is_object()) {
return false;
}
auto it = json_->find(name);
if (it == json_->end()) {
return false;
}
return JsonReader{&*it}.read_epoch_millis(result.emplace());
}
} // namespace bts::system::json
@@ -1,15 +1,50 @@
#pragma once
#include "bts-system/TimeStamp.h"
#include "bts-system/bts_assert.h"
#include "bts-system/logging.h"
#include "bts-system/overloaded.h"
#include <cstdint>
#include <nlohmann/json.hpp>
#include <optional>
#include <string>
#include <variant>
#include "bts-system/TimeStamp.h"
#include "bts-system/bts_assert.h"
#include "bts-system/logging.h"
namespace bts::system::json {
namespace bts::io {
#ifdef LOG_JSON_READ_ERRORS
#define JSON_READ_AS(json, field, type, dest) \
type dest; \
JsonReader{&json}.read(#field, dest)
#define JSON_READ_AS_ELSE(js, field, type, dest, else) \
type dest; \
if (!JsonReader{&js}.read(#field, dest)) { \
_ERROR( \
"{}. Failed to read {} field '{}'", string{#field}, \
string{__FUNCTION__}, system::json::JsonDumper::dump(js)); \
else; \
}
#else
#define JSON_READ_AS(json, field, type, dest) \
type dest; \
JsonReader{&json}.read(#field, dest);
#define JSON_READ_AS_ELSE(json, field, type, dest, else) \
type dest; \
if (!JsonReader{&json}.read(#field, dest)) { \
else; \
}
#endif
#define JSON_READ(json, field, type) JSON_READ_AS(json, field, type, field)
/**
* Reads the json fields. If the read fails, performs the 'else' action
*/
#define JSON_READ_ELSE(j, field, type, else) \
JSON_READ_AS_ELSE(j, field, type, field, else)
class JsonReader {
public:
@@ -100,6 +135,12 @@ class JsonReader {
template <class T>
bool read_or(const std::string &name, T &result, T default_value) const;
template <class T1, class T2>
int read_variant(
const std::string &name1,
const std::string &name2,
std::variant<T1, T2> &result);
private:
const nlohmann::json *json_;
@@ -288,6 +329,24 @@ bool JsonReader::read_flat(
return true;
}
template <class T1, class T2>
int JsonReader::read_variant(
const std::string &name1,
const std::string &name2,
std::variant<T1, T2> &result) {
auto it = json_->find(name1);
if (it != json_->end()) {
result = it->get<T1>();
return 1;
}
it = json_->find(name2);
if (it != json_->end()) {
result = it->get<T2>();
return 2;
}
return 0;
}
#undef CATCH_PARSE_ERRORS
} // namespace bts::io
} // namespace bts::system::json
@@ -0,0 +1,11 @@
#pragma once
// #if __cplusplus >= 201703L
#include <variant>
// Helper to visit std::variant
namespace bts {
template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template <class... Ts>
overloaded(Ts...) -> overloaded<Ts...>; // not needed as of C++20
} // namespace bts
// #endif
@@ -0,0 +1,30 @@
#pragma once
#include <cstdint>
#include <nlohmann/json.hpp>
#include <string>
#include <vector>
namespace bts::system::threading {
class ThreadConfig {
public:
std::string name_rx;
bool exclude;
std::vector<std::uint32_t> cores;
};
inline void to_json(
nlohmann ::json &nlohmann_json_j, const ThreadConfig &nlohmann_json_t) {
nlohmann_json_j["name_rx"] = nlohmann_json_t.name_rx;
nlohmann_json_j["exclude"] = nlohmann_json_t.exclude;
nlohmann_json_j["cores"] = nlohmann_json_t.cores;
}
inline void from_json(
const nlohmann ::json &nlohmann_json_j, ThreadConfig &nlohmann_json_t) {
nlohmann_json_j.at("name_rx").get_to(nlohmann_json_t.name_rx);
nlohmann_json_j.at("exclude").get_to(nlohmann_json_t.exclude);
nlohmann_json_j.at("cores").get_to(nlohmann_json_t.cores);
};
} // namespace bts::system::threading
@@ -0,0 +1,117 @@
#include "thread_manager.h"
#include "bts-system/bts_assert.h"
#include "bts-system/logging.h"
#include "bts-system/threading/thread_config.h"
#include <fmt/format.h>
#include <functional>
#include <pthread.h>
#include <regex>
#include <string>
#include <sys/prctl.h>
namespace bts::system::threading {
using std::function;
using std::string;
using std::uint32_t;
using std::vector;
ThreadConfig ThreadManager::get_thread_cfg(const string &name) {
for (const auto &cfg : this->threads) {
std::smatch match;
std::regex rgx{cfg.name_rx};
if (std::regex_search(name, match, rgx)) {
return cfg;
}
}
bts_assert_msg(
false, fmt::format("No configuration found for thread {}", name));
}
vector<uint32_t> ThreadManager::get_affinity(const string &name) {
vector<uint32_t> result;
auto cfg = get_thread_cfg(name);
if (cfg.exclude) {
auto total_cpus = sysconf(_SC_NPROCESSORS_ONLN);
auto &cores = cfg.cores;
for (int i = 0; i < total_cpus; i++) {
if (std::find(cores.begin(), cores.end(), i) == cores.end()) {
result.push_back(i);
}
}
} else {
for (const auto &core : cfg.cores) {
result.push_back(core);
}
}
return result;
}
void configure_thread(
const string &name, const ThreadConfig &cfg, pthread_t t) {
if (!cfg.cores.empty()) {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
if (cfg.exclude) {
auto total_cpus = sysconf(_SC_NPROCESSORS_ONLN);
for (int i = 0; i < total_cpus; i++) {
CPU_SET(i, &cpuset);
}
for (const auto &core : cfg.cores) {
CPU_CLR(core, &cpuset);
}
} else {
for (const auto &core : cfg.cores) {
CPU_SET(core, &cpuset);
}
}
int rc = pthread_setaffinity_np(t, sizeof(cpu_set_t), &cpuset);
if (rc != 0) {
LOG(Gen, Error,
"Could not set core affinity for {} on thread {}. Error "
"code={}",
name, cfg.name_rx, rc);
}
}
}
void ThreadManager::set_thread_name(const string &name) {
prctl(PR_SET_NAME, name.c_str(), 0, 0, 0);
}
void ThreadManager::configure_current_thread(const string &name) {
configure_thread(name, get_thread_cfg(name), pthread_self());
set_thread_name(name);
}
void ThreadManager::configure_current_thread(
const string &name, ThreadConfig const &cfg) {
configure_thread(name, cfg, pthread_self());
set_thread_name(name);
}
std::thread ThreadManager::make_thread_from_function(
const string &name, function<void()> const &c) {
auto result = std::thread([c, name]() {
set_thread_name(name);
c();
});
configure_thread(name, get_thread_cfg(name), result.native_handle());
return result;
}
std::thread ThreadManager::make_thread_from_function(
const string &name, function<void()> const &c, ThreadConfig const &cfg) {
auto result = std::thread([c, name]() {
set_thread_name(name);
c();
});
configure_thread(name, cfg, result.native_handle());
return result;
}
} // namespace bts::system::threading
@@ -0,0 +1,48 @@
#pragma once
#include "bts-system/threading/thread_config.h"
#include <functional>
#include <string>
#include <thread>
namespace bts::system::threading {
class ThreadManager {
public:
explicit ThreadManager(std::vector<ThreadConfig> threads);
std::thread make_thread_from_function(
std::string const &name,
std::function<void()> const &c,
ThreadConfig const &cfg);
std::thread make_thread_from_function(
std::string const &name, std::function<void()> const &c);
template <class C>
std::thread make_thread(std::string const &name, C callable) {
return make_thread_from_function(name, std::function<void()>(callable));
}
template <class C>
std::thread make_thread(
std::string const &name, C callable, ThreadConfig const &cfg) {
return make_thread_from_function(
name, std::function<void()>(callable), cfg);
}
void configure_current_thread(std::string const &name);
void configure_current_thread(std::string const &name, ThreadConfig const &);
ThreadConfig get_thread_cfg(std::string const &);
std::vector<uint32_t> get_affinity(std::string const &name);
private:
std::vector<ThreadConfig> threads;
static void set_thread_name(const std::string &name);
};
} // namespace bts::system::threading
@@ -0,0 +1,163 @@
#pragma once
#include <ostream>
#include <utility>
namespace bts::system {
template <class Tag, class ValueT>
class ValueWrapper {
public:
typedef ValueT Value;
constexpr ValueWrapper() = default;
constexpr explicit ValueWrapper(const ValueT &v);
constexpr explicit ValueWrapper(ValueT &&v);
constexpr ValueWrapper(const ValueWrapper &v) = default;
constexpr ValueWrapper(ValueWrapper &&v) = default;
ValueWrapper &operator=(ValueWrapper const &v) = default;
ValueWrapper &operator=(ValueWrapper &&v) = default;
constexpr const ValueT &value() const;
constexpr ValueT &value();
private:
ValueT v_;
};
template <class Tag, class ValueT>
constexpr bool operator==(
const ValueWrapper<Tag, ValueT> &lhs, const ValueWrapper<Tag, ValueT> &rhs);
template <class Tag, class ValueT>
constexpr bool operator!=(
const ValueWrapper<Tag, ValueT> &lhs, const ValueWrapper<Tag, ValueT> &rhs);
template <class Tag, class ValueT>
constexpr bool operator>(
const ValueWrapper<Tag, ValueT> &lhs, const ValueWrapper<Tag, ValueT> &rhs);
template <class Tag, class ValueT>
constexpr bool operator>=(
const ValueWrapper<Tag, ValueT> &lhs, const ValueWrapper<Tag, ValueT> &rhs);
template <class Tag, class ValueT>
constexpr bool operator<(
const ValueWrapper<Tag, ValueT> &lhs, const ValueWrapper<Tag, ValueT> &rhs);
template <class Tag, class ValueT>
constexpr bool operator<=(
const ValueWrapper<Tag, ValueT> &lhs, const ValueWrapper<Tag, ValueT> &rhs);
template <class Tag, class V>
std::ostream &operator<<(std::ostream &s, ValueWrapper<Tag, V> const &v);
} // namespace bts::system
namespace std {
template <typename Tag, typename Value>
struct hash<bts::system::ValueWrapper<Tag, Value>> {
std::size_t operator()(const bts::system::ValueWrapper<Tag, Value> &k) const;
};
} // namespace std
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
namespace bts::system {
template <class Tag, class ValueT>
constexpr ValueWrapper<Tag, ValueT>::ValueWrapper(const ValueT &v) : v_(v) {}
template <class Tag, class ValueT>
constexpr ValueWrapper<Tag, ValueT>::ValueWrapper(ValueT &&v)
: v_(std::move(v)) {}
template <class Tag, class ValueT>
constexpr ValueT const &ValueWrapper<Tag, ValueT>::value() const {
return v_;
}
template <class Tag, class ValueT>
constexpr ValueT &ValueWrapper<Tag, ValueT>::value() {
return v_;
}
#define BIN_OP(result_t, op) \
template <class Tag, class ValueT> \
constexpr result_t operator op( \
const ValueWrapper<Tag, ValueT> &lhs, \
const ValueWrapper<Tag, ValueT> &rhs) { \
return result_t(lhs.value() op rhs.value()); \
}
BIN_OP(bool, ==)
BIN_OP(bool, !=)
BIN_OP(bool, >)
BIN_OP(bool, >=)
BIN_OP(bool, <)
BIN_OP(bool, <=)
#undef BIN_OP
template <class Tag, class V>
V &operator*(ValueWrapper<Tag, V> &v);
template <class Tag, class V>
const V &operator*(const ValueWrapper<Tag, V> &v);
// template <class Tag, class V>
// constexpr V* operator->(ValueWrapper<Tag, V> &v) noexcept;
// template <class Tag, class V>
// constexpr const V* operator->(const ValueWrapper<Tag, V> &v) noexcept;
template <class Tag, class V>
std::ostream &operator<<(std::ostream &s, ValueWrapper<Tag, V> const &v);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
template <class Tag, class V>
V &operator*(ValueWrapper<Tag, V> &v) {
return v.value();
}
// template <class Tag, class V>
// constexpr V *operator->(ValueWrapper<Tag, V> &v) noexcept {
// return &v.value();
// }
// template <class Tag, class V>
// constexpr const V *operator->(const ValueWrapper<Tag, V> &v) noexcept {
// return &v.value();
// }
template <class Tag, class V>
const V &operator*(const ValueWrapper<Tag, V> &v) {
return v.value();
}
template <class Tag, class V>
std::ostream &operator<<(std::ostream &s, ValueWrapper<Tag, V> const &v) {
s << v.value();
return s;
}
} // namespace bts::system
namespace std {
template <typename Tag, typename Value>
std::size_t hash<bts::system::ValueWrapper<Tag, Value>>::operator()(
const bts::system::ValueWrapper<Tag, Value> &k) const {
return hash<Value>()(k.value());
}
} // namespace std
+18
View File
@@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.23)
project(bts-websocket)
file(
GLOB_RECURSE ${PROJECT_NAME}_Sources
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
${PROJECT_NAME}/*.cpp)
add_library(${PROJECT_NAME} ${${PROJECT_NAME}_Sources})
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(${PROJECT_NAME} PUBLIC bts-system bts-http curl)
FetchContent_MakeAvailable(curlpp)
FetchContent_GetProperties(
curlpp
SOURCE_DIR CURLPP_SOURCE_DIR
BINARY_DIR CURLPP_BINARY_DIR)
list(APPEND CMAKE_BUILD_RPATH ${curlpp_BINARY_DIR})
target_link_libraries(${PROJECT_NAME} PUBLIC crypto curlpp ssl)
@@ -0,0 +1,26 @@
#pragma once
#include "bts-system/value_wrapper.h"
#include <nlohmann/json.hpp>
#include <string>
namespace bts::websocket {
/**
* Represents an access token for a websocket server. To use it, the websocket
* client should include the following header in the handshake request:
* `Authorization:Bearer \p access_token`.
*/
using WebsocketAccessToken =
system::ValueWrapper<struct WebsocketAccessTokenTag, std::string>;
inline void from_json(const nlohmann::json &j, WebsocketAccessToken &t) {
*t = j.get<std::string>();
}
inline void to_json(nlohmann::json &j, const WebsocketAccessToken &t) {
j = *t;
}
} // namespace bts::websocket
@@ -0,0 +1,81 @@
#pragma once
#include "bts-http/oauth_request.h"
#include "bts-system/json/json_reader.h"
#include "bts-system/overloaded.h"
#include "bts-system/value_wrapper.h"
#include "bts-websocket/websocket_access_token.h"
#include "bts-websocket/websocket_authorization_type.h"
#include <nlohmann/json.hpp>
#include <stdexcept>
#include <string>
#include <variant>
namespace bts::websocket {
class WebsocketAuthorization {
public:
http::OauthRequest &oauth();
const http::OauthRequest &oauth() const;
WebsocketAccessToken &access_token();
const WebsocketAccessToken &access_token() const;
WebsocketAuthorizationType type() const;
friend void to_json(nlohmann::json &j, const WebsocketAuthorization &cfg);
friend void from_json(const nlohmann::json &j, WebsocketAuthorization &cfg);
private:
std::variant<WebsocketAccessToken, http::OauthRequest> value_;
};
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline http::OauthRequest &WebsocketAuthorization::oauth() {
return std::get<http::OauthRequest>(value_);
}
inline const http::OauthRequest &WebsocketAuthorization::oauth() const {
return std::get<http::OauthRequest>(value_);
}
inline WebsocketAccessToken &WebsocketAuthorization::access_token() {
return std::get<WebsocketAccessToken>(value_);
}
inline const WebsocketAccessToken &
WebsocketAuthorization::access_token() const {
return std::get<WebsocketAccessToken>(value_);
}
inline WebsocketAuthorizationType WebsocketAuthorization::type() const {
return std::visit(
overloaded{
[](const WebsocketAccessToken &) {
return WebsocketAuthorizationType::AccessToken;
},
[](const http::OauthRequest &) {
return WebsocketAuthorizationType::OauthRequest;
},
[]() { return WebsocketAuthorizationType::None; }},
value_);
}
inline void to_json(nlohmann ::json &j, const WebsocketAuthorization &c) {
std::visit(
overloaded{
[&](const WebsocketAccessToken &t) { j["access_token"] = *t; },
[&](const http::OauthRequest &o) { j["oauth"] = o; },
},
c.value_);
}
inline void from_json(const nlohmann ::json &j, WebsocketAuthorization &c) {
system::json::JsonReader jr{&j};
jr.read_variant("access_token", "oauth", c.value_);
};
} // namespace bts::websocket
@@ -0,0 +1,23 @@
#pragma once
#include "bts-system/FMT_IMPL.h"
#include "bts-system/enum_macros.h"
#include <fmt/format.h>
namespace bts::websocket {
// clang-format off
#define FOR_ALL_VALUES(per_value) \
per_value(WebsocketAuthorizationType, None) \
per_value(WebsocketAuthorizationType, AccessToken) \
per_value(WebsocketAuthorizationType, OauthRequest)
// clang-format on
ENUM_CLASS_DEF(WebsocketAuthorizationType, FOR_ALL_VALUES);
#undef FOR_ALL_VALUES
} // namespace bts::websocket
FMT_IMPL(bts::websocket::WebsocketAuthorizationType, t, "{}", t.name());
@@ -0,0 +1,273 @@
#include "websocket_client.h"
#include "bts-http/QueryParams.h"
#include "bts-http/URL.h"
#include "bts-http/curl_requestor.h"
#include "bts-http/http_client.h"
#include "bts-http/http_request.h"
#include "bts-http/http_response.h"
#include "bts-http/oauth_requestor.h"
#include "bts-http/oauth_response.h"
#include "bts-system/TimeSpan.h"
#include "bts-system/TimeStamp.h"
#include "bts-system/UNUSED.h"
#include "bts-system/bts_assert.h"
#include "bts-system/json/json_dumper.h"
#include "bts-system/json/json_reader.h"
#include "bts-websocket/websocket_access_token.h"
#include "bts-websocket/websocket_authorization_type.h"
#include "bts-websocket/websocket_client_config.h"
#include <atomic>
#include <curl/curl.h>
#include <curl/easy.h>
#include <fmt/format.h>
#include <exception>
#include <functional>
#include <iostream>
#include <nlohmann/json.hpp>
#include <optional>
#include <stdexcept>
#include <string>
#include <thread>
#include <type_traits>
#include <unordered_map>
#include <vector>
#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 {
using namespace std::chrono_literals;
using nlohmann::json;
using std::optional;
using std::string;
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;
// ws endpoint http://markets.api.uat.cmegroup.com/marketdatastream/v1
#define _ASSERT_MSGF(condition, ...) \
bts_assert_msgf(condition, "WebsocketClient::" __VA_ARGS__)
#define _LOG(level, ...) LOG(md, level, "WebsocketClient::" __VA_ARGS__)
#define _DIAG(...) _LOG(Diag, __VA_ARGS__)
#define _INFO(...) _LOG(Info, __VA_ARGS__)
#define _WARN(...) _LOG(Warn, __VA_ARGS__)
#define _ERROR(...) _LOG(Error, __VA_ARGS__)
struct WebsocketClient::PImpl {
WebsocketClientConfig cfg;
WebsocketppClient client;
WebsocketppClient::connection_ptr con;
std::optional<MessageHandler> message_handler;
bool client_is_open{false};
std::size_t sub_id_counter{0};
string server_name;
std::optional<std::thread> run_thread;
int client_id{client_id_counter++};
~PImpl() {
if (run_thread) {
run_thread->join();
}
}
void close(websocketpp::close::status::value code, std::string reason) {
_WARN("Closing connection {}", client_id);
websocketpp::lib::error_code ec;
client.close(con->get_handle(), code, reason, ec);
if (ec) {
_ERROR(
"Error while closing connection {}. {}", client_id, ec.message());
}
}
websocketpp::lib::error_code send_text(const string &text) {
websocketpp::lib::error_code ec;
_INFO("Sending message. {}", text);
client.send(con, text, websocketpp::frame::opcode::value::TEXT, ec);
return ec;
}
WebsocketAccessToken get_access_token() {
switch (cfg.auth.type().value) {
case websocket::WebsocketAuthorizationType::AccessToken:
return cfg.auth.access_token();
case websocket::WebsocketAuthorizationType::OauthRequest:
return WebsocketAccessToken{
http::OauthRequestor::request(cfg.auth.oauth()).access_token};
default:
return {};
}
}
/**
* Connects to the websocket server
*
* @param timeout The amount of time to (optionally) block thread waiting
* for connection
*/
void connect() {
const auto access_token = get_access_token();
_INFO(
"Connecting to server. client_id: {}; cfg:\n{}", client_id,
system::json::JsonDumper::dump(cfg));
try {
init_logging();
init_handlers();
// Get connection
websocketpp::lib::error_code ec;
con = client.get_connection(cfg.url, ec);
if (ec) {
_ERROR(
"Error connecting to server. client_id: {}. {}", client_id,
ec.message());
return;
}
// Add auth header (if non-empty)
if (!access_token.value().empty()) {
con->append_header("Authorization", "Bearer " + *access_token);
}
// Connect
client.connect(con);
_ASSERT_MSGF(
!run_thread,
"Connection thread has already been created. client_id: {}",
client_id);
run_thread = std::thread{[this]() {
_INFO("run_thread begin. client_id: {}", client_id);
client.run();
}};
// Wait for connect
if (cfg.timeout) {
wait_for_connect(*cfg.timeout);
}
_INFO("Connection opened. client_id: {}", client_id);
} catch (const websocketpp::exception &ex) {
_ERROR(
"Error connecting to server. client_id: {}. {}", client_id,
string{ex.what()});
}
}
bool wait_for_connect(core::TimeSpan timeout) {
auto start = core::TimeStamp::now();
// Wait for client to connect
while (!client_is_open) {
_INFO("{}", "Waiting for connection to open...");
std::this_thread::sleep_for(std::chrono::seconds(1));
auto t = core::TimeStamp::now() - start;
if (t > timeout) {
_INFO(
"Timed out waiting for connection after {} secs. client_id: "
"{}",
t.total_seconds(), client_id);
return false;
}
}
return true;
}
private:
static std::atomic_int client_id_counter;
auto on_tls_init(ConnectionHdl /*hdl*/) {
auto result =
websocketpp::lib::make_shared<SslContext>(SslContext::sslv23_client);
result->set_verify_mode(boost::asio::ssl::verify_none);
return result;
}
void init_handlers() {
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) {
auto con = client.get_con_from_hdl(hdl);
auto ec = con->get_ec();
if (ec) {
_ERROR(
"websocket failed with error. client_id: {}. {}", client_id,
string{ec.message()});
} else {
_INFO("websocket failed. client_id: {}", client_id);
}
});
}
void init_logging() {
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) {
auto con = client.get_con_from_hdl(hdl);
auto ec = con->get_ec();
if (ec) {
_ERROR("websocket opened with error. {}", string{ec.message()});
} else {
_INFO("{}", "websocket opened");
}
client_is_open = true;
}
void on_message(ConnectionHdl /*hdl*/, MessageTypePtr msg) {
if (!message_handler) {
_ERROR("on message. No handler.\n{}", msg->get_payload());
return;
}
_INFO("on message.\n{}", msg->get_payload());
(*message_handler)(msg->get_payload());
}
void on_close(ConnectionHdl hdl) {
client_is_open = false;
auto con = client.get_con_from_hdl(hdl);
auto ec = con->get_ec();
if (ec) {
_ERROR("websocket closed with error. {}", string{ec.message()});
} else {
_INFO("{}", string{"websocket closed"});
}
}
};
WebsocketClient::WebsocketClient(WebsocketClientConfig cfg)
: me_(std::make_unique<PImpl>()) {
me_->cfg = std::move(cfg);
}
WebsocketClient::~WebsocketClient() = default;
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() { me_->connect(); }
void WebsocketClient::send_text(const string &text) { me_->send_text(text); }
} // namespace bts::websocket
@@ -0,0 +1,42 @@
#pragma once
#include "bts-http/URL.h"
#include "bts-system/value_wrapper.h"
#include "bts-system/delegate.h"
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <cstddef>
#include <curl/curl.h>
#include <iostream>
#include <memory>
#include <nlohmann/json.hpp>
#include <string>
#include <vector>
#include <websocketpp/config/core_client.hpp>
#include <websocketpp/message_buffer/message.hpp>
namespace bts::websocket {
class WebsocketClientConfig;
class WebsocketClient {
public:
explicit WebsocketClient(WebsocketClientConfig cfg);
~WebsocketClient();
using MessageTypePtr = websocketpp::config::core_client::message_type::ptr;
using MessageHandler = system::Delegate<void(const std::string &)>;
void connect();
MessageHandler &message_handler();
void send_text(const std::string &text);
private:
struct PImpl;
std::unique_ptr<PImpl> me_;
};
} // namespace bts::websocket
@@ -0,0 +1,65 @@
#pragma once
#include <iostream>
#include <nlohmann/json.hpp>
#include <string>
#include <vector>
#include "bts-http/oauth_request.h"
#include "bts-system/json/json_reader.h"
#include "bts-system/threading/thread_config.h"
#include "bts-websocket/websocket_authorization.h"
namespace bts::websocket {
class WebsocketClientConfig {
public:
/**
* The websocket server url
*/
std::string url;
/**
* Authorization parameters for the websocket handshake
*/
WebsocketAuthorization auth;
/**
* Thread configuration for the websocket client's response handlers
*/
system::threading::ThreadConfig thread;
std::optional<core::TimeSpan> timeout;
};
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline void to_json(
nlohmann ::json &nlohmann_json_j,
const WebsocketClientConfig &nlohmann_json_t) {
nlohmann_json_j["url"] = nlohmann_json_t.url;
nlohmann_json_j["auth"] = nlohmann_json_t.auth;
nlohmann_json_j["thread"] = nlohmann_json_t.thread;
}
inline void from_json(
const nlohmann ::json &nlohmann_json_j,
WebsocketClientConfig &nlohmann_json_t) {
nlohmann_json_j.at("url").get_to(nlohmann_json_t.url);
nlohmann_json_j.at("auth").get_to(nlohmann_json_t.auth);
// nlohmann_json_j.at("thread").get_to(nlohmann_json_t.thread);
};
// inline void to_json(nlohmann::json &j, const WebsocketClientConfig &cfg) {
// j["url"] = cfg.url;
// j["thread"] = cfg.thread;
// j["auth"] = cfg.auth;
// }
// inline void from_json(const nlohmann::json &j, WebsocketClientConfig &cfg) {
// system::json::JsonReader jr{&j};
// jr.read("url", cfg.url);
// jr.read("thread", cfg.thread);
// jr.read("auth", cfg.auth);
// };
} // namespace bts::websocket