Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
emper
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
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Florian Schmaus
emper
Commits
e34f1f5d
Commit
e34f1f5d
authored
4 years ago
by
Florian Fischer
Browse files
Options
Downloads
Patches
Plain Diff
[IO] add a simple echo server app
parent
460c2f05
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
apps/EchoServer.cpp
+72
-0
72 additions, 0 deletions
apps/EchoServer.cpp
apps/meson.build
+6
-0
6 additions, 0 deletions
apps/meson.build
with
78 additions
and
0 deletions
apps/EchoServer.cpp
0 → 100644
+
72
−
0
View file @
e34f1f5d
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright © 2020-2021 Florian Fischer
#include
<sys/types.h>
#include
<cassert>
#include
<climits>
#include
<cstdlib>
#include
<cstring>
#include
<iostream>
#include
<string>
#include
"Common.hpp"
#include
"Runtime.hpp"
#include
"emper-common.h"
#include
"io.hpp"
const
int
DECIMAL
=
10
;
const
std
::
string
HOST
=
"0.0.0.0"
;
const
int
PORT
=
12345
;
auto
main
(
int
argc
,
char
*
argv
[])
->
int
{
int
port
=
PORT
;
std
::
string
host
=
HOST
;
if
(
argc
>
2
)
{
std
::
cerr
<<
"Usage: "
<<
argv
[
0
]
<<
" [port]"
<<
std
::
endl
;
exit
(
EXIT_FAILURE
);
}
if
(
argc
>
1
)
{
long
aport
=
strtol
(
argv
[
1
],
nullptr
,
DECIMAL
);
assert
(
aport
<=
INT_MAX
&&
aport
>=
INT_MIN
);
port
=
(
int
)
aport
;
}
std
::
cout
<<
"Echoserver listening on "
<<
host
<<
":"
<<
port
<<
std
::
endl
;
Runtime
runtime
;
auto
*
listener
=
emper
::
io
::
tcp_listener
(
host
,
port
,
[](
int
socket
)
{
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
char
buf
[
1024
];
for
(;;)
{
ssize_t
bytes_recv
=
emper
::
io
::
recvAndWait
(
socket
,
buf
,
sizeof
(
buf
),
0
);
if
(
unlikely
(
bytes_recv
<=
0
))
{
// socket was shutdown
if
(
bytes_recv
<
0
)
{
DIE_MSG_ERRNO
(
"server read failed"
);
}
emper
::
io
::
closeAndForget
(
socket
);
return
;
}
if
(
unlikely
(
bytes_recv
==
5
&&
strncmp
(
"quit
\n
"
,
buf
,
bytes_recv
)
==
0
))
{
exit
(
EXIT_SUCCESS
);
}
ATTR_UNUSED
ssize_t
bytes_send
=
emper
::
io
::
sendAndWait
(
socket
,
buf
,
bytes_recv
,
0
);
assert
(
bytes_recv
==
bytes_send
);
}
});
if
(
!
listener
)
{
exit
(
EXIT_FAILURE
);
}
runtime
.
scheduleFromAnywhere
(
*
listener
);
runtime
.
waitUntilFinished
();
return
EXIT_FAILURE
;
}
This diff is collapsed.
Click to expand it.
apps/meson.build
+
6
−
0
View file @
e34f1f5d
...
...
@@ -9,3 +9,9 @@ worker_sleep_example_exe = executable(
'WorkerSleepExample.cpp'
,
dependencies
:
emper_dep
,
)
echoserver_exe
=
executable
(
'echoserver'
,
'EchoServer.cpp'
,
dependencies
:
emper_dep
,
)
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