From f2e1edcff3f366eb9bdf75a7e49c27066b426bf5 Mon Sep 17 00:00:00 2001 From: Craig Edwards Date: Mon, 2 Dec 2024 11:38:19 +0000 Subject: [PATCH] voice session reconnect --- include/dpp/discordclient.h | 3 +-- include/dpp/wsclient.h | 10 +--------- src/dpp/discordvoiceclient.cpp | 2 ++ src/dpp/voice/enabled/cleanup.cpp | 4 ++-- src/dpp/voice/enabled/thread.cpp | 8 +++++++- 5 files changed, 13 insertions(+), 14 deletions(-) diff --git a/include/dpp/discordclient.h b/include/dpp/discordclient.h index 74a609fa0f..fd5d1812aa 100644 --- a/include/dpp/discordclient.h +++ b/include/dpp/discordclient.h @@ -35,10 +35,9 @@ #include #include - - #define DISCORD_API_VERSION "10" #define API_PATH "/api/v" DISCORD_API_VERSION + namespace dpp { // Forward declarations diff --git a/include/dpp/wsclient.h b/include/dpp/wsclient.h index c7fc99d4ee..da2a6c6fdf 100644 --- a/include/dpp/wsclient.h +++ b/include/dpp/wsclient.h @@ -135,15 +135,6 @@ class DPP_EXPORT websocket_client : public ssl_client { */ bool parseheader(std::string& buffer); - /** - * @brief Unpack a frame and pass completed frames up the stack. - * @param buffer The buffer to operate on. Gets modified to remove completed frames on the head of the buffer - * @param offset The offset to start at (reserved for future use) - * @param first True if is the first element (reserved for future use) - * @return true if a complete frame has been received - */ - bool unpack(std::string& buffer, uint32_t offset, bool first = true); - /** * @brief Fill a header for outbound messages * @param outbuf The raw frame to fill @@ -233,6 +224,7 @@ class DPP_EXPORT websocket_client : public ssl_client { /** * @brief Send OP_CLOSE error code 1000 to the other side of the connection. * This indicates graceful close. + * @note This informs Discord to invalidate the session, you cannot resume if you send this */ void send_close_packet(); diff --git a/src/dpp/discordvoiceclient.cpp b/src/dpp/discordvoiceclient.cpp index 5b241d1122..31801c62f7 100644 --- a/src/dpp/discordvoiceclient.cpp +++ b/src/dpp/discordvoiceclient.cpp @@ -231,6 +231,8 @@ void discord_voice_client::error(uint32_t errorcode) this->terminating = true; log(dpp::ll_error, "This is a non-recoverable error, giving up on voice connection"); } + + this->close(); } void discord_voice_client::set_user_gain(snowflake user_id, float factor) diff --git a/src/dpp/voice/enabled/cleanup.cpp b/src/dpp/voice/enabled/cleanup.cpp index 6d7b594b1d..45288afa8c 100644 --- a/src/dpp/voice/enabled/cleanup.cpp +++ b/src/dpp/voice/enabled/cleanup.cpp @@ -33,11 +33,11 @@ namespace dpp { void discord_voice_client::cleanup() { - if (encoder) { + if (encoder != nullptr) { opus_encoder_destroy(encoder); encoder = nullptr; } - if (repacketizer) { + if (repacketizer != nullptr) { opus_repacketizer_destroy(repacketizer); repacketizer = nullptr; } diff --git a/src/dpp/voice/enabled/thread.cpp b/src/dpp/voice/enabled/thread.cpp index 2d5372977a..135d1cc3fe 100644 --- a/src/dpp/voice/enabled/thread.cpp +++ b/src/dpp/voice/enabled/thread.cpp @@ -50,10 +50,16 @@ void discord_voice_client::on_disconnect() { } last_loop_time = current_time; - log(dpp::ll_debug, "Attempting to reconnect the websocket..."); + ssl_client::close(); owner->start_timer([this](auto handle) { + log(dpp::ll_debug, "Attempting to reconnect voice websocket " + std::to_string(channel_id) + " to wss://" + hostname + "..."); owner->stop_timer(handle); cleanup(); + if (timer_handle) { + owner->stop_timer(timer_handle); + timer_handle = 0; + } + start = time(nullptr); setup(); terminating = false; ssl_client::connect();