diff --git a/include/dpp/discordvoiceclient.h b/include/dpp/discordvoiceclient.h index ca2d189d7f..56fc4bb752 100644 --- a/include/dpp/discordvoiceclient.h +++ b/include/dpp/discordvoiceclient.h @@ -410,9 +410,11 @@ class DPP_EXPORT discord_voice_client : public websocket_client uint16_t sequence; /** - * @brief Last received sequence from gateway + * @brief Last received sequence from gateway. + * + * Needed for heartbeat and resume payload. */ - uint16_t receive_sequence; + int32_t receive_sequence; /** * @brief Timestamp value used in outbound audio. Each packet diff --git a/src/dpp/discordvoiceclient.cpp b/src/dpp/discordvoiceclient.cpp index 3e44d6aedc..8b5b0fa588 100644 --- a/src/dpp/discordvoiceclient.cpp +++ b/src/dpp/discordvoiceclient.cpp @@ -328,7 +328,7 @@ discord_voice_client::discord_voice_client(dpp::cluster* _cluster, snowflake _ch repacketizer(nullptr), fd(INVALID_SOCKET), sequence(0), - receive_sequence(0), + receive_sequence(-1), timestamp(0), packet_nonce(1), last_timestamp(std::chrono::high_resolution_clock::now()), @@ -531,6 +531,16 @@ bool discord_voice_client::handle_frame(const std::string &data, ws_opcode opcod return true; } + if (j.find("seq") != j.end() && j["seq"].is_number()) { + /** + * Save the sequence number needed for heartbeat and resume payload. + * + * NOTE: Contrary to the documentation, discord does not seem to send messages with sequence number + * in order, should we only save the sequence if it's larger number? + */ + receive_sequence = j["seq"].get(); + } + if (j.find("op") != j.end()) { uint32_t op = j["op"]; @@ -608,6 +618,9 @@ bool discord_voice_client::handle_frame(const std::string &data, ws_opcode opcod this->heartbeat_interval = j["d"]["heartbeat_interval"].get(); } + /* Reset receive_sequence on HELLO */ + receive_sequence = -1; + if (!modes.empty()) { log(dpp::ll_debug, "Resuming voice session " + this->sessionid + "..."); json obj = { @@ -618,7 +631,7 @@ bool discord_voice_client::handle_frame(const std::string &data, ws_opcode opcod { "server_id", std::to_string(this->server_id) }, { "session_id", this->sessionid }, { "token", this->token }, - { "seq_ack", this->sequence }, + { "seq_ack", this->receive_sequence }, } } }; @@ -843,9 +856,6 @@ void discord_voice_client::read_ready() std::memcpy(&vp.seq, &buffer[2], sizeof(rtp_seq_t)); vp.seq = ntohs(vp.seq); - /* Used for buffered resume of receive */ - receive_sequence = vp.seq; - /* Get the timestamp of the voice UDP packet */ std::memcpy(&vp.timestamp, &buffer[4], sizeof(rtp_timestamp_t)); vp.timestamp = ntohl(vp.timestamp);