Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
code
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ReadMailReallyFast
code
Commits
d815690a
Commit
d815690a
authored
3 years ago
by
Leon Dietrich
Committed by
Benny Baumann
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
add: max number of connected clients boundry for tcp server socket
parent
5b733302
No related branches found
No related tags found
1 merge request
!1
First unit tests
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/net/tcp_server_socket.cpp
+20
-4
20 additions, 4 deletions
src/net/tcp_server_socket.cpp
src/net/tcp_server_socket.hpp
+21
-1
21 additions, 1 deletion
src/net/tcp_server_socket.hpp
with
41 additions
and
5 deletions
src/net/tcp_server_socket.cpp
+
20
−
4
View file @
d815690a
...
@@ -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
;
}
}
}
This diff is collapsed.
Click to expand it.
src/net/tcp_server_socket.hpp
+
21
−
1
View file @
d815690a
...
@@ -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
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment