Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Aug 15, 2022
1 parent 8360952 commit 5cb75b5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
11 changes: 11 additions & 0 deletions include/dpp/discordclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,12 @@ class DPP_EXPORT discord_client : public websocket_client
*/
void end_zlib();

/**
* @brief Update the websocket hostname with the resume url
* from the last READY event
*/
void set_resume_hostname();

public:
/**
* @brief Owning cluster
Expand Down Expand Up @@ -350,6 +356,11 @@ class DPP_EXPORT discord_client : public websocket_client
*/
std::unordered_map<snowflake, voiceconn*> connecting_voice_channels;

/**
* @brief The gateway address we reconnect to when we resume a session
*/
std::string resume_gateway_url;

/**
* @brief Log a message to whatever log the user is using.
* The logged message is passed up the chain to the on_log event in user code which can then do whatever
Expand Down
11 changes: 9 additions & 2 deletions src/dpp/discordclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ discord_client::discord_client(dpp::cluster* _cluster, uint32_t _shard_id, uint3
websocket_ping(0.0),
ready(false),
last_heartbeat_ack(time(nullptr)),
protocol(ws_proto)
protocol(ws_proto),
resume_gateway_url(DEFAULT_GATEWAY)
{
zlib = new zlibcontext();
etf = new etf_parser();
Expand Down Expand Up @@ -141,6 +142,11 @@ void discord_client::end_zlib()
}
}

void discord_client::set_resume_hostname()
{
hostname = resume_gateway_url;
}

void discord_client::thread_run()
{
utility::set_thread_name(std::string("shard/") + std::to_string(shard_id));
Expand All @@ -155,9 +161,10 @@ void discord_client::thread_run()
end_zlib();
setup_zlib();
do {
this->log(ll_debug, "Attempting reconnection of shard " + std::to_string(this->shard_id));
this->log(ll_debug, "Attempting reconnection of shard " + std::to_string(this->shard_id) + " to wss://" + resume_gateway_url);
error = false;
try {
set_resume_hostname();
ssl_client::connect();
websocket_client::connect();
}
Expand Down
16 changes: 16 additions & 0 deletions src/dpp/events/ready.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <dpp/discordevents.h>
#include <dpp/cluster.h>
#include <dpp/stringops.h>
#include <dpp/dns.h>
#include <dpp/nlohmann/json.hpp>

using json = nlohmann::json;
Expand All @@ -43,6 +44,21 @@ std::mutex protect_the_loot;
void ready::handle(discord_client* client, json &j, const std::string &raw) {
client->log(dpp::ll_info, "Shard id " + std::to_string(client->shard_id) + " (" + std::to_string(client->shard_id + 1) + "/" + std::to_string(client->max_shards) + ") ready!");
client->sessionid = j["d"]["session_id"];
/* Session-specific gateway resume url
* https://discord.com/developers/docs/change-log#sessionspecific-gateway-resume-urls
*
* Discord give us the hostname wrapped in wss://crap/ like we're going to pass it to
* some top-heavy lib. Let's strip all this out if given to us so we just have a
* hostname.
*/
std::string ugly(j["d"]["resume_gateway_url"]);
if (ugly.substr(0, 6) == "wss://") {
client->resume_gateway_url = ugly.substr(6, ugly.length() - 7);
} else {
client->resume_gateway_url = ugly;
}
/* Pre-resolve it into our cache so that we aren't waiting on this when we need it later */
static_cast<void>(resolve_hostname(client->resume_gateway_url, "443"));

client->ready = true;

Expand Down
18 changes: 9 additions & 9 deletions src/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ std::map<std::string, test_t> tests = {
{"COMPARISON", {tt_offline, "dpp::manged object comparison", false, false}},
{"CHANNELCACHE", {tt_online, "dpp::find_channel()", false, false}},
{"CHANNELTYPES", {tt_online, "dpp::channel type flags", false, false}},
{"PERMISSION_CLASS", {tt_offline, "testing dpp::permission functionality", false, false}},
{"USER.GET_MENTION", {tt_offline, "testing dpp::user::get_mention functionality", false, false}},
{"USER.FORMAT_USERNAME", {tt_offline, "testing dpp::user::format_username functionality", false, false}},
{"USER.GET_CREATION_TIME", {tt_offline, "testing dpp::user::get_creation_time functionality", false, false}},
{"UTILITY.ICONHASH", {tt_offline, "testing dpp::utility::iconhash functionality", false, false}},
{"UTILITY.MAKE_URL_PARAMETERS", {tt_offline, "testing dpp::utility::make_url_parameters functionality", false, false}},
{"UTILITY.MARKDOWN_ESCAPE", {tt_offline, "testing dpp::utility::markdown_escape functionality", false, false}},
{"UTILITY.TOKENIZE", {tt_offline, "testing dpp::utility::tokenize functionality", false, false}},
{"UTILITY.URL_ENCODE", {tt_offline, "testing dpp::utility::url_encode functionality", false, false}},
{"PERMISSION_CLASS", {tt_offline, "dpp::permission", false, false}},
{"USER.GET_MENTION", {tt_offline, "dpp::user::get_mention", false, false}},
{"USER.FORMAT_USERNAME", {tt_offline, "dpp::user::format_username", false, false}},
{"USER.GET_CREATION_TIME", {tt_offline, "dpp::user::get_creation_time", false, false}},
{"UTILITY.ICONHASH", {tt_offline, "dpp::utility::iconhash", false, false}},
{"UTILITY.MAKE_URL_PARAMETERS", {tt_offline, "dpp::utility::make_url_parameters", false, false}},
{"UTILITY.MARKDOWN_ESCAPE", {tt_offline, "dpp::utility::markdown_escape", false, false}},
{"UTILITY.TOKENIZE", {tt_offline, "dpp::utility::tokenize", false, false}},
{"UTILITY.URL_ENCODE", {tt_offline, "dpp::utility::url_encode", false, false}},
};

double start = dpp::utility::time_f();
Expand Down

0 comments on commit 5cb75b5

Please sign in to comment.