check pt: Before going to chicago
This commit is contained in:
1 parent
9edf1dbd1a
commit
aaf55ba65b
29 files changed
+1211
-3
No files matched your search
@@ -1,9 +1,11 @@
|
||||
#include "bts-cme-websocket/cme_message_handler.h"
|
||||
#include "bts-cme-websocket/cme_message_handler_proxy.h"
|
||||
#include "bts-cme-websocket/cme_message_logger.h"
|
||||
#include "bts-cme-websocket/cme_subscription_topic.h"
|
||||
#include "bts-cme-websocket/cme_websocket_session.h"
|
||||
#include "bts-cme-websocket/cme_websocket_session_config.h"
|
||||
#include "bts-cme-websocket/messages/cme_product_key.h"
|
||||
#include "bts-cme-websocket/messages/cme_trade_summary_payload.h"
|
||||
#include "bts-system/UNUSED.h"
|
||||
#include "bts-system/json/json_dumper.h"
|
||||
#include "bts-system/json/json_reader.h"
|
||||
@@ -20,7 +22,12 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using bts::system::json::JsonDumper;
|
||||
using bts::system::json::JsonReader;
|
||||
using bts::websocket::CmeMessageHandlerProxy;
|
||||
using bts::websocket::CmeMessageLogger;
|
||||
using bts::websocket::CmeTradeSummaryPayload;
|
||||
using bts::websocket::CmeWebsocketSession;
|
||||
using bts::websocket::CmeWebsocketSessionConfig;
|
||||
using CLI::App;
|
||||
using std::ifstream;
|
||||
@@ -36,6 +43,10 @@ static CmeWebsocketSessionConfig parse_config(const string &config_arg) {
|
||||
return cfg;
|
||||
}
|
||||
|
||||
static void on_trades(CmeTradeSummaryPayload payload) {
|
||||
LOG(Md, Info, "on_trades.\n{}", JsonDumper::dump(payload));
|
||||
}
|
||||
|
||||
static void run(const std::string &config_arg) {
|
||||
const auto cfg = parse_config(config_arg);
|
||||
|
||||
@@ -44,14 +55,16 @@ static void run(const std::string &config_arg) {
|
||||
~CurlInit() { curlpp::terminate(); }
|
||||
} _CURL_INIT;
|
||||
|
||||
bts::websocket::CmeMessageLogger cme_handler;
|
||||
bts::websocket::CmeWebsocketSession session{cfg};
|
||||
CmeMessageHandlerProxy<CmeTradeSummaryPayload> trade_handler;
|
||||
trade_handler.callback().bind<&on_trades>();
|
||||
// CmeMessageLogger cme_handler;
|
||||
CmeWebsocketSession session{cfg};
|
||||
session.start();
|
||||
session.subscribe(
|
||||
std::vector<bts::websocket::CmeProductKey>{{"XCME", "FUT", "BTC"}},
|
||||
std::vector<bts::websocket::CmeSubscriptionTopic>{
|
||||
bts::websocket::CmeSubscriptionTopic::TRADE_TICKER},
|
||||
&cme_handler);
|
||||
&trade_handler);
|
||||
static const int sleep_secs = 3;
|
||||
while (true) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(sleep_secs));
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
cmake_minimum_required(VERSION 3.23)
|
||||
project(bts-cme-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 bts-websocket)
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "bts-cme-websocket/messages/cme_message.h"
|
||||
#include "bts-system/delegate.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
class CmeMessageHandler {
|
||||
public:
|
||||
virtual ~CmeMessageHandler() = default;
|
||||
virtual void on_message(const CmeMessage &msg) = 0;
|
||||
};
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
|
||||
#include "bts-cme-websocket/cme_message_handler.h"
|
||||
#include "bts-cme-websocket/messages/cme_message.h"
|
||||
#include "bts-system/delegate.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
/**
|
||||
* CmeMessageHandler that provides a bindable callback to handle payloads
|
||||
* (parsed from json to \tparam T).
|
||||
*/
|
||||
template <class T = nlohmann::json>
|
||||
class CmeMessageHandlerProxy : public CmeMessageHandler {
|
||||
public:
|
||||
using Callback = system::Delegate<void(T)>;
|
||||
|
||||
constexpr Callback &callback();
|
||||
|
||||
void on_message(const CmeMessage &msg) override;
|
||||
|
||||
private:
|
||||
Callback callback_;
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
template <class T>
|
||||
constexpr typename CmeMessageHandlerProxy<T>::Callback &
|
||||
CmeMessageHandlerProxy<T>::callback() {
|
||||
return callback_;
|
||||
}
|
||||
|
||||
template <>
|
||||
inline void CmeMessageHandlerProxy<nlohmann::json>::on_message(
|
||||
const CmeMessage &msg) {
|
||||
callback_(msg);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline void CmeMessageHandlerProxy<T>::on_message(const CmeMessage &msg) {
|
||||
if (msg.payload) {
|
||||
callback_(msg.payload->get<T>());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "bts-cme-websocket/cme_message_handler.h"
|
||||
#include "bts-cme-websocket/messages/cme_message.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_message(const CmeMessage &msg) override {
|
||||
LOG(Md, Info, "CmeMessageLogger::on_message.\n{}",
|
||||
bts::system::json::JsonDumper::dump(msg));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "bts-system/value_wrapper.h"
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
using CmeSubscriptionId =
|
||||
system::ValueWrapper<struct CmeSubscriptionTag, std::uint32_t>;
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,73 @@
|
||||
#pragma once
|
||||
|
||||
#include "bts-system/FMT_IMPL.h"
|
||||
#include "bts-system/HASH_IMPL.h"
|
||||
#include "bts-system/enum_macros.h"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
// clang-format off
|
||||
#define FOR_ALL_VALUES(per_value) \
|
||||
per_value(CmeSubscriptionTopic, NONE) \
|
||||
per_value(CmeSubscriptionTopic, TRADE_TICKER) \
|
||||
per_value(CmeSubscriptionTopic, TOP_OF_BOOK) \
|
||||
per_value(CmeSubscriptionTopic, STAT)
|
||||
// clang-format on
|
||||
|
||||
/**
|
||||
* Represents subscription topics (message types) at Cme. The names aren't
|
||||
* verbatim matches with the subscriptionMessageType fields they represent in
|
||||
* the websocket request json. See CmeWebsocketRequestFactory for details.
|
||||
*/
|
||||
ENUM_CLASS_DEF(CmeSubscriptionTopic, FOR_ALL_VALUES)
|
||||
|
||||
inline std::string topic_outgoing_name(CmeSubscriptionTopic topic) {
|
||||
switch (topic.value) {
|
||||
case CmeSubscriptionTopic::NONE:
|
||||
return {};
|
||||
case CmeSubscriptionTopic::TRADE_TICKER:
|
||||
return "TRD";
|
||||
case CmeSubscriptionTopic::TOP_OF_BOOK:
|
||||
return "TOB";
|
||||
default:
|
||||
return topic.name();
|
||||
}
|
||||
}
|
||||
|
||||
inline std::string topic_incoming_name(CmeSubscriptionTopic topic) {
|
||||
switch (topic.value) {
|
||||
case CmeSubscriptionTopic::NONE:
|
||||
return {};
|
||||
case CmeSubscriptionTopic::TRADE_TICKER:
|
||||
return "TS";
|
||||
case CmeSubscriptionTopic::TOP_OF_BOOK:
|
||||
return "OBTS";
|
||||
default:
|
||||
return topic.name();
|
||||
}
|
||||
}
|
||||
|
||||
#undef FOR_ALL_VALUES
|
||||
|
||||
inline void to_json(nlohmann::json &j, const CmeSubscriptionTopic &t) {
|
||||
j = t.name();
|
||||
}
|
||||
|
||||
inline void from_json(const nlohmann::json &j, CmeSubscriptionTopic &t) {
|
||||
if (!CmeSubscriptionTopic::parse(j.get<std::string>(), &t)) {
|
||||
throw std::runtime_error{fmt::format(
|
||||
"Failed to parse CmeSubscriptionTopic from '{}'", j.dump())};
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace bts::websocket
|
||||
|
||||
FMT_IMPL(bts::websocket::CmeSubscriptionTopic, t, "{}", t.name());
|
||||
|
||||
#define FOR_HASH_MEMBERS(op) op(value)
|
||||
|
||||
HASH_IMPL(bts::websocket::CmeSubscriptionTopic, FOR_HASH_MEMBERS);
|
||||
|
||||
#undef FOR_HASH_MEMBERS
|
||||
@@ -0,0 +1,205 @@
|
||||
#include "cme_websocket_session.h"
|
||||
#include "bts-cme-websocket/cme_message_handler.h"
|
||||
#include "bts-cme-websocket/cme_subscription_id.h"
|
||||
#include "bts-cme-websocket/cme_subscription_topic.h"
|
||||
#include "bts-cme-websocket/cme_websocket_session_config.h"
|
||||
#include "bts-cme-websocket/messages/cme_message.h"
|
||||
#include "bts-cme-websocket/messages/cme_message_header.h"
|
||||
#include "bts-cme-websocket/messages/cme_product_key.h"
|
||||
#include "bts-cme-websocket/messages/cme_trade_summary.h"
|
||||
#include "bts-cme-websocket/messages/cme_trade_summary_payload.h"
|
||||
#include "bts-cme-websocket/messages/cme_websocket_request.h"
|
||||
#include "bts-cme-websocket/messages/cme_websocket_request_factory.h"
|
||||
#include "bts-cme-websocket/messages/top_of_book.h"
|
||||
#include "bts-cme-websocket/ssl_certificate_verifier.h"
|
||||
#include "bts-cme-websocket/tls_initializer.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 nlohmann::json;
|
||||
using std::optional;
|
||||
using std::pair;
|
||||
using std::shared_ptr;
|
||||
using std::string;
|
||||
using std::unordered_map;
|
||||
using std::vector;
|
||||
using system::json::JsonDumper;
|
||||
|
||||
using SslContext = boost::asio::ssl::context;
|
||||
|
||||
// ws endpoint http://markets.api.uat.cmegroup.com/marketdatastream/v1
|
||||
|
||||
#define _ASSERT_MSGF(condition, ...) \
|
||||
bts_assert_msgf(condition, "CmeWebsocketSession::" __VA_ARGS__)
|
||||
#define _LOG(level, ...) LOG(md, level, "CmeWebsocketSession::" __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;
|
||||
CmeSubscriptionId sub_id_counter{0};
|
||||
string server_name;
|
||||
std::optional<std::thread> run_thread;
|
||||
int session_id{session_id_counter_++};
|
||||
CmeWebsocketRequestFactory req_factory_;
|
||||
std::unordered_map<string, std::vector<std::shared_ptr<CmeMessageHandler>>>
|
||||
topic_handlers;
|
||||
|
||||
~PImpl() {
|
||||
if (run_thread) {
|
||||
run_thread->join();
|
||||
}
|
||||
}
|
||||
|
||||
void on_login_status(const CmeMessage &msg) {
|
||||
_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(msg));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void on_subscription_status(const CmeMessage &msg) {
|
||||
// If there is an error, log it and return.
|
||||
_INFO("on_subscription_status:\n{}", JsonDumper::dump(msg));
|
||||
if (msg.payload->at("status").get<string>() != "subscribed") {
|
||||
_ERROR(
|
||||
"on_subscription_status. status not subscribed:\n{}",
|
||||
JsonDumper::dump(msg));
|
||||
}
|
||||
}
|
||||
|
||||
void on_heartbeat(const CmeMessage & /*msg*/) {
|
||||
// Placeholder for hearbeat handler
|
||||
}
|
||||
|
||||
void 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;
|
||||
}
|
||||
if (msg.header.messageType == "heartbeat") {
|
||||
on_heartbeat(msg);
|
||||
return;
|
||||
}
|
||||
if (msg.header.messageType == "loginStatus") {
|
||||
on_login_status(msg);
|
||||
return;
|
||||
}
|
||||
if (msg.header.messageType == "subscriptionStatus") {
|
||||
on_subscription_status(msg);
|
||||
return;
|
||||
}
|
||||
auto hnds = topic_handlers.find(msg.header.messageType);
|
||||
if (hnds != topic_handlers.end()) {
|
||||
for (auto &hnd : hnds->second) {
|
||||
hnd->on_message(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
static std::atomic<int> session_id_counter_;
|
||||
};
|
||||
|
||||
CmeWebsocketSession::CmeWebsocketSession(CmeWebsocketSessionConfig cfg)
|
||||
: me_(std::make_unique<PImpl>(std::move(cfg))) {
|
||||
me_->client.message_handler().bind<&PImpl::on_message>(*me_);
|
||||
}
|
||||
|
||||
CmeWebsocketSession::~CmeWebsocketSession() = default;
|
||||
|
||||
std::atomic_int CmeWebsocketSession::PImpl::session_id_counter_ = 0;
|
||||
|
||||
void CmeWebsocketSession::start() { me_->client.connect(); }
|
||||
|
||||
std::string CmeWebsocketSession::subscribe(
|
||||
std::vector<
|
||||
std::tuple<CmeProductKey, CmeSubscriptionTopic, MessageHandlerPtr>>
|
||||
topic_handlers) {
|
||||
vector<CmeProductKey> products;
|
||||
vector<CmeSubscriptionTopic> topics;
|
||||
for (auto &[p, t, h] : topic_handlers) {
|
||||
if (std::find(products.begin(), products.end(), p) == products.end()) {
|
||||
products.push_back(p);
|
||||
}
|
||||
if (std::find(topics.begin(), topics.end(), t) == topics.end()) {
|
||||
topics.push_back(t);
|
||||
}
|
||||
me_->topic_handlers[topic_incoming_name(t)].push_back(h);
|
||||
}
|
||||
auto cme_req = me_->req_factory_.subscription(products, topics);
|
||||
if (!me_->client.is_connected()) {
|
||||
throw std::runtime_error{
|
||||
"Unable to add subscription because client is not connected."};
|
||||
}
|
||||
me_->client.send_text(JsonDumper::dump(sub_it->second.request));
|
||||
return *sub_it->second.request.header.requestId;
|
||||
}
|
||||
|
||||
bool CmeWebsocketSession::unsubscribe(const string &id) {
|
||||
// TODO
|
||||
auto it = me_->topic_handlers.find(id);
|
||||
if (it == me_->subscriptions.end()) {
|
||||
return false;
|
||||
}
|
||||
CmeWebsocketRequest req = it->second.request;
|
||||
req.header.messageType = "unsubscribe";
|
||||
req.header.sentTime = core::TimeStamp::now();
|
||||
me_->subscriptions.erase(it);
|
||||
me_->client.send_text(JsonDumper::dump(req));
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
|
||||
#include "bts-cme-websocket/cme_message_handler.h"
|
||||
#include "bts-cme-websocket/cme_subscription_id.h"
|
||||
#include "bts-cme-websocket/cme_subscription_topic.h"
|
||||
#include "bts-cme-websocket/messages/cme_product_key.h"
|
||||
#include "bts-cme-websocket/messages/cme_websocket_request.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();
|
||||
|
||||
using MessageHandlerPtr = std::shared_ptr<CmeMessageHandler>;
|
||||
|
||||
/**
|
||||
* Subscribes to topics for the specified products (a handler is
|
||||
* paired with each topic).
|
||||
*/
|
||||
void subscribe(
|
||||
std::vector<
|
||||
std::tuple<CmeProductKey, CmeSubscriptionTopic, MessageHandlerPtr>>
|
||||
topic_handlers);
|
||||
|
||||
bool unsubscribe(
|
||||
std::vector<
|
||||
std::tuple<CmeProductKey, CmeSubscriptionTopic, MessageHandlerPtr>>
|
||||
topic_handlers);
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> me_;
|
||||
};
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include "bts-cme-websocket/cme_subscription_topic.h"
|
||||
#include "bts-cme-websocket/messages/cme_product_key.h"
|
||||
#include "bts-cme-websocket/messages/cme_websocket_request.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 <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
class CmeWebsocketSessionConfig {
|
||||
public:
|
||||
struct SubscriptionConfig {
|
||||
std::vector<CmeSubscriptionTopic> topics;
|
||||
std::vector<CmeProductKey> products;
|
||||
};
|
||||
|
||||
// Needed to obtain the access token (ignored if access token is non-empty)
|
||||
websocket::WebsocketClientConfig websocket;
|
||||
std::vector<SubscriptionConfig> subscriptions;
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
inline void to_json(
|
||||
nlohmann::json &j,
|
||||
const CmeWebsocketSessionConfig::SubscriptionConfig &cfg) {
|
||||
j["topics"] = cfg.topics;
|
||||
j["products"] = cfg.products;
|
||||
}
|
||||
|
||||
inline void from_json(
|
||||
const nlohmann::json &j,
|
||||
CmeWebsocketSessionConfig::SubscriptionConfig &cfg) {
|
||||
system::json::JsonReader jr{&j};
|
||||
jr.read("topics", cfg.topics);
|
||||
jr.read("products", cfg.products);
|
||||
};
|
||||
|
||||
inline void to_json(nlohmann::json &j, const CmeWebsocketSessionConfig &cfg) {
|
||||
j["websocket"] = cfg.websocket;
|
||||
j["subscriptions"] = cfg.subscriptions;
|
||||
}
|
||||
|
||||
inline void from_json(const nlohmann::json &j, CmeWebsocketSessionConfig &cfg) {
|
||||
system::json::JsonReader jr{&j};
|
||||
jr.read("websocket", cfg.websocket);
|
||||
jr.read("subscriptions", cfg.subscriptions);
|
||||
};
|
||||
|
||||
} // 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,35 @@
|
||||
#pragma once
|
||||
|
||||
#include "bts-cme-websocket/messages/cme_message_header.h"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
template <class Payload = nlohmann::json>
|
||||
class CmeMessageT {
|
||||
public:
|
||||
CmeMessageHeader header;
|
||||
std::optional<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;
|
||||
if (msg.payload) {
|
||||
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);
|
||||
auto payload = j.find("payload");
|
||||
if (payload != j.end()) {
|
||||
payload->get_to(msg.payload.emplace());
|
||||
}
|
||||
};
|
||||
|
||||
} // 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/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_extended_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,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
class CmeProductKey {
|
||||
public:
|
||||
std::string exchangeMic;
|
||||
std::string productType;
|
||||
std::string productCode;
|
||||
};
|
||||
|
||||
inline bool operator==(const CmeProductKey &lhs, const CmeProductKey &rhs) {
|
||||
return lhs.exchangeMic == rhs.exchangeMic &&
|
||||
lhs.productType == rhs.productType &&
|
||||
lhs.productCode == rhs.productCode;
|
||||
}
|
||||
|
||||
inline bool operator!=(const CmeProductKey &lhs, const CmeProductKey &rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
|
||||
CmeProductKey, exchangeMic, productType, productCode);
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
class CmeTradeSummary {
|
||||
public:
|
||||
std::int64_t aggressorSide;
|
||||
std::int64_t mdTradeEntryId;
|
||||
double tradePrice;
|
||||
double tradeQty;
|
||||
std::uint32_t tradeOrderCount;
|
||||
std::string tradeUpdateAction;
|
||||
};
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
|
||||
CmeTradeSummary,
|
||||
aggressorSide,
|
||||
mdTradeEntryId,
|
||||
tradePrice,
|
||||
tradeQty,
|
||||
tradeOrderCount,
|
||||
tradeUpdateAction)
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "bts-cme-websocket/messages/cme_trade_summary.h"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
class CmeTradeSummaryPayload {
|
||||
public:
|
||||
std::int64_t lastUpdateTime;
|
||||
CmeTradeSummary tradeSummary;
|
||||
};
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
|
||||
CmeTradeSummaryPayload, lastUpdateTime, tradeSummary)
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "bts-cme-websocket/messages/cme_message.h"
|
||||
#include "bts-cme-websocket/messages/cme_websocket_request_payload.h"
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
using CmeWebsocketRequest = CmeMessageT<CmeWebsocketRequestPayload>;
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
|
||||
#include "bts-cme-websocket/cme_subscription_topic.h"
|
||||
#include "bts-cme-websocket/messages/cme_message_header.h"
|
||||
#include "bts-cme-websocket/messages/cme_product_key.h"
|
||||
#include "bts-cme-websocket/messages/cme_websocket_request.h"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
class CmeWebsocketRequestFactory {
|
||||
public:
|
||||
CmeWebsocketRequest subscription(
|
||||
const std::vector<CmeProductKey> &subscriptions,
|
||||
const std::vector<CmeSubscriptionTopic> &topics);
|
||||
|
||||
CmeWebsocketRequest unsubscription(
|
||||
const std::vector<CmeProductKey> &subscriptions,
|
||||
const std::vector<CmeSubscriptionTopic> &topics);
|
||||
|
||||
private:
|
||||
// Ensure unique ids for each request
|
||||
std::atomic<std::uint32_t> id_counter_;
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
inline CmeWebsocketRequest CmeWebsocketRequestFactory::subscription(
|
||||
const std::vector<CmeProductKey> &products,
|
||||
const std::vector<CmeSubscriptionTopic> &topics) {
|
||||
CmeWebsocketRequest sub;
|
||||
sub.payload.emplace();
|
||||
sub.payload->subscriptions = std::move(products);
|
||||
sub.payload->subscriptionMessageTypes.reserve(topics.size());
|
||||
for (const auto t : topics) {
|
||||
sub.payload->subscriptionMessageTypes.push_back(topic_outgoing_name(t));
|
||||
}
|
||||
sub.header.requestId = std::to_string(id_counter_++);
|
||||
sub.header.messageType = "subscribe";
|
||||
sub.header.sentTime = core::TimeStamp::now();
|
||||
return sub;
|
||||
}
|
||||
|
||||
inline CmeWebsocketRequest CmeWebsocketRequestFactory::unsubscription(
|
||||
const std::vector<CmeProductKey> &products,
|
||||
const std::vector<CmeSubscriptionTopic> &topics) {
|
||||
CmeWebsocketRequest sub;
|
||||
sub.payload.emplace();
|
||||
sub.payload->subscriptions = std::move(products);
|
||||
sub.payload->subscriptionMessageTypes.resize(topics.size());
|
||||
for (const auto t : topics) {
|
||||
sub.payload->subscriptionMessageTypes.push_back(topic_outgoing_name(t));
|
||||
}
|
||||
sub.header.requestId = std::to_string(id_counter_++);
|
||||
sub.header.messageType = "unsubscribe";
|
||||
sub.header.sentTime = core::TimeStamp::now();
|
||||
return sub;
|
||||
}
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "bts-cme-websocket/cme_subscription_topic.h"
|
||||
#include "bts-cme-websocket/messages/cme_product_key.h"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
struct CmeWebsocketRequestPayload {
|
||||
std::vector<CmeProductKey> subscriptions;
|
||||
std::vector<std::string> subscriptionMessageTypes;
|
||||
};
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
|
||||
CmeWebsocketRequestPayload, subscriptions, subscriptionMessageTypes);
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <nlohmann/detail/macro_scope.hpp>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
class PrivateOrderQty {
|
||||
public:
|
||||
std::int64_t orderId;
|
||||
double lastOrderQty;
|
||||
};
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(PrivateOrderQty, orderId, lastOrderQty)
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include "bts-cme-websocket/messages/cme_book_level.h"
|
||||
#include "bts-cme-websocket/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
|
||||
@@ -0,0 +1,120 @@
|
||||
#include "ssl_certificate_verifier.h"
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
/// Verify that the certificate common name matches the given hostname
|
||||
bool SslCertificateVerifier::verify_common_name(char const *hostname, X509 *cert) {
|
||||
// Find the position of the CN field in the Subject field of the certificate
|
||||
int common_name_loc = X509_NAME_get_index_by_NID(
|
||||
X509_get_subject_name(cert), NID_commonName, -1);
|
||||
if (common_name_loc < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Extract the CN field
|
||||
X509_NAME_ENTRY *common_name_entry =
|
||||
X509_NAME_get_entry(X509_get_subject_name(cert), common_name_loc);
|
||||
if (common_name_entry == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Convert the CN field to a C string
|
||||
ASN1_STRING *common_name_asn1 = X509_NAME_ENTRY_get_data(common_name_entry);
|
||||
if (common_name_asn1 == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
char const *common_name_str =
|
||||
(char const *)ASN1_STRING_get0_data(common_name_asn1);
|
||||
|
||||
// Make sure there isn't an embedded NUL character in the CN
|
||||
if (static_cast<size_t>(ASN1_STRING_length(common_name_asn1)) !=
|
||||
strlen(common_name_str)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Compare expected hostname with the CN
|
||||
return (strcasecmp(hostname, common_name_str) == 0);
|
||||
}
|
||||
|
||||
/// Verify that one of the subject alternative names matches the given hostname
|
||||
bool SslCertificateVerifier::verify_subject_alternative_name(
|
||||
const char *hostname, X509 *cert) {
|
||||
STACK_OF(GENERAL_NAME) *san_names = NULL;
|
||||
|
||||
san_names = (STACK_OF(GENERAL_NAME) *)X509_get_ext_d2i(
|
||||
cert, NID_subject_alt_name, NULL, NULL);
|
||||
if (san_names == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int san_names_count = sk_GENERAL_NAME_num(san_names);
|
||||
|
||||
bool result = false;
|
||||
|
||||
for (int i = 0; i < san_names_count; i++) {
|
||||
const GENERAL_NAME *current_name = sk_GENERAL_NAME_value(san_names, i);
|
||||
|
||||
if (current_name->type != GEN_DNS) {
|
||||
continue;
|
||||
}
|
||||
|
||||
char const *dns_name =
|
||||
(char const *)ASN1_STRING_get0_data(current_name->d.dNSName);
|
||||
|
||||
// Make sure there isn't an embedded NUL character in the DNS name
|
||||
if (static_cast<size_t>(ASN1_STRING_length(current_name->d.dNSName)) !=
|
||||
strlen(dns_name)) {
|
||||
break;
|
||||
}
|
||||
// Compare expected hostname with the CN
|
||||
result = (strcasecmp(hostname, dns_name) == 0);
|
||||
}
|
||||
sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* This code is derived from examples and documentation found ato00po
|
||||
* http://www.boost.org/doc/libs/1_61_0/doc/html/boost_asio/example/cpp03/ssl/client.cpp
|
||||
* and
|
||||
* https://github.com/iSECPartners/ssl-conservatory
|
||||
*/
|
||||
bool SslCertificateVerifier::verify_certificate(
|
||||
const char *hostname,
|
||||
bool preverified,
|
||||
boost::asio::ssl::verify_context &ctx) {
|
||||
// The verify callback can be used to check whether the certificate that is
|
||||
// being presented is valid for the peer. For example, RFC 2818 describes
|
||||
// the steps involved in doing this for HTTPS. Consult the OpenSSL
|
||||
// documentation for more details. Note that the callback is called once
|
||||
// for each certificate in the certificate chain, starting from the root
|
||||
// certificate authority.
|
||||
|
||||
// Retrieve the depth of the current cert in the chain. 0 indicates the
|
||||
// actual server cert, upon which we will perform extra validation
|
||||
// (specifically, ensuring that the hostname matches. For other certs we
|
||||
// will use the 'preverified' flag from Asio, which incorporates a number of
|
||||
// non-implementation specific OpenSSL checking, such as the formatting of
|
||||
// certs and the trusted status based on the CA certs we imported earlier.
|
||||
int depth = X509_STORE_CTX_get_error_depth(ctx.native_handle());
|
||||
|
||||
// if we are on the final cert and everything else checks out, ensure that
|
||||
// the hostname is present on the list of SANs or the common name (CN).
|
||||
if (depth == 0 && preverified) {
|
||||
X509 *cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
|
||||
|
||||
if (verify_subject_alternative_name(hostname, cert)) {
|
||||
return true;
|
||||
} else if (verify_common_name(hostname, cert)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return preverified;
|
||||
}
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <websocketpp/common/connection_hdl.hpp>
|
||||
#include <websocketpp/config/asio_client.hpp>
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
class SslCertificateVerifier {
|
||||
public:
|
||||
static bool verify_common_name(char const *hostname, X509 *cert);
|
||||
|
||||
static bool verify_subject_alternative_name(
|
||||
const char *hostname, X509 *cert);
|
||||
|
||||
static bool verify_certificate(
|
||||
const char *hostname,
|
||||
bool preverified,
|
||||
boost::asio::ssl::verify_context &ctx);
|
||||
};
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,42 @@
|
||||
#include "tls_initializer.h"
|
||||
#include "bts-cme-websocket/ssl_certificate_verifier.h"
|
||||
#include <websocketpp/common/connection_hdl.hpp>
|
||||
#include <websocketpp/config/asio_client.hpp>
|
||||
#include <websocketpp/roles/client_endpoint.hpp>
|
||||
|
||||
typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
|
||||
typedef websocketpp::lib::shared_ptr<websocketpp::lib::asio::ssl::context>
|
||||
context_ptr;
|
||||
|
||||
using websocketpp::lib::bind;
|
||||
using websocketpp::lib::placeholders::_1;
|
||||
using websocketpp::lib::placeholders::_2;
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
context_ptr TlsInitializer::init(
|
||||
const char *hostname, websocketpp::connection_hdl) {
|
||||
context_ptr ctx = websocketpp::lib::make_shared<boost::asio::ssl::context>(
|
||||
boost::asio::ssl::context::sslv23);
|
||||
|
||||
try {
|
||||
ctx->set_options(
|
||||
boost::asio::ssl::context::default_workarounds |
|
||||
boost::asio::ssl::context::no_sslv2 |
|
||||
boost::asio::ssl::context::no_sslv3 |
|
||||
boost::asio::ssl::context::single_dh_use);
|
||||
|
||||
// TODO: actually verify ssl certificate!
|
||||
ctx->set_verify_mode(boost::asio::ssl::verify_none);
|
||||
ctx->set_verify_callback(bind(
|
||||
&SslCertificateVerifier::verify_certificate, hostname, ::_1, ::_2));
|
||||
|
||||
// Here we load the CA certificates of all CA's that this client trusts.
|
||||
// ctx->load_verify_file("ca-chain.cert.pem");
|
||||
} catch (std::exception &e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <websocketpp/common/connection_hdl.hpp>
|
||||
#include <websocketpp/config/asio_client.hpp>
|
||||
#include <websocketpp/roles/client_endpoint.hpp>
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
class TlsInitializer {
|
||||
public:
|
||||
static websocketpp::lib::shared_ptr<websocketpp::lib::asio::ssl::context>
|
||||
init(const char *hostname, websocketpp::connection_hdl hdl);
|
||||
};
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "bts-system/HashCombiner.h"
|
||||
|
||||
#define _HASH_ARG(x) , std::hash<decltype(t.x)>{}(t.x)
|
||||
|
||||
#define HASH_IMPL(type, for_all) \
|
||||
template <> \
|
||||
struct std::hash<type> { \
|
||||
std::size_t operator()(const type &t) const noexcept { \
|
||||
std::size_t rv = 0; \
|
||||
bts::collections::HashCombiner::combine_hash(rv for_all(_HASH_ARG)); \
|
||||
return rv; \
|
||||
} \
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <boost/container_hash/hash.hpp>
|
||||
#include <cstddef>
|
||||
|
||||
namespace bts::collections {
|
||||
class HashCombiner {
|
||||
public:
|
||||
template <class T>
|
||||
static void combine_hash(std::size_t &seed, T arg);
|
||||
|
||||
template <class T, class... T2>
|
||||
static void combine_hash(std::size_t &seed, T arg, T2... next);
|
||||
};
|
||||
|
||||
template <class T>
|
||||
void HashCombiner::combine_hash(std::size_t &seed, T arg) {
|
||||
boost::hash_combine(seed, arg);
|
||||
}
|
||||
|
||||
template <class T, class... T2>
|
||||
void HashCombiner::combine_hash(std::size_t &seed, T arg, T2... next) {
|
||||
boost::hash_combine(seed, arg);
|
||||
combine_hash(seed, next...);
|
||||
}
|
||||
|
||||
} // namespace bts::collections
|
||||
Reference in new issue
Block a user