2021-03-17 19:07:40 +00:00
|
|
|
#include "core/CNShared.hpp"
|
2020-08-18 20:42:30 +00:00
|
|
|
|
2020-08-19 20:42:44 +00:00
|
|
|
#if defined(__MINGW32__) && !defined(_GLIBCXX_HAS_GTHREADS)
|
2020-08-18 20:42:30 +00:00
|
|
|
#include "mingw/mingw.mutex.h"
|
2020-09-14 13:53:48 +00:00
|
|
|
#else
|
2020-08-18 20:42:30 +00:00
|
|
|
#include <mutex>
|
|
|
|
#endif
|
|
|
|
|
2022-07-23 22:16:04 +00:00
|
|
|
static std::unordered_map<int64_t, LoginMetadata*> login;
|
|
|
|
static std::mutex mtx;
|
2020-08-18 20:42:30 +00:00
|
|
|
|
2022-07-23 22:16:04 +00:00
|
|
|
void CNShared::storeLoginMetadata(int64_t sk, LoginMetadata *lm) {
|
|
|
|
std::lock_guard<std::mutex> lock(mtx);
|
|
|
|
|
|
|
|
// take ownership of connection data
|
|
|
|
login[sk] = lm;
|
2020-08-18 20:42:30 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 22:16:04 +00:00
|
|
|
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())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// transfer ownership of connection data to shard
|
|
|
|
LoginMetadata *lm = login[sk];
|
|
|
|
login.erase(sk);
|
2020-08-19 17:22:54 +00:00
|
|
|
|
2022-07-23 22:16:04 +00:00
|
|
|
return lm;
|
2020-08-18 20:42:30 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 22:16:04 +00:00
|
|
|
void CNShared::pruneLoginMetadata(CNServer *serv, time_t currTime) {
|
|
|
|
std::lock_guard<std::mutex> lock(mtx);
|
|
|
|
|
|
|
|
auto it = login.begin();
|
|
|
|
while (it != login.end()) {
|
|
|
|
auto& sk = it->first;
|
|
|
|
auto& lm = it->second;
|
|
|
|
|
|
|
|
if (lm->timestamp + CNSHARED_TIMEOUT > currTime) {
|
|
|
|
std::cout << "[WARN] Pruning hung connection attempt" << std::endl;
|
2020-08-18 20:42:30 +00:00
|
|
|
|
2022-07-23 22:16:04 +00:00
|
|
|
// deallocate object and remove map entry
|
|
|
|
delete login[sk];
|
|
|
|
it = login.erase(it); // skip the invalidated iterator
|
|
|
|
} else {
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
}
|
2020-08-18 20:42:30 +00:00
|
|
|
}
|