Skip to content

Commit

Permalink
remove unnessecary threadpool executions
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkrissym committed Oct 6, 2024
1 parent 902eeb3 commit 324399a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Discord.C++/Discord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 7 additions & 11 deletions Discord.C++/Gateway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand All @@ -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");
Expand Down Expand Up @@ -234,14 +232,12 @@ DiscordCPP::SharedFuture<void> DiscordCPP::Gateway::send(const json& message) {
});
}

DiscordCPP::SharedFuture<void> 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()));
}
}
2 changes: 1 addition & 1 deletion Discord.C++/Gateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Gateway {

DLL_EXPORT virtual void connect(const std::string& url);
DLL_EXPORT SharedFuture<void> send(const json& message);
DLL_EXPORT SharedFuture<void> close();
DLL_EXPORT void close();
};

} // namespace DiscordCPP
22 changes: 8 additions & 14 deletions Discord.C++/VoiceClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,17 @@ DiscordCPP::VoiceClient::VoiceClient(std::shared_ptr<MainGateway> main_ws,
DiscordCPP::VoiceClient::~VoiceClient() {
_log.debug("~VoiceClient");
_ready = false;
disconnect().get();
_voice_ws->close().get();
disconnect();
_voice_ws->close();
}

DiscordCPP::SharedFuture<void> DiscordCPP::VoiceClient::disconnect() {
void DiscordCPP::VoiceClient::disconnect() {
if (!_ready) {
DiscordCPP::SharedFuture<void> future;
future.set();
return future;
return;
}

_ready = false;

json payload = {
{"op", 4}, //
{"d", {
Expand All @@ -230,15 +230,9 @@ DiscordCPP::SharedFuture<void> DiscordCPP::VoiceClient::disconnect() {
{"self_deaf", true} //
}} //
};
std::shared_ptr<DiscordCPP::Future<void>> main_future = _main_ws->send(payload).get_future();
std::shared_ptr<DiscordCPP::Future<void>> 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");
});
}
Expand Down
2 changes: 1 addition & 1 deletion Discord.C++/VoiceClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class VoiceClient {
*/
DLL_EXPORT SharedFuture<void> play(AudioSource* source);
DLL_EXPORT void stop_playing();
DLL_EXPORT SharedFuture<void> disconnect();
DLL_EXPORT void disconnect();
};

} // namespace DiscordCPP
2 changes: 1 addition & 1 deletion test_bot/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) + ")");
Expand Down

0 comments on commit 324399a

Please sign in to comment.