Skip to content

Commit

Permalink
fix: finally it works. TODO: cleaup
Browse files Browse the repository at this point in the history
  • Loading branch information
Neko-Life committed Oct 13, 2024
1 parent 78c75e3 commit 91ee46c
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 97 deletions.
45 changes: 41 additions & 4 deletions include/dpp/discordvoiceclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ enum voice_websocket_opcode_t : uint8_t {
voice_client_dave_mls_key_package = 26,
voice_client_dave_mls_proposals = 27,
voice_client_dave_mls_commit_message = 28,
voice_client_dave_announce_commit_transaction = 29,
voice_client_dave_announce_commit_transition = 29,
voice_client_dave_mls_welcome = 30,
voice_client_dave_mls_invalid_commit_welcome = 31,
};
Expand Down Expand Up @@ -206,7 +206,7 @@ struct dave_binary_header_t {
[[nodiscard]] std::vector<uint8_t> get_data() const;

/**
* Get transition ID for process_welcome
* Get transition ID for process_commit and process_welcome
*
* @return Transition ID
*/
Expand All @@ -215,7 +215,7 @@ struct dave_binary_header_t {
private:
/**
* @brief Transition id, only valid when the opcode is
* welcome state. Use get_transition_id() to obtain value.
* commit and welcome state. Use get_transition_id() to obtain value.
*/
uint16_t transition_id;
};
Expand Down Expand Up @@ -482,7 +482,7 @@ class DPP_EXPORT discord_voice_client : public websocket_client
*/
std::set<std::string> dave_mls_new_user_list;

/**
/**
* @brief File descriptor for UDP connection
*/
dpp::socket fd;
Expand Down Expand Up @@ -583,6 +583,11 @@ class DPP_EXPORT discord_voice_client : public websocket_client
*/
dave_version_t dave_version;

/**
* @brief Current dave session key generation.
*/
uint32_t dave_current_epoch;

/**
* @brief Send data to UDP socket immediately.
*
Expand Down Expand Up @@ -1200,6 +1205,38 @@ class DPP_EXPORT discord_voice_client : public websocket_client
* which internally uses scrypt.
*/
void get_user_privacy_code(const dpp::snowflake user, privacy_code_callback_t callback) const;

/*
* @brief Notify gateway ready for a DAVE transition.
*
* Fires Voice Ready event when appropriate.
*
* https://daveprotocol.com/#commit-handling
*/
void ready_for_transition(const std::string &data);

/**
* @brief Reset dave session, send voice_client_dave_mls_invalid_commit_welcome
* payload with current transition Id and our new key package to gateway.
*
* https://daveprotocol.com/#recovery-from-invalid-commit-or-welcome
*/
void recover_from_invalid_commit_welcome();

/**
* @brief Execute pending protocol upgrade/downgrade to/from dave.
*/
void execute_pending_upgrade_downgrade();

/**
* @brief Reset dave session and prepare initial session group.
*/
void reinit_dave_mls_group();

/**
* @brief Process roster map from commit/welcome.
*/
void process_mls_group_rosters(const std::map<unsigned long, std::vector<unsigned char>> &rmap);
};

} // namespace dpp
Expand Down
9 changes: 6 additions & 3 deletions src/dpp/discordvoiceclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ bool discord_voice_client::is_playing() {
}

uint16_t dave_binary_header_t::get_transition_id() const {
if (opcode != voice_client_dave_mls_welcome) {
throw dpp::logic_exception("Can't get transition ID from buffer that is not of type voice_client_dave_mls_welcome(30)");
bool has_transition_id = opcode == voice_client_dave_mls_welcome || opcode == voice_client_dave_announce_commit_transition;
if (!has_transition_id) {
throw dpp::logic_exception("Can't get transition ID from buffer that is not of type voice_client_dave_announce_commit_transition(29) or voice_client_dave_mls_welcome(30)");
}
return transition_id;
}
Expand All @@ -98,7 +99,9 @@ dave_binary_header_t::dave_binary_header_t(const std::string& buffer) {
seq = (buffer[0] << 8) | buffer[1];
opcode = buffer[2];
transition_id = (buffer[3] << 8) | buffer[4];
package.assign(buffer.begin() + (opcode == voice_client_dave_mls_welcome ? 5 : 3), buffer.end());

bool has_transition_id = opcode == voice_client_dave_mls_welcome || opcode == voice_client_dave_announce_commit_transition;
package.assign(buffer.begin() + (has_transition_id ? 5 : 3), buffer.end());
}

std::vector<uint8_t> dave_binary_header_t::get_data() const {
Expand Down
1 change: 1 addition & 0 deletions src/dpp/voice/enabled/constructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ discord_voice_client::discord_voice_client(dpp::cluster* _cluster, snowflake _ch
sending(false),
tracks(0),
dave_version(enable_dave ? dave_version_1 : dave_version_none),
dave_current_epoch(0),
creator(_cluster),
terminating(false),
heartbeat_interval(0),
Expand Down
9 changes: 5 additions & 4 deletions src/dpp/voice/enabled/enabled.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ struct dave_state {
* @brief Current transition ID
*/
uint64_t transition_id{0};
/**
* @brief True when we have a sending ratchet
*/
bool have_sending_ratchet{false};
/**
* @brief Have sent ready event to listeners
*/
Expand Down Expand Up @@ -133,6 +129,11 @@ struct dave_state {
* MLS group is not established.
*/
std::string privacy_code;

/**
* @brief Cached roster map.
*/
dave::roster_map cached_roster_map;
};

/**
Expand Down
Loading

0 comments on commit 91ee46c

Please sign in to comment.