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
d6e0e92e
Commit
d6e0e92e
authored
4 years ago
by
Leon Dietrich
Browse files
Options
Downloads
Patches
Plain Diff
add: socketaddr aware ctor for tcp_server_socket
parent
b6613c81
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/net/tcp_server_socket.cpp
+29
-18
29 additions, 18 deletions
src/net/tcp_server_socket.cpp
src/net/tcp_server_socket.hpp
+3
-1
3 additions, 1 deletion
src/net/tcp_server_socket.hpp
with
32 additions
and
19 deletions
src/net/tcp_server_socket.cpp
+
29
−
18
View file @
d6e0e92e
...
...
@@ -22,48 +22,59 @@
#include
"macros.hpp"
#include
"net/socketaddress.hpp"
#include
"net/tcp_client.hpp"
namespace
rmrf
::
net
{
tcp_server_socket
::
tcp_server_socket
(
uint16_t
port
,
incoming_client_listener_type
client_listener_
)
:
ss
{
nullptr
},
client_listener
(
client_listener_
),
number_of_connected_clients
(
0
)
{
auto_fd
raw_
socket_fd
{
socket
(
AF_INET6
,
SOCK_STREAM
,
0
)};
if
(
!
raw_
socket_fd
.
valid
())
{
tcp_server_socket
::
tcp_server_socket
(
socketaddr
socket_identifier
,
incoming_client_listener_type
client_listener_
)
:
ss
{
nullptr
},
client_listener
(
client_listener_
),
number_of_connected_clients
(
0
)
{
auto_fd
socket_fd
{
socket
(
socket_identifier
.
family
()
,
SOCK_STREAM
,
0
)};
if
(
!
socket_fd
.
valid
())
{
// TODO implement propper error handling
throw
netio_exception
(
"Failed to create socket fd."
);
}
sockaddr_in6
addr
;
addr
.
sin6_family
=
AF_INET6
;
addr
.
sin6_port
=
htons
(
port
);
addr
.
sin6_addr
=
IN6ADDR_ANY_INIT
;
socketaddr
sa
{
addr
};
if
(
bind
(
raw_socket_fd
.
get
(),
sa
.
ptr
(),
sa
.
size
())
!=
0
)
{
if
(
bind
(
socket_fd
.
get
(),
socket_identifier
.
ptr
(),
socket_identifier
.
size
())
!=
0
)
{
std
::
string
msg
=
"Failed to bind to all addresses (FIXME)"
;
if
(
port
<
1024
)
{
msg
+=
"
\n
You tried to bind to a port smaller than 1024. Are you root?"
;
}
if
(
socket_identifier
.
family
()
==
AF_INET6
||
socket_identifier
.
family
()
==
AF_INET
)
{
// TODO find a nice way to check for the port
/*
if (port < 1024) {
msg += "\nYou tried to bind to a port smaller than 1024. Are you root?";
}
*/
}
throw
netio_exception
(
msg
);
}
// Append the non blocking flag to the file state of the socket fd.
// This might be linux only. We should check that
fcntl
(
raw_
socket_fd
.
get
(),
F_SETFL
,
fcntl
(
raw_
socket_fd
.
get
(),
F_GETFL
,
0
)
|
O_NONBLOCK
);
if
(
listen
(
raw_
socket_fd
.
get
(),
5
)
==
-
1
)
{
fcntl
(
socket_fd
.
get
(),
F_SETFL
,
fcntl
(
socket_fd
.
get
(),
F_GETFL
,
0
)
|
O_NONBLOCK
);
if
(
listen
(
socket_fd
.
get
(),
5
)
==
-
1
)
{
throw
netio_exception
(
"Failed to enable listening mode for raw socket"
);
}
this
->
ss
=
std
::
make_shared
<
async_server_socket
>
(
std
::
forward
<
auto_fd
>
(
raw_
socket_fd
));
this
->
ss
=
std
::
make_shared
<
async_server_socket
>
(
std
::
forward
<
auto_fd
>
(
socket_fd
));
using
namespace
std
::
placeholders
;
this
->
ss
->
set_accept_handler
(
std
::
bind
(
&
tcp_server_socket
::
await_raw_socket_incomming
,
this
,
_1
,
_2
));
}
static
inline
socketaddr
get_ipv6_socketaddr
(
const
uint16_t
port
)
{
sockaddr_in6
addr
;
addr
.
sin6_family
=
AF_INET6
;
addr
.
sin6_port
=
htons
(
port
);
addr
.
sin6_addr
=
IN6ADDR_ANY_INIT
;
socketaddr
sa
{
addr
};
return
sa
;
}
tcp_server_socket
::
tcp_server_socket
(
const
uint16_t
port
,
incoming_client_listener_type
client_listener_
)
:
tcp_server_socket
{
get_ipv6_socketaddr
(
port
),
client_listener_
}
{
}
// As we're not depending on the actual async server object we need to suppress the warning that we're not using it.
void
tcp_server_socket
::
await_raw_socket_incomming
(
async_server_socket
::
self_ptr_type
ass
,
const
auto_fd
&
socket
)
{
MARK_UNUSED
(
ass
);
...
...
This diff is collapsed.
Click to expand it.
src/net/tcp_server_socket.hpp
+
3
−
1
View file @
d6e0e92e
...
...
@@ -12,6 +12,7 @@
#include
"net/async_server.hpp"
#include
"net/netio_exception.hpp"
#include
"net/socketaddress.hpp"
#include
"net/tcp_client.hpp"
namespace
rmrf
::
net
{
...
...
@@ -25,7 +26,8 @@ private:
incoming_client_listener_type
client_listener
;
std
::
atomic_uint32_t
number_of_connected_clients
;
public:
tcp_server_socket
(
uint16_t
port
,
incoming_client_listener_type
client_listener_
);
tcp_server_socket
(
const
uint16_t
port
,
incoming_client_listener_type
client_listener_
);
tcp_server_socket
(
socketaddr
socket_identifier
,
incoming_client_listener_type
client_listener_
);
int
get_number_of_connected_clients
()
const
;
private:
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