Add continous ping function to WebsocketClient (cpp)
This commit is contained in:
1 parent
61f04c4605
commit
e7e334fb5a
2 files changed
+49
-4
No files matched your search
@@ -11,6 +11,7 @@
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/utils/formatters/exception_formatter.h"
|
||||
#include "lib/utils/thread_manager.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
@@ -99,6 +100,7 @@ using namespace std::chrono_literals;
|
||||
|
||||
using bts::core::TimeSpan;
|
||||
using bts::core::TimeStamp;
|
||||
using bts::threading::configure_current_thread;
|
||||
using nlohmann::json;
|
||||
using std::lock_guard;
|
||||
using std::map;
|
||||
@@ -150,6 +152,7 @@ struct WebsocketClient::PImpl {
|
||||
size_t sub_id_counter{0};
|
||||
string server_name;
|
||||
optional<thread> io_thread;
|
||||
optional<thread> ping_thread;
|
||||
int client_id{client_id_counter++};
|
||||
mutex instance_mx;
|
||||
State state = State::IDLE;
|
||||
@@ -309,14 +312,17 @@ struct WebsocketClient::PImpl {
|
||||
"io_thread. Websocket io threw websocketpp::exception. {}; "
|
||||
"err: {}",
|
||||
snapshot(), std::string(ex.what()));
|
||||
bts_assert_msgf(false, "io_thread. Websocket io threw websocketpp::exception. {}; "
|
||||
bts_assert_msgf(
|
||||
false,
|
||||
"io_thread. Websocket io threw websocketpp::exception. {}; "
|
||||
"err: {}",
|
||||
snapshot(), std::string(ex.what()));
|
||||
snapshot(), std::string(ex.what()));
|
||||
} catch (const std::exception &ex) {
|
||||
ERROR_FN(
|
||||
"io_thread. Websocket io threw std::exception. {}; err: {}",
|
||||
snapshot(), std::string(ex.what()));
|
||||
bts_assert_msgf(false, "io_thread. Websocket io threw std::exception. {}; err: {}",
|
||||
bts_assert_msgf(
|
||||
false, "io_thread. Websocket io threw std::exception. {}; err: {}",
|
||||
snapshot(), std::string(ex.what()));
|
||||
} catch (...) {
|
||||
bts_assert_msgf(
|
||||
@@ -350,9 +356,40 @@ struct WebsocketClient::PImpl {
|
||||
}
|
||||
}
|
||||
|
||||
void ping_continuously(int interval_secs, string payload) {
|
||||
INFO_FN("ping_continuously");
|
||||
if (ping_thread) {
|
||||
// stop the old ping
|
||||
is_continuous_ping = false;
|
||||
ping_thread->join();
|
||||
}
|
||||
is_continuous_ping = true;
|
||||
ping_thread.emplace([&, interval_secs, payload = std::move(payload)]() {
|
||||
configure_current_thread(fmt::format("{}-ping", config.name));
|
||||
const string &url = config.url;
|
||||
DIAG_FN("ping_continuously. Thread starting. url: {}", url);
|
||||
auto last_ping_time = TimeStamp::now();
|
||||
std::this_thread::sleep_for(std::chrono::seconds(interval_secs));
|
||||
while (state == State::OPEN && is_continuous_ping) {
|
||||
const auto now = TimeStamp::now();
|
||||
if ((now - last_ping_time).seconds() > interval_secs) {
|
||||
DIAG_FN("ping_continuously. Pinging websocket. url: {}", url);
|
||||
con->ping(payload);
|
||||
last_ping_time = now;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
}
|
||||
DIAG_FN("ping_continuously. Thread exiting. url: {}", url);
|
||||
});
|
||||
}
|
||||
|
||||
void stop_continuous_ping() { is_continuous_ping = false; }
|
||||
|
||||
private:
|
||||
static std::atomic<int> client_id_counter;
|
||||
|
||||
bool is_continuous_ping = false;
|
||||
|
||||
std::shared_ptr<boost::asio::ssl::context> on_tls_init(
|
||||
ConnectionHdl /*hdl*/) {
|
||||
INFO_FN("on_tls_init. state: {}; version: {}", state, version);
|
||||
@@ -502,6 +539,13 @@ bool WebsocketClient::is_connected() const {
|
||||
|
||||
void WebsocketClient::ping(const string &payload) { me_->con->ping(payload); }
|
||||
|
||||
void WebsocketClient::ping_continuously(
|
||||
int interval_secs, const std::string &payload) {
|
||||
return me_->ping_continuously(interval_secs, payload);
|
||||
}
|
||||
|
||||
void WebsocketClient::stop_continous_ping() { me_->stop_continuous_ping(); }
|
||||
|
||||
const string &WebsocketClient::url() const { return me_->config.url; }
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -33,6 +33,8 @@ class WebsocketClient {
|
||||
CloseHandler &close_handler();
|
||||
websocketpp::lib::error_code send_text(const std::string &text);
|
||||
void ping(const std::string &payload = "");
|
||||
void ping_continuously(int interval_secs, const std::string &payload = "");
|
||||
void stop_continous_ping();
|
||||
|
||||
const std::string &url() const;
|
||||
|
||||
@@ -42,4 +44,3 @@ class WebsocketClient {
|
||||
};
|
||||
|
||||
} // namespace bts::websocket
|
||||
|
||||
Reference in new issue
Block a user