Skip to content
Snippets Groups Projects
Commit d815690a authored by Leon Dietrich's avatar Leon Dietrich Committed by Benny Baumann
Browse files

add: max number of connected clients boundry for tcp server socket

parent 5b733302
No related branches found
No related tags found
1 merge request!1First unit tests
...@@ -33,7 +33,9 @@ tcp_server_socket::tcp_server_socket( ...@@ -33,7 +33,9 @@ tcp_server_socket::tcp_server_socket(
) : ) :
ss{nullptr}, ss{nullptr},
client_listener(client_listener_), client_listener(client_listener_),
number_of_connected_clients(0) overflow_client_listener(nullptr),
number_of_connected_clients(0),
max_number_of_simulataneusly_allowed_clients(0)
{ {
auto_fd socket_fd{socket(socket_identifier.family(), SOCK_STREAM, 0)}; auto_fd socket_fd{socket(socket_identifier.family(), SOCK_STREAM, 0)};
...@@ -108,12 +110,18 @@ void tcp_server_socket::await_raw_socket_incomming( ...@@ -108,12 +110,18 @@ void tcp_server_socket::await_raw_socket_incomming(
// Generate client object from fd and announce it // Generate client object from fd and announce it
this->number_of_connected_clients++; this->number_of_connected_clients++;
using namespace std::placeholders; using namespace std::placeholders;
this->client_listener(tcp_client(std::bind(&tcp_server_socket::client_destructed_cb, this, _1), auto_fd(client_fd_raw), address, port));
if (this->max_number_of_simulataneusly_allowed_clients == 0 || this->get_number_of_connected_clients() <= this->max_number_of_simulataneusly_allowed_clients) {
this->client_listener(tcp_client(std::bind(&tcp_server_socket::client_destructed_cb, this, _1), auto_fd(client_fd_raw), address, port));
} else {
if (this->overflow_client_listener != nullptr) {
this->overflow_client_listener(tcp_client(std::bind(&tcp_server_socket::client_destructed_cb, this, _1), auto_fd(client_fd_raw), address, port));
}
}
} }
int tcp_server_socket::get_number_of_connected_clients() const { unsigned int tcp_server_socket::get_number_of_connected_clients() const {
return this->number_of_connected_clients; return this->number_of_connected_clients;
} }
...@@ -123,4 +131,12 @@ void tcp_server_socket::client_destructed_cb(exit_status_t exit_status) { ...@@ -123,4 +131,12 @@ void tcp_server_socket::client_destructed_cb(exit_status_t exit_status) {
this->number_of_connected_clients--; this->number_of_connected_clients--;
} }
void tcp_server_socket::set_client_overflow_handler(incoming_client_listener_type overflow_client_listener_) {
this->overflow_client_listener = overflow_client_listener_;
}
void tcp_server_socket::set_maximum_concurrent_connections(unsigned int max_connections) {
this->max_number_of_simulataneusly_allowed_clients = max_connections;
}
} }
...@@ -32,7 +32,9 @@ public: ...@@ -32,7 +32,9 @@ public:
private: private:
async_server_socket::self_ptr_type ss; async_server_socket::self_ptr_type ss;
incoming_client_listener_type client_listener; incoming_client_listener_type client_listener;
incoming_client_listener_type overflow_client_listener;
std::atomic_uint32_t number_of_connected_clients; std::atomic_uint32_t number_of_connected_clients;
unsigned int max_number_of_simulataneusly_allowed_clients;
public: public:
/** /**
...@@ -59,7 +61,25 @@ public: ...@@ -59,7 +61,25 @@ public:
* @brief Get the current number of connected clients. * @brief Get the current number of connected clients.
* @return The number of connected clients * @return The number of connected clients
*/ */
int get_number_of_connected_clients() const; unsigned int get_number_of_connected_clients() const;
/**
* This method sets the overflow handler to use if the maximum number of allowed clients was reached.
* The purpose of said handler is to inform the connected client that the connection cant be established
* due to isufficient resources. It should transmit the appropriate error message and drop the client.
* @brief Set the client overflow handler
* @param overflow_client_listener The listener that handles clients that couldn't be accepted
*/
void set_client_overflow_handler(incoming_client_listener_type overflow_client_listener);
/**
* Use this method in order to set the maximum number of allowed connections. Set it to
* 0 in order to disable the limit. If anything other than 0 is set it is highly recommended
* to also set an overflow handler.
* @brief Set the maximum allowed simultaneus connections.
* @param max_connections The maximum number of allowed connections.
*/
void set_maximum_concurrent_connections(unsigned int max_connections);
private: private:
void await_raw_socket_incomming(async_server_socket::self_ptr_type ass, const auto_fd& socket); void await_raw_socket_incomming(async_server_socket::self_ptr_type ass, const auto_fd& socket);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment