Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove buf #62

Merged
merged 2 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,18 @@ All (except for ones that don't have mandatory parameters) REST request methods
Each time a client object is created, a websocket client is also instantiated. In fact, the websocket client accepts the Client object as an argument.

<br /> The websocket client holds a map of all stream connection names and their current status. **symbol@stream_name** (i.e: btc@aggTrade). This is very crucial to know in order to be able to close a stream by using the `close_stream()` method.
<br />Not all streams accept the same arguments list, but all of them accept an `std::string` buffer and a functor object to use as callback.
<br />Not all streams accept the same arguments list, but all of them accept a functor object to use as callback.

- #### Callback functor
All streams accepts a reference to std::string buffer and a reference to a functor object. This is implemented using templates, therefore the template type of the stream, when called, should be the type of the functor object.
>client_obj.stream_aggTrade<typename SomeFunctor\>(symbol, buff, functor_obj)
All streams accept a reference to a functor object. This is implemented using templates, therefore the template type of the stream, when called, should be the type of the functor object.
>client_obj.stream_aggTrade<typename SomeFunctor\>(symbol, functor_obj)

<br /> It would be good practice to set the buffer as a member of the functor object.
- #### Stream Manager
The WebsocketClient class has a `stream_manager` method, which is responsible for the stream connection. It is possible to set `reconnect_on_error` by using Client's `ws_auto_reconnect()` method, and also specify the number of attempts by using `set_max_reconnect_count()` method.
<br />The `stream_manager` method closes a stream when the stream status is set to zero by the `close_stream()` method, or if any other error was encountered (unless `reconnect_on_error` is true).
<br /> The stream manager also accepts a `bool` for whether or not to ping a listen key periodically. The ping interval is 30 minutes by default, and can be set using `set_refresh_key_interval()` method (in `Client`).
- #### Custom Streams
Custom streams are possible by using `Client`'s `custom_stream()` method. This method accepts 4 arguments: `stream_path` std::string, `buffer` std::string, `functor` functor, and `ping_listen_key` bool (when true, pings listen key periodically).
Custom streams are possible by using `Client`'s `custom_stream()` method. This method accepts 3 arguments: `stream_path`, std::string, `functor` functor, and `ping_listen_key` bool (when true, pings listen key periodically).

- #### Notes
1. Default arguments are not allowed with threads. The argument must be specified
Expand Down
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
Loading