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
98ba9fd3
Verified
Commit
98ba9fd3
authored
4 years ago
by
Benny Baumann
Browse files
Options
Downloads
Patches
Plain Diff
chg: Use auto_fd as early as possible
parent
e1142588
No related branches found
Branches containing commit
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
+22
-15
22 additions, 15 deletions
src/net/tcp_server_socket.cpp
src/net/tcp_server_socket.hpp
+1
-1
1 addition, 1 deletion
src/net/tcp_server_socket.hpp
with
23 additions
and
16 deletions
src/net/tcp_server_socket.cpp
+
22
−
15
View file @
98ba9fd3
...
@@ -4,53 +4,60 @@
...
@@ -4,53 +4,60 @@
* Created on: 02.01.2021
* Created on: 02.01.2021
* Author: doralitze
* Author: doralitze
*/
*/
#include
"net/tcp_server_socket.hpp"
#include
"net/tcp_server_socket.hpp"
#include
<arpa/inet.h>
#include
<unistd.h>
#include
<fcntl.h>
#include
<fcntl.h>
#include
<functional>
#include
<stdlib.h>
#include
<stdlib.h>
#include
<string.h>
#include
<string.h>
#include
<unistd.h>
#include
<arpa/inet.h>
#include
<sys/types.h>
#include
<sys/types.h>
#include
<sys/socket.h>
#include
<sys/socket.h>
#include
<sys/stat.h>
#include
<sys/stat.h>
#include
<netinet/in.h>
#include
<netinet/in.h>
#include
<functional>
#include
"macros.hpp"
#include
"macros.hpp"
#include
"net/socketaddress.hpp"
namespace
rmrf
::
net
{
namespace
rmrf
::
net
{
tcp_server_socket
::
tcp_server_socket
(
uint16_t
port
,
incoming_client_listener_type
client_listener_
)
:
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
)
{
ss
{
nullptr
},
client_listener
(
client_listener_
),
number_of_connected_clients
(
0
)
{
auto
raw_socket_fd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
auto
_fd
raw_socket_fd
{
socket
(
AF_INET
6
,
SOCK_STREAM
,
0
)
}
;
if
(
raw_socket_fd
<
0
)
{
if
(
!
raw_socket_fd
.
valid
()
)
{
// TODO implement propper error handling
// TODO implement propper error handling
throw
netio_exception
(
"Failed to create socket fd."
);
throw
netio_exception
(
"Failed to create socket fd."
);
}
}
struct
sockaddr_in
addr
;
sockaddr_in6
addr
;
addr
.
sin_family
=
AF_INET
;
addr
.
sin6_family
=
AF_INET6
;
addr
.
sin_port
=
htons
(
port
);
addr
.
sin6_port
=
htons
(
port
);
addr
.
sin_addr
.
s_addr
=
INADDR_ANY
;
// TODO FIXME
addr
.
sin6_addr
=
IN6ADDR_ANY_INIT
;
if
(
bind
(
raw_socket_fd
,
(
struct
sockaddr
*
)
&
addr
,
sizeof
(
addr
))
!=
0
)
{
socketaddr
sa
{
addr
};
if
(
bind
(
raw_socket_fd
.
get
(),
sa
.
ptr
(),
sa
.
size
())
!=
0
)
{
std
::
string
msg
=
"Failed to bind to all addresses (FIXME)"
;
std
::
string
msg
=
"Failed to bind to all addresses (FIXME)"
;
if
(
port
<
1024
)
{
if
(
port
<
1024
)
{
msg
+=
"
\
t
You tried to bind to a port smaller than 1024. Are you root?"
;
msg
+=
"
\
n
You tried to bind to a port smaller than 1024. Are you root?"
;
}
}
throw
netio_exception
(
msg
);
throw
netio_exception
(
msg
);
}
}
// Append the non blocking flag to the file state of the socket fd.
// Append the non blocking flag to the file state of the socket fd.
// This might be linux only. We should check that
// This might be linux only. We should check that
fcntl
(
raw_socket_fd
,
F_SETFL
,
fcntl
(
raw_socket_fd
,
F_GETFL
,
0
)
|
O_NONBLOCK
);
fcntl
(
raw_socket_fd
.
get
()
,
F_SETFL
,
fcntl
(
raw_socket_fd
.
get
()
,
F_GETFL
,
0
)
|
O_NONBLOCK
);
if
(
listen
(
raw_socket_fd
,
5
)
==
-
1
)
{
if
(
listen
(
raw_socket_fd
.
get
()
,
5
)
==
-
1
)
{
throw
netio_exception
(
"Failed to enable listening mode for raw socket"
);
throw
netio_exception
(
"Failed to enable listening mode for raw socket"
);
}
}
this
->
ss
=
std
::
make_shared
<
async_server_socket
>
(
auto_fd
(
raw_socket_fd
));
this
->
ss
=
std
::
make_shared
<
async_server_socket
>
(
std
::
forward
<
auto_fd
>
(
raw_socket_fd
));
using
namespace
std
::
placeholders
;
using
namespace
std
::
placeholders
;
this
->
ss
->
set_accept_handler
(
std
::
bind
(
&
tcp_server_socket
::
await_raw_socket_incomming
,
this
,
_1
,
_2
));
this
->
ss
->
set_accept_handler
(
std
::
bind
(
&
tcp_server_socket
::
await_raw_socket_incomming
,
this
,
_1
,
_2
));
}
}
...
...
This diff is collapsed.
Click to expand it.
src/net/tcp_server_socket.hpp
+
1
−
1
View file @
98ba9fd3
...
@@ -20,7 +20,7 @@ class tcp_server_socket : public std::enable_shared_from_this<tcp_server_socket>
...
@@ -20,7 +20,7 @@ class tcp_server_socket : public std::enable_shared_from_this<tcp_server_socket>
public:
public:
typedef
std
::
function
<
void
(
tcp_client
)
>
incoming_client_listener_type
;
typedef
std
::
function
<
void
(
tcp_client
)
>
incoming_client_listener_type
;
private:
private:
std
::
shared_ptr
<
async_server_socket
>
ss
;
async_server_socket
::
self_ptr_type
ss
;
incoming_client_listener_type
client_listener
;
incoming_client_listener_type
client_listener
;
int
number_of_connected_clients
;
int
number_of_connected_clients
;
public:
public:
...
...
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