Skip to content

Commit

Permalink
remove buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
flashnuke committed Oct 11, 2021
1 parent 45f0489 commit 4f1720a
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 202 deletions.
5 changes: 2 additions & 3 deletions examples/custom_ws_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ struct SomeFunctor
Json::CharReaderBuilder charbuilder;
Json::CharReader* charreader;
std::string parse_errors;
std::string msg_buffer;
Json::Value stream_msg;

SomeFunctor()
: msg_buffer{ "" }, parse_errors{ }, charreader{ charbuilder.newCharReader() }
: parse_errors{ }, charreader{ charbuilder.newCharReader() }
{}


Expand Down Expand Up @@ -40,7 +39,7 @@ int main()
SomeFunctor ws_stream_read{};

std::string cust_stream = "btcusdt@aggTrade/ethusdt@aggTrade";
std::thread t1(&SpotClient::custom_stream<SomeFunctor>, std::ref(my_client), cust_stream, std::ref(ws_stream_read.msg_buffer), std::ref(ws_stream_read), 0);
std::thread t1(&SpotClient::custom_stream<SomeFunctor>, std::ref(my_client), cust_stream, std::ref(ws_stream_read), 0);

t1.join();
}
Expand Down
5 changes: 2 additions & 3 deletions examples/futures_user_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ struct SomeFunctor
Json::CharReaderBuilder charbuilder;
Json::CharReader* charreader;
std::string parse_errors;
std::string msg_buffer;
Json::Value stream_msg;

SomeFunctor()
: msg_buffer{ "" }, parse_errors{ }, charreader{ charbuilder.newCharReader() }
: parse_errors{ }, charreader{ charbuilder.newCharReader() }
{}


Expand Down Expand Up @@ -43,7 +42,7 @@ int main()
FuturesClientUSDT my_client{ api_key, api_secret };
SomeFunctor ws_stream_read{};

std::thread t1(&FuturesClientUSDT::stream_userStream<SomeFunctor>, std::ref(my_client), std::ref(ws_stream_read.msg_buffer), std::ref(ws_stream_read), 1);
std::thread t1(&FuturesClientUSDT::stream_userStream<SomeFunctor>, std::ref(my_client), std::ref(ws_stream_read), 1);

t1.join();
}
Expand Down
5 changes: 2 additions & 3 deletions examples/options_trade_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ struct SomeFunctor
Json::CharReaderBuilder charbuilder;
Json::CharReader* charreader;
std::string parse_errors;
std::string msg_buffer;
Json::Value stream_msg;

SomeFunctor()
: msg_buffer{ "" }, parse_errors{ }, charreader{ charbuilder.newCharReader() }
: parse_errors{ }, charreader{ charbuilder.newCharReader() }
{}


Expand Down Expand Up @@ -40,7 +39,7 @@ int main()
SomeFunctor ws_stream_read{};

std::string symbol = "BTC-210430-56000-C";
std::thread t1(&OpsClient::stream_Trade<SomeFunctor>, std::ref(my_client), symbol, std::ref(ws_stream_read.msg_buffer), std::ref(ws_stream_read));
std::thread t1(&OpsClient::stream_Trade<SomeFunctor>, std::ref(my_client), symbol, std::ref(ws_stream_read));

t1.join();
}
Expand Down
5 changes: 2 additions & 3 deletions examples/orderbook_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class OrderbookManager

public:
const std::string symbol;
std::string msg_buffer;

std::vector<std::pair<double, double>> bids;
std::vector<std::pair<double, double>> asks;
Expand All @@ -50,7 +49,7 @@ int main()
FuturesClientUSDT public_client{};
OrderbookManager btcusdt_orderbook{ "btcusdt", public_client };

std::thread t4(&FuturesClientUSDT::stream_depth_partial<OrderbookManager>, std::ref(public_client), btcusdt_orderbook.symbol, std::ref(btcusdt_orderbook.msg_buffer), std::ref(btcusdt_orderbook), 5, 100);
std::thread t4(&FuturesClientUSDT::stream_depth_partial<OrderbookManager>, std::ref(public_client), btcusdt_orderbook.symbol, std::ref(btcusdt_orderbook), 5, 100);
btcusdt_orderbook.setup_initial_snap();

