From 2dbe2629c1acdb8aa24a328124efa2841e61626b Mon Sep 17 00:00:00 2001 From: dongresource Date: Sun, 31 Jul 2022 03:16:07 +0200 Subject: [PATCH] Tweak CNShared * Separate pruning frequency from timeout * Pluralize CNShared map: login -> logins * Increase connection timeout to 15 minutes * Do not deallocate a nullptr in playerEnter() * Kill connections rejected by playerEnter() * Remove redundant inclusions of mutex headers in a few places --- src/PlayerManager.cpp | 11 ++++++++--- src/core/CNShared.cpp | 24 +++++++++--------------- src/core/CNShared.hpp | 6 +++++- src/db/internal.hpp | 6 ------ 4 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/PlayerManager.cpp b/src/PlayerManager.cpp index e3cb3fd..01df718 100644 --- a/src/PlayerManager.cpp +++ b/src/PlayerManager.cpp @@ -211,11 +211,16 @@ static void enterPlayer(CNSocket* sock, CNPacketData* data) { LoginMetadata *lm = CNShared::getLoginMetadata(enter->iEnterSerialKey); if (lm == nullptr) { - delete lm; - std::cout << "[WARN] Refusing invalid REQ_PC_ENTER" << std::endl; + + // send failure packet INITSTRUCT(sP_FE2CL_REP_PC_ENTER_FAIL, fail); sock->sendPacket(fail, P_FE2CL_REP_PC_ENTER_FAIL); + + // kill the connection + sock->kill(); + // no need to call _killConnection(); Player isn't in shard yet + return; } @@ -711,5 +716,5 @@ void PlayerManager::init() { REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_CHANGE_MENTOR, changePlayerGuide); REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_FIRST_USE_FLAG_SET, setFirstUseFlag); - REGISTER_SHARD_TIMER(CNShared::pruneLoginMetadata, CNSHARED_TIMEOUT); + REGISTER_SHARD_TIMER(CNShared::pruneLoginMetadata, CNSHARED_PERIOD); } diff --git a/src/core/CNShared.cpp b/src/core/CNShared.cpp index 5ca6574..d9d4e14 100644 --- a/src/core/CNShared.cpp +++ b/src/core/CNShared.cpp @@ -1,31 +1,25 @@ #include "core/CNShared.hpp" -#if defined(__MINGW32__) && !defined(_GLIBCXX_HAS_GTHREADS) - #include "mingw/mingw.mutex.h" -#else - #include -#endif - -static std::unordered_map login; +static std::unordered_map logins; static std::mutex mtx; void CNShared::storeLoginMetadata(int64_t sk, LoginMetadata *lm) { std::lock_guard lock(mtx); // take ownership of connection data - login[sk] = lm; + logins[sk] = lm; } LoginMetadata* CNShared::getLoginMetadata(int64_t sk) { std::lock_guard lock(mtx); // fail if the key isn't found - if (login.find(sk) == login.end()) + if (logins.find(sk) == logins.end()) return nullptr; // transfer ownership of connection data to shard - LoginMetadata *lm = login[sk]; - login.erase(sk); + LoginMetadata *lm = logins[sk]; + logins.erase(sk); return lm; } @@ -33,8 +27,8 @@ LoginMetadata* CNShared::getLoginMetadata(int64_t sk) { void CNShared::pruneLoginMetadata(CNServer *serv, time_t currTime) { std::lock_guard lock(mtx); - auto it = login.begin(); - while (it != login.end()) { + auto it = logins.begin(); + while (it != logins.end()) { auto& sk = it->first; auto& lm = it->second; @@ -42,8 +36,8 @@ void CNShared::pruneLoginMetadata(CNServer *serv, time_t currTime) { std::cout << "[WARN] Pruning hung connection attempt" << std::endl; // deallocate object and remove map entry - delete login[sk]; - it = login.erase(it); // skip the invalidated iterator + delete logins[sk]; + it = logins.erase(it); // skip the invalidated iterator } else { it++; } diff --git a/src/core/CNShared.hpp b/src/core/CNShared.hpp index e99e07b..a74298c 100644 --- a/src/core/CNShared.hpp +++ b/src/core/CNShared.hpp @@ -10,7 +10,11 @@ #include "Player.hpp" -#define CNSHARED_TIMEOUT 30000 +/* + * Connecions time out after 15 minutes, checked every 30 seconds. + */ +#define CNSHARED_TIMEOUT 900000 +#define CNSHARED_PERIOD 30000 struct LoginMetadata { uint64_t FEKey; diff --git a/src/db/internal.hpp b/src/db/internal.hpp index 22eeb75..376960e 100644 --- a/src/db/internal.hpp +++ b/src/db/internal.hpp @@ -3,12 +3,6 @@ #include "db/Database.hpp" #include -#if defined(__MINGW32__) && !defined(_GLIBCXX_HAS_GTHREADS) - #include "mingw/mingw.mutex.h" -#else - #include -#endif - extern std::mutex dbCrit; extern sqlite3 *db;