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
This commit is contained in:
dongresource 2022-07-31 03:16:07 +02:00
parent 271eef83d3
commit 2dbe2629c1
4 changed files with 22 additions and 25 deletions

View File

@ -211,11 +211,16 @@ static void enterPlayer(CNSocket* sock, CNPacketData* data) {
LoginMetadata *lm = CNShared::getLoginMetadata(enter->iEnterSerialKey); LoginMetadata *lm = CNShared::getLoginMetadata(enter->iEnterSerialKey);
if (lm == nullptr) { if (lm == nullptr) {
delete lm;
std::cout << "[WARN] Refusing invalid REQ_PC_ENTER" << std::endl; std::cout << "[WARN] Refusing invalid REQ_PC_ENTER" << std::endl;
// send failure packet
INITSTRUCT(sP_FE2CL_REP_PC_ENTER_FAIL, fail); INITSTRUCT(sP_FE2CL_REP_PC_ENTER_FAIL, fail);
sock->sendPacket(fail, P_FE2CL_REP_PC_ENTER_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; 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_CHANGE_MENTOR, changePlayerGuide);
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_FIRST_USE_FLAG_SET, setFirstUseFlag); 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);
} }

View File

@ -1,31 +1,25 @@
#include "core/CNShared.hpp" #include "core/CNShared.hpp"
#if defined(__MINGW32__) && !defined(_GLIBCXX_HAS_GTHREADS) static std::unordered_map<int64_t, LoginMetadata*> logins;
#include "mingw/mingw.mutex.h"
#else
#include <mutex>
#endif
static std::unordered_map<int64_t, LoginMetadata*> login;
static std::mutex mtx; static std::mutex mtx;
void CNShared::storeLoginMetadata(int64_t sk, LoginMetadata *lm) { void CNShared::storeLoginMetadata(int64_t sk, LoginMetadata *lm) {
std::lock_guard<std::mutex> lock(mtx); std::lock_guard<std::mutex> lock(mtx);
// take ownership of connection data // take ownership of connection data
login[sk] = lm; logins[sk] = lm;
} }
LoginMetadata* CNShared::getLoginMetadata(int64_t sk) { LoginMetadata* CNShared::getLoginMetadata(int64_t sk) {
std::lock_guard<std::mutex> lock(mtx); std::lock_guard<std::mutex> lock(mtx);
// fail if the key isn't found // fail if the key isn't found
if (login.find(sk) == login.end()) if (logins.find(sk) == logins.end())
return nullptr; return nullptr;
// transfer ownership of connection data to shard // transfer ownership of connection data to shard
LoginMetadata *lm = login[sk]; LoginMetadata *lm = logins[sk];
login.erase(sk); logins.erase(sk);
return lm; return lm;
} }
@ -33,8 +27,8 @@ LoginMetadata* CNShared::getLoginMetadata(int64_t sk) {
void CNShared::pruneLoginMetadata(CNServer *serv, time_t currTime) { void CNShared::pruneLoginMetadata(CNServer *serv, time_t currTime) {
std::lock_guard<std::mutex> lock(mtx); std::lock_guard<std::mutex> lock(mtx);
auto it = login.begin(); auto it = logins.begin();
while (it != login.end()) { while (it != logins.end()) {
auto& sk = it->first; auto& sk = it->first;
auto& lm = it->second; 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; std::cout << "[WARN] Pruning hung connection attempt" << std::endl;
// deallocate object and remove map entry // deallocate object and remove map entry
delete login[sk]; delete logins[sk];
it = login.erase(it); // skip the invalidated iterator it = logins.erase(it); // skip the invalidated iterator
} else { } else {
it++; it++;
} }

View File

@ -10,7 +10,11 @@
#include "Player.hpp" #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 { struct LoginMetadata {
uint64_t FEKey; uint64_t FEKey;

View File

@ -3,12 +3,6 @@
#include "db/Database.hpp" #include "db/Database.hpp"
#include <sqlite3.h> #include <sqlite3.h>
#if defined(__MINGW32__) && !defined(_GLIBCXX_HAS_GTHREADS)
#include "mingw/mingw.mutex.h"
#else
#include <mutex>
#endif
extern std::mutex dbCrit; extern std::mutex dbCrit;
extern sqlite3 *db; extern sqlite3 *db;