Skip to content
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
2 changes: 1 addition & 1 deletion include/dpp/discordclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class DPP_EXPORT voiceconn : public std::enable_shared_from_this<voiceconn> {
*/
bool is_active() const;

voiceconn& request(bool failed_resume = false);
voiceconn& request(bool session_invalid = true);

/**
* @brief Create websocket object and connect it.
Expand Down
9 changes: 8 additions & 1 deletion include/dpp/discordvoiceclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ using privacy_code_callback_t = std::function<void(const std::string&)>;
/**
* @brief A callback for a full reconnection after the voice client disconnects due to error.
*/
using full_reconnection_callback_t = std::function<void()>;
using full_reconnection_callback_t = std::function<void(bool)>;

/** @brief Implements a discord voice connection.
* Each discord_voice_client connects to one voice channel and derives from a websocket client.
Expand Down Expand Up @@ -441,6 +441,13 @@ class DPP_EXPORT discord_voice_client : public websocket_client
*/
bool sent_stop_frames{};

/**
* @brief Whether we got 4006 session invalid error code
*
* This is to tell reconnect callback to clear session data for the next reconnect attempt
*/
bool session_invalid{};

/**
* @brief Number of times we have tried to reconnect in the last few seconds
*/
Expand Down
10 changes: 5 additions & 5 deletions src/dpp/discordclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,11 +607,11 @@ voiceconn::~voiceconn() {
this->disconnect();
}

voiceconn& voiceconn::request(bool failed_resume) {
voiceconn& voiceconn::request(bool session_invalid) {
/* Creating new connection from previously failed Opcode 7 Resume doesn't guarantee */
/* to receive voice_state_update AND voice_server_update event */
/* Keep previous session data so we can reconnect */
if (!failed_resume) {
/* Keep previous session data if still valid so we can reconnect */
if (session_invalid) {
this->token.clear();
this->session_id.clear();
this->websocket_hostname.clear();
Expand All @@ -626,9 +626,9 @@ voiceconn& voiceconn::connect() {
if (this->is_ready() && !this->is_active()) {
try {
this->creator->log(ll_debug, "Connecting voice for guild " + std::to_string(this->guild_id) + " channel " + std::to_string(this->channel_id));
full_reconnection_callback_t reconnection_callback = [weak_this=weak_from_this()] {
full_reconnection_callback_t reconnection_callback = [weak_this=weak_from_this()] (bool session_invalid) {
if (std::shared_ptr<voiceconn> strong_this = weak_this.lock()) {
strong_this->request(true);
strong_this->request(session_invalid);
}
};
this->voiceclient = std::make_unique<discord_voice_client>(creator->creator, std::move(reconnection_callback), this->channel_id, this->guild_id, this->token, this->session_id, this->websocket_hostname, this->dave);
Expand Down
1 change: 1 addition & 0 deletions src/dpp/discordvoiceclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ void discord_voice_client::error(uint32_t errorcode)
log(dpp::ll_error, "This is a non-recoverable error, giving up on voice connection");
}

this->session_invalid = errorcode == 4006;
this->close();
}

Expand Down
4 changes: 3 additions & 1 deletion src/dpp/voice/enabled/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ void discord_voice_client::on_disconnect() {
/* If we've looped 5 or more times, abort the loop. */
if (terminating || times_looped >= 5) {
log(dpp::ll_warning, "Reached max loops whilst attempting to read from the websocket. Starting a full reconnection.");
owner->queue_work(1, reconnection_callback);
bool si = this->session_invalid;
auto rc = reconnection_callback;
owner->queue_work(1, [rc, si]() { rc(si) ;});
return;
}
last_loop_time = current_time;
Expand Down
Loading