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);
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);
}

View File

@ -1,31 +1,25 @@
#include "core/CNShared.hpp"
#if defined(__MINGW32__) && !defined(_GLIBCXX_HAS_GTHREADS)
#include "mingw/mingw.mutex.h"
#else
#include <mutex>
#endif
static std::unordered_map<int64_t, LoginMetadata*> login;
static std::unordered_map<int64_t, LoginMetadata*> logins;
static std::mutex mtx;
void CNShared::storeLoginMetadata(int64_t sk, LoginMetadata *lm) {
std::lock_guard<std::mutex> lock(mtx);
// take ownership of connection data
login[sk] = lm;
logins[sk] = lm;
}
LoginMetadata* CNShared::getLoginMetadata(int64_t sk) {
std::lock_guard<std::mutex> 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<std::mutex> 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++;
}

View File

@ -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;

View File

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