while (1)
Expand All @@ -65,7 +64,7 @@ int main()


OrderbookManager::OrderbookManager(const std::string ticker_symbol, FuturesClientUSDT& client_init)
: symbol{ ticker_symbol }, user_client{ &client_init }, msg_buffer{ "" }, parse_errors{ }, charreader{ charbuilder.newCharReader() }
: symbol{ ticker_symbol }, user_client{ &client_init }, parse_errors{ }, charreader{ charbuilder.newCharReader() }
{}

void OrderbookManager::reset_order_book(std::vector<std::pair<double, double>>& side)
Expand Down
106 changes: 53 additions & 53 deletions include/Exchange_Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,40 +101,40 @@ class Client
// WS Streams

template <typename FT>
unsigned int stream_aggTrade(const std::string& symbol, std::string& buffer, FT& functor);
unsigned int stream_aggTrade(const std::string& symbol, FT& functor);

template <typename FT>
unsigned int stream_Trade(const std::string& symbol, std::string& buffer, FT& functor);
unsigned int stream_Trade(const std::string& symbol, FT& functor);

template <typename FT>
unsigned int stream_kline(const std::string& symbol, std::string& buffer, FT& functor, std::string interval = "1h");
unsigned int stream_kline(const std::string& symbol, FT& functor, std::string interval = "1h");

template <typename FT>
unsigned int stream_ticker_ind_mini(const std::string& symbol, std::string& buffer, FT& functor);
unsigned int stream_ticker_ind_mini(const std::string& symbol, FT& functor);

template <typename FT>
unsigned int stream_ticker_all_mini(std::string& buffer, FT& functor);
unsigned int stream_ticker_all_mini(FT& functor);

template <typename FT>
unsigned int stream_ticker_ind(const std::string& symbol, std::string& buffer, FT& functor);
unsigned int stream_ticker_ind(const std::string& symbol, FT& functor);

template <typename FT>
unsigned int stream_ticker_all(std::string& buffer, FT& functor);
unsigned int stream_ticker_all(FT& functor);

template <typename FT>
unsigned int stream_ticker_ind_book(const std::string& symbol, std::string& buffer, FT& functor);
unsigned int stream_ticker_ind_book(const std::string& symbol, FT& functor);

template <typename FT>
unsigned int stream_ticker_all_book(std::string& buffer, FT& functor);
unsigned int stream_ticker_all_book(FT& functor);

template <typename FT>
unsigned int stream_depth_partial(const std::string& symbol, std::string& buffer, FT& functor, const unsigned int levels = 5, const unsigned int interval = 100);
unsigned int stream_depth_partial(const std::string& symbol, FT& functor, const unsigned int levels = 5, const unsigned int interval = 100);

template <typename FT>
unsigned int stream_depth_diff(const std::string& symbol, std::string& buffer, FT& functor, const unsigned int interval = 100);
unsigned int stream_depth_diff(const std::string& symbol, FT& functor, const unsigned int interval = 100);

template <typename FT>
unsigned int stream_userstream(std::string& buffer, FT& functor, const bool ping_listen_key = 0);
unsigned int stream_userstream(FT& functor, const bool ping_listen_key = 0);

std::string get_listen_key();
Json::Value ping_listen_key(const std::string& listen_key = ""); // only spot requires key
Expand Down Expand Up @@ -303,7 +303,7 @@ class Client
Json::Value query_margin_interest_rate_history(const Params* params_ptr);

template <typename FT>
unsigned int margin_stream_userstream(std::string& buffer, FT& functor, const bool ping_listen_key = 0, const bool& isolated_margin_type = 0);
unsigned int margin_stream_userstream(FT& functor, const bool ping_listen_key = 0, const bool& isolated_margin_type = 0);
std::string margin_get_listen_key();
std::string margin_isolated_get_listen_key(const std::string symbol);
Json::Value margin_ping_listen_key(const std::string& listen_key);
Expand Down Expand Up @@ -421,7 +421,7 @@ class Client
Json::Value custom_delete_req(const std::string& base, const std::string& endpoint, const Params* params_ptr, const bool& signature = 0);

