Skip to content

Commit

Permalink
wait for pending reconnects when closing websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkrissym committed Oct 11, 2024
1 parent 5d775bf commit d72d023
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
16 changes: 14 additions & 2 deletions Discord.C++/Gateway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ void DiscordCPP::Gateway::start_heartbeating() {
this->send(payload).get();
_log.debug("Heartbeat message has been sent");
} catch (const std::exception& e) {
_log.error("Cannot send heartbeat message: " +
std::string(e.what()));
_log.error("Cannot send heartbeat message: " + std::string(e.what()));
}
}
}
Expand All @@ -58,8 +57,11 @@ void DiscordCPP::Gateway::start_heartbeating() {
void DiscordCPP::Gateway::on_websocket_disconnnect() {
_connected = false;
if (_keepalive == false) {
_reconnecting = false;
_reconnect_finished.notify_all();
return;
}
_reconnecting = true;
_log.info("Websocket connection closed");

threadpool->execute([this] {
Expand All @@ -79,7 +81,11 @@ void DiscordCPP::Gateway::on_websocket_disconnnect() {
_log.info("reconnected");
_reconnect_timeout = 0;
_last_heartbeat_ack = time(nullptr);
_reconnecting = false;
_reconnect_finished.notify_all();
} catch (const beast::system_error& e) {
_reconnecting = false;
_reconnect_finished.notify_all();
_log.error("Failed to reconnect: " + std::string(e.what()));
on_websocket_disconnnect();
}
Expand Down Expand Up @@ -123,6 +129,7 @@ DiscordCPP::Gateway::Gateway(std::string token, const std::shared_ptr<Threadpool
_keepalive(true),
_reconnect_timeout(0),
_connected(false),
_reconnecting(false),
_sequence_number(0) {
_log = Logger("Discord.Gateway");

Expand Down Expand Up @@ -240,4 +247,9 @@ void DiscordCPP::Gateway::close() {
} catch (const std::exception& e) {
_log.error("Error while closing websocket: " + std::string(e.what()));
}

std::unique_lock<std::mutex> lock(_reconnect_mutex);
_reconnect_finished.wait(lock, [this]() {
return !_reconnecting;
});
}
6 changes: 6 additions & 0 deletions Discord.C++/Gateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <boost/asio/ssl.hpp>
#include <boost/beast/ssl.hpp>
#include <boost/beast/websocket.hpp>
#include <condition_variable>
#include <mutex>
#include <nlohmann/json.hpp>

#include "Logger.h"
Expand Down Expand Up @@ -42,6 +44,10 @@ class Gateway {
std::thread _heartbeat_task;
/// indicator if Gateway is connected
bool _connected;
/// condition to wait for ongoing reconnects
std::condition_variable _reconnect_finished;
std::mutex _reconnect_mutex;
bool _reconnecting;
/// current sequence number
unsigned int _sequence_number;
/// logging instance
Expand Down
9 changes: 5 additions & 4 deletions Discord.C++/VoiceClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ DiscordCPP::VoiceClient::VoiceClient(std::shared_ptr<MainGateway> main_ws,
DiscordCPP::VoiceClient::~VoiceClient() {
_log.debug("~VoiceClient");
_ready = false;
disconnect();
_voice_ws->close();
}

Expand All @@ -232,9 +231,11 @@ void DiscordCPP::VoiceClient::disconnect() {
};

_voice_ws->close();
_main_ws->send(payload).then([this]() {
_log.debug("Payload with Opcode 4 (Gateway Voice State Update) has been sent");
});
_main_ws->send(payload)
.then([this]() {
_log.debug("Payload with Opcode 4 (Gateway Voice State Update) has been sent");
})
.get();
}

void DiscordCPP::VoiceClient::stop_playing() {
Expand Down

0 comments on commit d72d023

Please sign in to comment.