Skip to content
GitLab
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
47ceee58
Commit
47ceee58
authored
Nov 05, 2020
by
Florian Fischer
Browse files
use port 12345 as default and merge send_all into client_func
parent
e49c52a0
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/emper-echo-server.c
View file @
47ceee58
...
...
@@ -16,31 +16,13 @@
#define unlikely(x) __builtin_expect(!!(x), 1)
int
port
;
static
int
port
=
12345
;
_Noreturn
void
die
(
const
char
*
msg
)
{
_Noreturn
static
void
die
(
const
char
*
msg
)
{
perror
(
msg
);
exit
(
EXIT_FAILURE
);
}
ssize_t
send_all
(
int
fd
,
const
void
*
buf
,
size_t
len
)
{
size_t
bytes_written
=
0
;
while
(
bytes_written
<
len
)
{
#ifdef USE_ASYNC_IO
ssize_t
new_bytes_written
=
sync_send
(
fd
,
buf
,
len
-
bytes_written
,
MSG_NOSIGNAL
);
#else
ssize_t
new_bytes_written
=
send
(
fd
,
buf
,
len
-
bytes_written
,
MSG_NOSIGNAL
);
#endif
if
(
unlikely
(
new_bytes_written
<
1
))
{
return
new_bytes_written
;
}
bytes_written
+=
new_bytes_written
;
}
return
(
ssize_t
)
bytes_written
;
}
#define BUF_MAX 4096
void
client_func
(
void
*
arg
)
{
int
client_fd
=
*
(
int
*
)
arg
;
...
...
@@ -64,17 +46,27 @@ void client_func(void* arg) {
return
;
}
}
ssize_t
bytes_send
=
send_all
(
client_fd
,
buf
,
bytes_recv
);
// socket was shutdown
if
(
unlikely
(
bytes_send
==
0
))
{
return
;
}
if
(
unlikely
(
bytes_send
==
-
1
))
{
perror
(
"recv failed"
);
if
(
errno
!=
EINTR
)
{
ssize_t
bytes_send
=
0
;
while
(
bytes_send
<
bytes_recv
)
{
#ifdef USE_ASYNC_IO
ssize_t
new_bytes_send
=
sync_send
(
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
// socket was shutdown
if
(
unlikely
(
new_bytes_send
==
0
))
{
return
;
}
if
(
unlikely
(
bytes_send
==
-
1
))
{
if
(
errno
!=
EINTR
)
{
perror
(
"recv failed"
);
return
;
}
}
bytes_send
+=
new_bytes_send
;
}
}
}
...
...
@@ -130,14 +122,11 @@ static void welcome_func() {
}
int
main
(
int
argc
,
char
*
argv
[])
{
if
(
argc
<
2
)
{
printf
(
"Please give a port number: ./%s [port]
\n
"
,
argv
[
0
])
;
exit
(
EXIT_FAILURE
);
if
(
argc
>
1
)
{
const
int
decimal
=
10
;
port
=
(
int
)
strtol
(
argv
[
1
],
NULL
,
decimal
);
}
const
int
decimal
=
10
;
port
=
(
int
)
strtol
(
argv
[
1
],
NULL
,
decimal
);
init_runtime
();
fiber
*
welcome_fiber
=
fiber_from0
(
welcome_func
);
...
...
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment