From 324399ad7af5ade6931b8f9ac411361023c44aa6 Mon Sep 17 00:00:00 2001 From: Arkrissym Date: Sun, 6 Oct 2024 19:45:33 +0200 Subject: [PATCH] remove unnessecary threadpool executions --- Discord.C++/Discord.cpp | 2 +- Discord.C++/Gateway.cpp | 18 +++++++----------- Discord.C++/Gateway.h | 2 +- Discord.C++/VoiceClient.cpp | 22 ++++++++-------------- Discord.C++/VoiceClient.h | 2 +- test_bot/main.cpp | 2 +- 6 files changed, 19 insertions(+), 29 deletions(-) diff --git a/Discord.C++/Discord.cpp b/Discord.C++/Discord.cpp index d42ca78..027e17d 100644 --- a/Discord.C++/Discord.cpp +++ b/Discord.C++/Discord.cpp @@ -51,7 +51,7 @@ DiscordCPP::Discord::Discord(const std::string &token, const Intents &intents, c DiscordCPP::Discord::~Discord() { for (auto &_gateway : _gateways) { - _gateway->close().get(); + _gateway->close(); } delete _user; diff --git a/Discord.C++/Gateway.cpp b/Discord.C++/Gateway.cpp index 66f1395..f512049 100644 --- a/Discord.C++/Gateway.cpp +++ b/Discord.C++/Gateway.cpp @@ -34,7 +34,6 @@ void DiscordCPP::Gateway::start_heartbeating() { _log.warning("Gateway stopped responding. Closing and restarting websocket..."); try { get_lowest_layer(*_client).close(); - //_client->close(websocket::close_reason(websocket::close_code::going_away, "Server not responding")); } catch (std::exception& e) { _log.error("Cannot close websocket: " + std::string(e.what())); } @@ -57,12 +56,11 @@ void DiscordCPP::Gateway::start_heartbeating() { } void DiscordCPP::Gateway::on_websocket_disconnnect() { - _log.info("Websocket connection closed"); - _connected = false; if (_keepalive == false) { return; } + _log.info("Websocket connection closed"); threadpool->execute([this] { _log.info("trying to reconnect in " + std::to_string((double)_reconnect_timeout / 1000) + "s"); @@ -234,14 +232,12 @@ DiscordCPP::SharedFuture DiscordCPP::Gateway::send(const json& message) { }); } -DiscordCPP::SharedFuture DiscordCPP::Gateway::close() { +void DiscordCPP::Gateway::close() { _keepalive = false; - return threadpool->execute([this]() { - try { - get_lowest_layer(*_client).close(); - } catch (const std::exception& e) { - _log.error("Error while closing websocket: " + std::string(e.what())); - } - }); + try { + get_lowest_layer(*_client).close(); + } catch (const std::exception& e) { + _log.error("Error while closing websocket: " + std::string(e.what())); + } } diff --git a/Discord.C++/Gateway.h b/Discord.C++/Gateway.h index f268b10..05b9d34 100644 --- a/Discord.C++/Gateway.h +++ b/Discord.C++/Gateway.h @@ -64,7 +64,7 @@ class Gateway { DLL_EXPORT virtual void connect(const std::string& url); DLL_EXPORT SharedFuture send(const json& message); - DLL_EXPORT SharedFuture close(); + DLL_EXPORT void close(); }; } // namespace DiscordCPP diff --git a/Discord.C++/VoiceClient.cpp b/Discord.C++/VoiceClient.cpp index 003e14e..e695b02 100644 --- a/Discord.C++/VoiceClient.cpp +++ b/Discord.C++/VoiceClient.cpp @@ -209,17 +209,17 @@ DiscordCPP::VoiceClient::VoiceClient(std::shared_ptr main_ws, DiscordCPP::VoiceClient::~VoiceClient() { _log.debug("~VoiceClient"); _ready = false; - disconnect().get(); - _voice_ws->close().get(); + disconnect(); + _voice_ws->close(); } -DiscordCPP::SharedFuture DiscordCPP::VoiceClient::disconnect() { +void DiscordCPP::VoiceClient::disconnect() { if (!_ready) { - DiscordCPP::SharedFuture future; - future.set(); - return future; + return; } + _ready = false; + json payload = { {"op", 4}, // {"d", { @@ -230,15 +230,9 @@ DiscordCPP::SharedFuture DiscordCPP::VoiceClient::disconnect() { {"self_deaf", true} // }} // }; - std::shared_ptr> main_future = _main_ws->send(payload).get_future(); - std::shared_ptr> voice_future = _voice_ws->close().get_future(); - - return threadpool->execute([this, voice_future, main_future] { - _ready = false; - - voice_future->get(); - main_future->get(); + _voice_ws->close(); + _main_ws->send(payload).then([this]() { _log.debug("Payload with Opcode 4 (Gateway Voice State Update) has been sent"); }); } diff --git a/Discord.C++/VoiceClient.h b/Discord.C++/VoiceClient.h index f1407c2..8807a96 100644 --- a/Discord.C++/VoiceClient.h +++ b/Discord.C++/VoiceClient.h @@ -89,7 +89,7 @@ class VoiceClient { */ DLL_EXPORT SharedFuture play(AudioSource* source); DLL_EXPORT void stop_playing(); - DLL_EXPORT SharedFuture disconnect(); + DLL_EXPORT void disconnect(); }; } // namespace DiscordCPP diff --git a/test_bot/main.cpp b/test_bot/main.cpp index ba4b8bf..e9c2466 100644 --- a/test_bot/main.cpp +++ b/test_bot/main.cpp @@ -32,7 +32,7 @@ class Client : public Discord { try { vc->play(source).get(); - vc->disconnect().get(); + vc->disconnect(); } catch (const OpusError& e) { log.error("Opus error: " + string(e.what()) + " (code: " + to_string(e.get_error_code()) + ")");