Skip to content

Commit

Permalink
catch exceptions on reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Dec 3, 2024
1 parent 5f5489e commit f584b2f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
36 changes: 22 additions & 14 deletions src/dpp/discordclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,28 +125,36 @@ discord_client::~discord_client()

void discord_client::on_disconnect()
{
if (reconnect_timer != 0U) {
log(dpp::ll_debug, "Lost connection to websocket on shard " + std::to_string(shard_id) + ", reconnection already in progress...");
return;
}
set_resume_hostname();
log(dpp::ll_debug, "Lost connection to websocket on shard " + std::to_string(shard_id) + ", reconnecting in 5 seconds...");
ssl_client::close();
end_zlib();
/* Stop the timer first if its already ticking, to prevent concurrent reconnects */
if (reconnect_timer != 0U) {
owner->stop_timer(reconnect_timer);
reconnect_timer = 0;
}
reconnect_timer = owner->start_timer([this](auto handle) {
log(dpp::ll_debug, "Reconnecting shard " + std::to_string(shard_id) + " to wss://" + hostname + "...");
owner->stop_timer(handle);
reconnect_timer = 0;
cleanup();
if (timer_handle) {
owner->stop_timer(timer_handle);
timer_handle = 0;
try {
cleanup();
if (timer_handle) {
owner->stop_timer(timer_handle);
timer_handle = 0;
}
start = time(nullptr);
ssl_client::connect();
start_connecting();
run();
owner->stop_timer(handle);
reconnect_timer = 0;
}
catch (const std::exception &e) {
/* If we get here, the timer will tick again */
ssl_client::close();
end_zlib();
log(dpp::ll_debug, "Error reconnecting shard " + std::to_string(shard_id) + ": " + std::string(e.what()) + "; Retry in 5 seconds...");
}
start = time(nullptr);
ssl_client::connect();
start_connecting();
run();
}, 5);
}

Expand Down
4 changes: 3 additions & 1 deletion src/dpp/wsclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,9 @@ void websocket_client::on_disconnect()

void websocket_client::close()
{
this->on_disconnect();
if (sfd != INVALID_SOCKET) {
this->on_disconnect();
}
this->state = HTTP_HEADERS;
ssl_client::close();
}
Expand Down

0 comments on commit f584b2f

Please sign in to comment.