diff --git a/include/dpp/discordclient.h b/include/dpp/discordclient.h index fd5d1812aa..1be08ae3b6 100644 --- a/include/dpp/discordclient.h +++ b/include/dpp/discordclient.h @@ -180,6 +180,11 @@ class DPP_EXPORT discord_client : public websocket_client */ void start_connecting(); + /** + * @brief Timer for reconnecting + */ + timer reconnect_timer{0}; + private: /** diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index c57394f739..4d1933ae0c 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -227,7 +227,7 @@ if(MSVC) string(REGEX REPLACE "/W[1|2|3|4]" "/W3" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") else() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall -Wno-unused-private-field -Wno-psabi -Wempty-body -Wignored-qualifiers -Wimplicit-fallthrough -Wmissing-field-initializers -Wsign-compare -Wtype-limits -Wuninitialized -Wshift-negative-value -pthread") - set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Og") + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g") if (NOT MINGW) add_link_options("-rdynamic") diff --git a/src/dpp/discordclient.cpp b/src/dpp/discordclient.cpp index 37d703d59e..55a6029619 100644 --- a/src/dpp/discordclient.cpp +++ b/src/dpp/discordclient.cpp @@ -114,6 +114,8 @@ void discord_client::cleanup() { delete etf; delete zlib; + etf = nullptr; + zlib = nullptr; } discord_client::~discord_client() @@ -127,9 +129,15 @@ void discord_client::on_disconnect() log(dpp::ll_debug, "Lost connection to websocket on shard " + std::to_string(shard_id) + ", reconnecting in 5 seconds..."); ssl_client::close(); end_zlib(); - owner->start_timer([this](auto handle) { + /* Stop the timer first if its already ticking, to prevent concurrent reconnects */ + if (reconnect_timer) { + 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); diff --git a/src/dpp/socketengines/epoll.cpp b/src/dpp/socketengines/epoll.cpp index 7418f6c7fd..e36f825158 100644 --- a/src/dpp/socketengines/epoll.cpp +++ b/src/dpp/socketengines/epoll.cpp @@ -146,7 +146,7 @@ struct DPP_EXPORT socket_engine_epoll : public socket_engine_base { ev.events |= EPOLLERR; } { - std::unique_lock lock(fds_mutex); + std::shared_lock lock(fds_mutex); ev.data.ptr = fds.find(e.fd)->second.get(); } return epoll_ctl(epoll_handle, EPOLL_CTL_ADD, e.fd, &ev) >= 0; @@ -169,7 +169,7 @@ struct DPP_EXPORT socket_engine_epoll : public socket_engine_base { ev.events |= EPOLLERR; } { - std::unique_lock lock(fds_mutex); + std::shared_lock lock(fds_mutex); ev.data.ptr = fds.find(e.fd)->second.get(); } return epoll_ctl(epoll_handle, EPOLL_CTL_MOD, e.fd, &ev) >= 0;