feat: max clients connected limit in TcpServer

This commit is contained in:
MihailRis
2025-10-05 22:52:55 +03:00
parent 2f15a1a8d4
commit 2895cff5f2
4 changed files with 37 additions and 8 deletions
+9 -8
View File
@@ -164,15 +164,16 @@ void Network::update() {
} }
++socketiter; ++socketiter;
} }
auto serveriter = servers.begin(); }
while (serveriter != servers.end()) { auto serveriter = servers.begin();
auto server = serveriter->second.get(); while (serveriter != servers.end()) {
if (!server->isOpen()) { auto server = serveriter->second.get();
serveriter = servers.erase(serveriter); if (!server->isOpen()) {
continue; serveriter = servers.erase(serveriter);
} continue;
++serveriter;
} }
server->update();
++serveriter;
} }
} }
+2
View File
@@ -37,6 +37,8 @@ namespace network {
[[nodiscard]] TransportType getTransportType() const noexcept override { [[nodiscard]] TransportType getTransportType() const noexcept override {
return TransportType::TCP; return TransportType::TCP;
} }
virtual void setMaxClientsConnected(int count) = 0;
}; };
class UdpServer : public Server { class UdpServer : public Server {
+24
View File
@@ -304,6 +304,7 @@ class SocketTcpServer : public TcpServer {
bool open = true; bool open = true;
std::unique_ptr<std::thread> thread = nullptr; std::unique_ptr<std::thread> thread = nullptr;
int port; int port;
int maxConnected = -1;
public: public:
SocketTcpServer(u64id_t id, Network* network, SOCKET descriptor, int port) SocketTcpServer(u64id_t id, Network* network, SOCKET descriptor, int port)
: id(id), network(network), descriptor(descriptor), port(port) {} : id(id), network(network), descriptor(descriptor), port(port) {}
@@ -312,6 +313,22 @@ public:
closeSocket(); closeSocket();
} }
void setMaxClientsConnected(int count) override {
maxConnected = count;
}
void update() override {
std::vector<u64id_t> clients;
for (u64id_t cid : this->clients) {
if (auto client = network->getConnection(cid, true)) {
if (client->getState() != ConnectionState::CLOSED) {
clients.emplace_back(cid);
}
}
}
std::swap(clients, this->clients);
}
void startListen(ConnectCallback handler) override { void startListen(ConnectCallback handler) override {
thread = std::make_unique<std::thread>([this, handler]() { thread = std::make_unique<std::thread>([this, handler]() {
while (open) { while (open) {
@@ -328,6 +345,11 @@ public:
close(); close();
break; break;
} }
if (maxConnected >= 0 && clients.size() >= maxConnected) {
logger.info() << "refused connection attempt from " << to_string(address);
closesocket(clientDescriptor);
continue;
}
logger.info() << "client connected: " << to_string(address); logger.info() << "client connected: " << to_string(address);
auto socket = std::make_shared<SocketTcpConnection>( auto socket = std::make_shared<SocketTcpConnection>(
clientDescriptor, address clientDescriptor, address
@@ -575,6 +597,8 @@ public:
SocketUdpServer::close(); SocketUdpServer::close();
} }
void update() override {}
void startListen(ServerDatagramCallback handler) override { void startListen(ServerDatagramCallback handler) override {
callback = std::move(handler); callback = std::move(handler);
+2
View File
@@ -78,6 +78,8 @@ namespace network {
class Server { class Server {
public: public:
virtual ~Server() = default; virtual ~Server() = default;
virtual void update() = 0;
virtual void close() = 0; virtual void close() = 0;
virtual bool isOpen() = 0; virtual bool isOpen() = 0;
[[nodiscard]] virtual TransportType getTransportType() const noexcept = 0; [[nodiscard]] virtual TransportType getTransportType() const noexcept = 0;