template <typename FT>
unsigned int custom_stream(const std::string stream_name, std::string& buffer, FT& functor, const bool ping_listen_key = 0);
unsigned int custom_stream(const std::string stream_name, FT& functor, const bool ping_listen_key = 0);

RestSession* _rest_client = nullptr;
WebsocketClient<T>* _ws_client = nullptr;
Expand Down Expand Up @@ -475,7 +475,7 @@ class FuturesClient : public Client<FuturesClient<CT>>
// -- Global that are going deeper to USDT and COIN

template <typename FT>
unsigned int v_stream_Trade(const std::string& symbol, std::string& buffer, FT& functor);
unsigned int v_stream_Trade(const std::string& symbol, FT& functor);

public:
friend Client<FuturesClient<CT>>;
Expand Down Expand Up @@ -531,43 +531,43 @@ class FuturesClient : public Client<FuturesClient<CT>>
// WS Streams

template <typename FT>
unsigned int stream_markprice(const std::string& symbol, std::string& buffer, FT& functor, unsigned int interval = 1000);
unsigned int stream_markprice(const std::string& symbol, FT& functor, unsigned int interval = 1000);

template <typename FT>
unsigned int stream_liquidation_orders(const std::string& symbol, std::string& buffer, FT& functor);
unsigned int stream_liquidation_orders(const std::string& symbol, FT& functor);

template <typename FT>
unsigned int stream_liquidation_orders_all(std::string& buffer, FT& functor);
unsigned int stream_liquidation_orders_all(FT& functor);

template <typename FT>
unsigned int stream_blvt_info(std::string& buffer, FT& functor, std::string token_name);
unsigned int stream_blvt_info(FT& functor, std::string token_name);

template <typename FT>
unsigned int stream_blvt_klines(std::string& buffer, FT& functor, std::string token_name, std::string interval);
unsigned int stream_blvt_klines(FT& functor, std::string token_name, std::string interval);

template <typename FT>
unsigned int stream_composite_index_symbol(std::string& buffer, FT& functor, std::string token_name);
unsigned int stream_composite_index_symbol(FT& functor, std::string token_name);

template <typename FT>
unsigned int stream_markprice_all(const std::string& pair, std::string& buffer, FT& functor); // only USDT
unsigned int stream_markprice_all(const std::string& pair, FT& functor); // only USDT

template <typename FT>
unsigned int stream_indexprice(const std::string& pair, std::string& buffer, FT& functor, unsigned int interval = 1000); // only Coin
unsigned int stream_indexprice(const std::string& pair, FT& functor, unsigned int interval = 1000); // only Coin

template <typename FT>
unsigned int stream_markprice_by_pair(const std::string& pair, std::string& buffer, FT& functor, unsigned int interval = 1000); // only coin
unsigned int stream_markprice_by_pair(const std::string& pair, FT& functor, unsigned int interval = 1000); // only coin

template <typename FT>
unsigned int stream_kline_contract(const std::string& pair_and_type, std::string& buffer, FT& functor, std::string interval = "1h"); // only coin
unsigned int stream_kline_contract(const std::string& pair_and_type, FT& functor, std::string interval = "1h"); // only coin

template <typename FT>
unsigned int stream_kline_index(const std::string& pair, std::string& buffer, FT& functor, std::string interval = "1h"); // only coin
unsigned int stream_kline_index(const std::string& pair, FT& functor, std::string interval = "1h"); // only coin

template <typename FT>
unsigned int stream_kline_markprice(const std::string& symbol, std::string& buffer, FT& functor, std::string interval = "1h"); // only coin
unsigned int stream_kline_markprice(const std::string& symbol, FT& functor, std::string interval = "1h"); // only coin

template <typename FT>
unsigned int v_stream_userstream(std::string& buffer, FT& functor, const bool ping_listen_key);
unsigned int v_stream_userstream(FT& functor, const bool ping_listen_key);



