Skip to content
Snippets Groups Projects
Commit be5c3e1e authored by Florian Schmaus's avatar Florian Schmaus
Browse files

Merge branch 'echo_server_terminate_runtime' into 'master'

[EchoServer] exit the echo server by terminate the runtime

See merge request !175
parents b5efa992 80150ded
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,7 @@
#include <sys/socket.h>
#include <sys/types.h>
#include <atomic>
#include <cerrno>
#include <chrono>
#include <cstdlib>
......@@ -25,6 +26,8 @@ const std::string PORT = "12345";
unsigned int computations_us = 0;
std::atomic<bool> quit = false;
auto main(int argc, char* argv[]) -> int {
std::string host = HOST;
std::string port = PORT;
......@@ -48,21 +51,20 @@ auto main(int argc, char* argv[]) -> int {
auto* listener = emper::io::tcp_listener(host, port, [](int socket) {
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
char buf[1024];
for (;;) {
while (!quit.load(std::memory_order_consume)) {
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));
}
finish:
emper::io::closeAndForget(socket);
return;
}
break;
if (unlikely(bytes_recv == 5 && strncmp("quit\n", buf, bytes_recv) == 0)) {
exit(EXIT_SUCCESS);
quit = true;
Runtime::getRuntime()->initiateTermination();
break;
}
const auto start = std::chrono::steady_clock::now();
......@@ -76,9 +78,11 @@ auto main(int argc, char* argv[]) -> int {
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;
break;
}
}
emper::io::closeAndForget(socket);
});
if (!listener) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment