mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-07-12 17:54:04 +00:00
implement connection limiter to replace manual limiting code
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
#pragma once
|
||||
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
|
||||
class TConnectionLimiter {
|
||||
public:
|
||||
class TGuard {
|
||||
public:
|
||||
TGuard() = default;
|
||||
TGuard(TConnectionLimiter* owner, std::string ip);
|
||||
|
||||
TGuard(const TGuard&) = delete;
|
||||
TGuard& operator=(const TGuard&) = delete;
|
||||
|
||||
~TGuard() { Release(); }
|
||||
|
||||
// not threadsafe
|
||||
TGuard(TGuard&& other) noexcept { *this = std::move(other); }
|
||||
// not threadsafe
|
||||
TGuard& operator=(TGuard&& other) noexcept;
|
||||
|
||||
private:
|
||||
friend class TConnectionLimiter;
|
||||
|
||||
// not threadsafe
|
||||
void Release();
|
||||
|
||||
TConnectionLimiter* mOwner { nullptr };
|
||||
std::string mIp;
|
||||
};
|
||||
|
||||
TConnectionLimiter(size_t maxPerIp, size_t maxGlobal);
|
||||
|
||||
[[nodiscard]] std::optional<TGuard> TryAcquire(const std::string& ip);
|
||||
|
||||
private:
|
||||
void Release(const std::string& ip) {
|
||||
std::unique_lock Lock { mMutex };
|
||||
auto It = mPerIp.find(ip);
|
||||
if (It != mPerIp.end()) {
|
||||
// this guard exists to avoid underflow in case something goes wrong and this gets called too many times
|
||||
if (It->second > 0)
|
||||
--It->second;
|
||||
if (It->second == 0)
|
||||
mPerIp.erase(It);
|
||||
}
|
||||
// this guard exists to avoid underflow in case something goes wrong and this gets called too many times
|
||||
if (mGlobal > 0)
|
||||
--mGlobal;
|
||||
}
|
||||
|
||||
const size_t mMaxPerIp;
|
||||
const size_t mMaxGlobal;
|
||||
|
||||
std::mutex mMutex { };
|
||||
std::unordered_map<std::string, size_t> mPerIp { };
|
||||
size_t mGlobal = 0;
|
||||
};
|
||||
+3
-3
@@ -20,6 +20,7 @@
|
||||
|
||||
#include "BoostAliases.h"
|
||||
#include "Compat.h"
|
||||
#include "TConnectionLimiter.h"
|
||||
#include "TResourceManager.h"
|
||||
#include "TServer.h"
|
||||
#include <boost/asio/io_context.hpp>
|
||||
@@ -40,7 +41,7 @@ public:
|
||||
void DisconnectClient(const std::weak_ptr<TClient>& c, const std::string& R);
|
||||
void DisconnectClient(TClient& c, const std::string& R);
|
||||
[[nodiscard]] bool SyncClient(const std::weak_ptr<TClient>& c);
|
||||
void Identify(TConnection&& client);
|
||||
void Identify(TConnection&& client, TConnectionLimiter::TGuard&&);
|
||||
std::shared_ptr<TClient> Authentication(TConnection&& ClientConnection);
|
||||
void SyncResources(TClient& c);
|
||||
[[nodiscard]] bool UDPSend(TClient& Client, std::vector<uint8_t> Data);
|
||||
@@ -61,8 +62,7 @@ private:
|
||||
std::thread mUDPThread;
|
||||
std::thread mTCPThread;
|
||||
std::mutex mOpenIDMutex;
|
||||
std::map<std::string, uint16_t> mClientMap;
|
||||
std::mutex mClientMapMutex;
|
||||
TConnectionLimiter mConnectionLimiter;
|
||||
|
||||
std::vector<uint8_t> UDPRcvFromClient(boost::asio::ip::udp::endpoint& ClientEndpoint);
|
||||
void OnConnect(const std::weak_ptr<TClient>& c);
|
||||
|
||||
Reference in New Issue
Block a user