Expand Down Expand Up @@ -661,34 +661,34 @@ class FuturesClientUSDT : public FuturesClient<FuturesClientUSDT>


template <typename FT>
unsigned int v_stream_markprice_all(const std::string& pair, std::string& buffer, FT& functor); // only USDT
unsigned int v_stream_markprice_all(const std::string& pair, FT& functor); // only USDT

template <typename FT>
unsigned int v_stream_indexprice(const std::string& pair, std::string& buffer, FT& functor, unsigned int interval); // only Coin
unsigned int v_stream_indexprice(const std::string& pair, FT& functor, unsigned int interval); // only Coin

template <typename FT>
unsigned int v_stream_markprice_by_pair(const std::string& pair, std::string& buffer, FT& functor, unsigned int interval); // only coin
unsigned int v_stream_markprice_by_pair(const std::string& pair, FT& functor, unsigned int interval); // only coin

template <typename FT>
unsigned int v_stream_blvt_info(std::string& buffer, FT& functor, std::string token_name); // only USDT
unsigned int v_stream_blvt_info(FT& functor, std::string token_name); // only USDT

template <typename FT>
unsigned int v_stream_blvt_klines(std::string& buffer, FT& functor, std::string token_name, std::string interval); // only USDT
unsigned int v_stream_blvt_klines(FT& functor, std::string token_name, std::string interval); // only USDT

template <typename FT>
unsigned int v_stream_composite_index_symbol(std::string& buffer, FT& functor, std::string token_name); // only USDT
unsigned int v_stream_composite_index_symbol(FT& functor, std::string token_name); // only USDT

template <typename FT>
unsigned int v_stream_kline_contract(const std::string& pair_and_type, std::string& buffer, FT& functor, std::string interval); // only coin
unsigned int v_stream_kline_contract(const std::string& pair_and_type, FT& functor, std::string interval); // only coin

template <typename FT>
unsigned int v_stream_kline_index(const std::string& pair, std::string& buffer, FT& functor, std::string interval); // only coin
unsigned int v_stream_kline_index(const std::string& pair, FT& functor, std::string interval); // only coin

template <typename FT>
unsigned int v_stream_kline_markprice(const std::string& symbol, std::string& buffer, FT& functor, std::string interval); // only coin
unsigned int v_stream_kline_markprice(const std::string& symbol, FT& functor, std::string interval); // only coin

template <typename FT>
unsigned int v__stream_userstream(std::string& buffer, FT& functor, const bool ping_listen_key);
unsigned int v__stream_userstream(FT& functor, const bool ping_listen_key);

std::string v__get_listen_key();
Json::Value v__ping_listen_key();
Expand Down Expand Up @@ -786,34 +786,34 @@ class FuturesClientCoin : public FuturesClient<FuturesClientCoin>
// -- going deeper...

template <typename FT>
unsigned int v_stream_markprice_all(const std::string& pair, std::string& buffer, FT& functor); // only USDT
unsigned int v_stream_markprice_all(const std::string& pair, FT& functor); // only USDT

template <typename FT>
unsigned int v_stream_indexprice(const std::string& pair, std::string& buffer, FT& functor, unsigned int interval); // only Coin
unsigned int v_stream_indexprice(const std::string& pair, FT& functor, unsigned int interval); // only Coin

template <typename FT>
unsigned int v_stream_markprice_by_pair(const std::string& pair, std::string& buffer, FT& functor, unsigned int interval); // only coin
unsigned int v_stream_markprice_by_pair(const std::string& pair, FT& functor, unsigned int interval); // only coin

template <typename FT>
unsigned int v_stream_blvt_info(std::string& buffer, FT& functor, std::string token_name); // only USDT
unsigned int v_stream_blvt_info(FT& functor, std::string token_name); // only USDT

template <typename FT>
unsigned int v_stream_blvt_klines(std::string& buffer, FT& functor, std::string token_name, std::string interval); // only USDT
unsigned int v_stream_blvt_klines(FT& functor, std::string token_name, std::string interval); // only USDT

template <typename FT>
unsigned int v_stream_composite_index_symbol(std::string& buffer, FT& functor, std::string token_name); // only USDT
unsigned int v_stream_composite_index_symbol(FT& functor, std::string token_name); // only USDT

template <typename FT>
unsigned int v_stream_kline_contract(const std::string& pair_and_type, std::string& buffer, FT& functor, std::string interval); // only coin
unsigned int v_stream_kline_contract(const std::string& pair_and_type, FT& functor, std::string interval); // only coin

template <typename FT>
unsigned int v_stream_kline_index(const std::string& pair, std::string& buffer, FT& functor, std::string interval); // only coin
unsigned int v_stream_kline_index(const std::string& pair, FT& functor, std::string interval); // only coin

template <typename FT>
unsigned int v_stream_kline_markprice(const std::string& symbol, std::string& buffer, FT& functor, std::string interval); // only coin
unsigned int v_stream_kline_markprice(const std::string& symbol, FT& functor, std::string interval); // only coin

template <typename FT>
unsigned int v__stream_userstream(std::string& buffer, FT& functor, const bool ping_listen_key);
unsigned int v__stream_userstream(FT& functor, const bool ping_listen_key);

std::string v__get_listen_key();
Json::Value v__ping_listen_key();
Expand Down Expand Up @@ -876,15 +876,15 @@ class SpotClient : public Client<SpotClient>
// WS Streams

template <typename FT>
unsigned int v_stream_Trade(const std::string& symbol, std::string& buffer, FT& functor);
unsigned int v_stream_Trade(const std::string& symbol, FT& functor);


// crtp infrastructure start

void v_init_ws_session();

template <typename FT>
unsigned int v_stream_userstream(std::string& buffer, FT& functor, const bool ping_listen_key);
unsigned int v_stream_userstream(FT& functor, const bool ping_listen_key);
std::string v_get_listen_key();
Json::Value v_ping_listen_key(const std::string& listen_key);
Json::Value v_revoke_listen_key(const std::string& listen_key);
Expand Down Expand Up @@ -950,18 +950,18 @@ class OpsClient : public Client<OpsClient>
void v_init_ws_session(); // todo: define (what is the port??? gzip used??)

template <typename FT>
unsigned int v_stream_userstream(std::string& buffer, FT& functor, const bool ping_listen_key); // todo: define
unsigned int v_stream_userstream(FT& functor, const bool ping_listen_key); // todo: define

std::string v_get_listen_key();
Json::Value v_ping_listen_key(const std::string& listen_key);
Json::Value v_revoke_listen_key(const std::string& listen_key);


template <typename FT>
unsigned int v_stream_Trade(const std::string& symbol, std::string& buffer, FT& functor);
unsigned int v_stream_Trade(const std::string& symbol, FT& functor);

template <typename FT>
unsigned int v_stream_kline(const std::string& symbol, std::string& buffer, FT& functor, std::string interval = "1h");
unsigned int v_stream_kline(const std::string& symbol, FT& functor, std::string interval = "1h");


// crtp infrastructure end
Expand Down
4 changes: 2 additions & 2 deletions include/WS_Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class WebsocketClient


template <typename FT>
void _connect_to_endpoint(const std::string stream_map_name, const std::string stream_path, std::string& buf, FT& functor, const bool ping_listen_key);
void _connect_to_endpoint(const std::string stream_map_name, const std::string stream_path, FT& functor, const bool ping_listen_key);

public:
unsigned int _max_reconnect_count;
Expand All @@ -28,7 +28,7 @@ class WebsocketClient
bool is_open(const std::string& stream_name) const;

template <typename FT>
void _stream_manager(std::string stream_map_name, const std::string stream_path, std::string& buf, FT& functor, const bool ping_listen_key = 0);
void _stream_manager(std::string stream_map_name, const std::string stream_path, FT& functor, const bool ping_listen_key = 0);

void _set_reconnect(const bool& reconnect);

Expand Down
Loading

0 comments on commit 4f1720a

Please sign in to comment.