WebsocketClient

This commit is contained in:
Taha Afzal committed 2021-09-27 11:52:57 -05:00
1 parent 43c3584dd2
commit 930357d11c
4 files changed
+110 -1

No files matched your search

+4 -1
View File
@@ -48,7 +48,9 @@ using MarketDataListenerID =
ValueWrapper<struct MarketDataListenedIDTag, std::uint32_t>;
// 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;
@@ -0,0 +1 @@
#include "WebsocketClient.h"
+104
View File
@@ -0,0 +1,104 @@
#pragma once
// #include <atomic>
// #include <bits/stdint-intn.h>
// #include <bits/stdint-uintn.h>
#include <boost/asio.hpp>
#include <boost/asio/buffer.hpp>
// #include <boost/asio/io_context.hpp>
// #include <boost/asio/ip/address.hpp>
// #include <boost/asio/ip/tcp.hpp>
// #include <boost/asio/strand.hpp>
// #include <boost/asio/ts/netfwd.hpp>
// #include <boost/beast/core.hpp>
// #include <boost/beast/core/buffers_to_string.hpp>
// #include <boost/beast/core/error.hpp>
// #include <boost/beast/websocket.hpp>
// #include <boost/bind/bind.hpp>
// #include <boost/bind/placeholders.hpp>
// #include <boost/core/addressof.hpp>
// #include <boost/date_time/posix_time/posix_time.hpp>
// #include <catch2/catch.hpp>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <memory.h>
#include <memory>
#include <queue>
#include <sstream>
#include <stdio.h>
#include <string>
#include <sys/types.h>
#include <thread>
#include <tuple>
#include <utility>
#include <vector>
// namespace beast = boost::beast; // from <boost/beast.hpp>
// namespace http = beast::http; // from <boost/beast/http.hpp>
// namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
// namespace net = boost::asio; // from <boost/asio.hpp>
// using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
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<WebsocketMessage> &msgs);
};
} // namespace md
} // namespace bts
@@ -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 <boost/container/flat_set.hpp>
#include <stdio.h>