Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Florian Fischer
emper-echo-server
Commits
4a655d69
Commit
4a655d69
authored
Nov 27, 2020
by
Florian Fischer
Browse files
modernize cpp echo server
parent
c95cce55
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/emper-echo-server.cpp
View file @
4a655d69
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright © 2020 Florian Fischer
#include
<errno.h>
#include
<netinet/in.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include
<sys/socket.h>
#include
<unistd.h>
#include
<cerrno>
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
#include
"AsyncLibrary.hpp"
#include
"Common.hpp"
#include
"Fiber.hpp"
#include
"Runtime.hpp"
#include
"config.h"
static
int
port
=
12345
;
static
const
int
DEFAULT_PORT
=
12345
;
static
int
port
=
DEFAULT_PORT
;
[[
noreturn
]]
static
void
die
(
const
char
*
msg
)
{
perror
(
msg
);
...
...
@@ -35,6 +37,7 @@ void client_func(void* arg) {
#endif
// socket was shutdown
if
(
unlikely
(
bytes_recv
==
0
))
{
shutdown
(
client_fd
,
SHUT_RDWR
);
return
;
}
...
...
@@ -49,7 +52,8 @@ void client_func(void* arg) {
ssize_t
bytes_send
=
0
;
while
(
bytes_send
<
bytes_recv
)
{
#ifdef USE_ASYNC_IO
ssize_t
new_bytes_send
=
emper
::
io
::
send_and_wait
(
client_fd
,
buf
,
bytes_recv
-
bytes_send
,
MSG_NOSIGNAL
);
ssize_t
new_bytes_send
=
emper
::
io
::
send_and_wait
(
client_fd
,
buf
,
bytes_recv
-
bytes_send
,
MSG_NOSIGNAL
);
#else
ssize_t
new_bytes_send
=
send
(
client_fd
,
buf
,
bytes_recv
-
bytes_send
,
MSG_NOSIGNAL
);
#endif
...
...
@@ -122,7 +126,7 @@ static void welcome_func() {
}
}
int
main
(
int
argc
,
char
*
argv
[])
{
auto
main
(
int
argc
,
char
*
argv
[])
->
int
{
if
(
argc
>
2
)
{
const
int
decimal
=
10
;
port
=
(
int
)
strtol
(
argv
[
1
],
nullptr
,
decimal
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment