Newer
Older
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright © 2020-2021 Florian Fischer
#include <sys/types.h>
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#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) {
LOGE("server read failed:" << strerror(errno));
emper::io::closeAndForget(socket);
return;
}
if (unlikely(bytes_recv == 5 && strncmp("quit\n", buf, bytes_recv) == 0)) {
exit(EXIT_SUCCESS);
}
ssize_t bytes_send = emper::io::sendAndWait(socket, buf, bytes_recv, MSG_NOSIGNAL, true);
if (unlikely(bytes_recv != bytes_send)) {
LOGE("server send failed: " << strerror(errno));
goto finish;
}