diff --git a/src/lib/md/emumba/MarketDataCommon.h b/src/lib/md/emumba/MarketDataCommon.h index 29290ec03..e4b48ad72 100644 --- a/src/lib/md/emumba/MarketDataCommon.h +++ b/src/lib/md/emumba/MarketDataCommon.h @@ -48,7 +48,9 @@ using MarketDataListenerID = ValueWrapper; // more types will be needed there like spots etc -enum class SecurityType { FUTURE, SPREAD, OPTION }; +enum class SecurityType { INDEX, FUTURE, SPREAD, OPTION }; + +enum class Currency { BTC, EH, USD }; // Usually exchanges give price's as mantissa and exponent. The fastest way is // to keep it like that and do the math in integers vs doubles (I can give you a @@ -127,6 +129,7 @@ class SecurityCommon { Venue venue; SecurityID _security_id{}; SecurityType security_type; + Currency currency; double display_factor{}; double cash_per_point{}; std::string symbol; diff --git a/src/lib/md/websockets/WebsocketClient.cpp b/src/lib/md/websockets/WebsocketClient.cpp new file mode 100644 index 000000000..a716ac3a1 --- /dev/null +++ b/src/lib/md/websockets/WebsocketClient.cpp @@ -0,0 +1 @@ +#include "WebsocketClient.h" \ No newline at end of file diff --git a/src/lib/md/websockets/WebsocketClient.h b/src/lib/md/websockets/WebsocketClient.h new file mode 100644 index 000000000..df5c5e6d3 --- /dev/null +++ b/src/lib/md/websockets/WebsocketClient.h @@ -0,0 +1,104 @@ +#pragma once + +// #include +// #include +// #include +#include +#include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// namespace beast = boost::beast; // from +// namespace http = beast::http; // from +// namespace websocket = beast::websocket; // from +// namespace net = boost::asio; // from +// using tcp = boost::asio::ip::tcp; // from + +namespace bts { +namespace md { + +// This lightweight wrapper type is used to exchange messages between the +// WebsocketClient it's user. Since it has a const* access to the data, it does +// NOT manage data's memory. +struct WebsocketMessage { + // The generators of the message controls the unique message_id + const std::uint64_t message_id; + // The actual data. This lifecycle of the memory is controlled by the + // generator of this message. The only assumption is that the memory is valid + // while this object is passed from the caller to the callee + char const *data; + const size_t data_size; +}; + +// All errors for now are irrecoverable. In case of an error the physical +// connection is destroyed, resources are collected and a new connection is +// attempted +struct WebsocketError { + enum class ErrorType { SETUP = 0, DISCONNECT, WRITE_FAILED, READ_FAILED }; + ErrorType error_type; + std::string message; +}; + +// This interface is passed to the WebsocketClient. +class IWebsocketListener { + public: + // This is called after a successful resoloution, connection and handshake. + // The first batch of data can be sent on this callback + virtual void on_connection_established() = 0; + // Called when an irrecoverable error happens. The WebsocketClient tries to + // reconnect after this. Any attempts to send_message are ignored until the + // connection is re-established + virtual void on_error(const WebsocketError &error) = 0; + virtual void on_write_complete(size_t message_id) = 0; + virtual void on_data(WebsocketMessage &msg) = 0; +}; + +struct WebsocketClientConfiguration { + const size_t msecs_bw_connection_tries = 5000; +}; + +// This is the main class the market data components (client to the exchange +// websockets) will use. The ip, port, other configurations, listener +// registration is done at construction time, followed by a blocking call to +// start. +class WebsocketClient : public IWebsocketListener { + public: + explicit WebsocketClient( + const std::string &ip, + const std::string &port, + WebsocketClientConfiguration conf, + IWebsocketListener *listener); + // this is a blocking call, that is called only once. + void start(); + // can be called on any IWebsocketListener callback + void send_message(const std::vector &msgs); +}; + +} // namespace md +} // namespace bts \ No newline at end of file diff --git a/src/lib/orderman/orders/spreader_orders/tests/spreader_tests.cpp b/src/lib/orderman/orders/spreader_orders/tests/spreader_tests.cpp index cc97147ac..da55ef6f8 100644 --- a/src/lib/orderman/orders/spreader_orders/tests/spreader_tests.cpp +++ b/src/lib/orderman/orders/spreader_orders/tests/spreader_tests.cpp @@ -1,6 +1,7 @@ #include "lib/md/emumba/IMarketDataListener.h" #include "lib/md/emumba/MarketDataCommon.h" #include "lib/md/emumba/MarketDataGateway.h" +#include "lib/md/websockets/WebsocketClient.h" #include "lib/orderman/orders/spreader_orders/tests/tests_common.h" #include #include