mirror of
https://github.com/OpenFusionProject/OpenFusion.git
synced 2024-11-17 03:20:06 +00:00
dongresource
100b4605ec
Revisiting this again; the issue was that the comparison operator was facing the wrong way, so connections were being pruned every 30 seconds or less. This was effectively a race condition kicking an unlucky player every so often when pruning happened exactly during an attempt to enter the game. Now that the proper timeout is being enforced, I've reduced it to 5 minutes, down from 15, since it really doesn't need to be that long.
46 lines
1.2 KiB
C++
46 lines
1.2 KiB
C++
#include "core/CNShared.hpp"
|
|
|
|
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
|
|
logins[sk] = lm;
|
|
}
|
|
|
|
LoginMetadata* CNShared::getLoginMetadata(int64_t sk) {
|
|
std::lock_guard<std::mutex> lock(mtx);
|
|
|
|
// fail if the key isn't found
|
|
if (logins.find(sk) == logins.end())
|
|
return nullptr;
|
|
|
|
// transfer ownership of connection data to shard
|
|
LoginMetadata *lm = logins[sk];
|
|
logins.erase(sk);
|
|
|
|
return lm;
|
|
}
|
|
|
|
void CNShared::pruneLoginMetadata(CNServer *serv, time_t currTime) {
|
|
std::lock_guard<std::mutex> lock(mtx);
|
|
|
|
auto it = logins.begin();
|
|
while (it != logins.end()) {
|
|
auto& sk = it->first;
|
|
auto& lm = it->second;
|
|
|
|
if (currTime > lm->timestamp + CNSHARED_TIMEOUT) {
|
|
std::cout << "[WARN] Pruning hung connection attempt" << std::endl;
|
|
|
|
// deallocate object and remove map entry
|
|
delete logins[sk];
|
|
it = logins.erase(it); // skip the invalidated iterator
|
|
} else {
|
|
it++;
|
|
}
|
|
}
|
|
}
|