Skip to content
Snippets Groups Projects
EchoServer.cpp 2.27 KiB
Newer Older
  • Learn to ignore specific revisions
  • // SPDX-License-Identifier: LGPL-3.0-or-later
    // Copyright © 2020-2021 Florian Fischer
    
    #include <sys/socket.h>
    
    #include <sys/types.h>
    
    
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    #include <string>
    
    #include "Common.hpp"
    
    #include "Debug.hpp"
    
    #include "Runtime.hpp"
    
    #include "io.hpp"
    
    
    #ifdef EMPER_HAS_COMPARE_H
    #include <compare>
    #endif
    
    
    const std::string HOST = "::";
    const std::string PORT = "12345";
    
    auto main(int argc, char* argv[]) -> int {
    	std::string host = HOST;
    
    	std::string port = PORT;
    
    	if (argc > 3) {
    		std::cerr << "Usage: " << argv[0] << " [port] [computation_us]" << std::endl;
    
    		exit(EXIT_FAILURE);
    	}
    
    	if (argc > 1) {
    
    		port = std::string(argv[1]);
    
    	if (argc > 2) {
    		computations_us = std::stoi(argv[2]);
    	}
    
    
    	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];
    
    		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));
    
    			}
    
    			if (unlikely(bytes_recv == 5 && strncmp("quit\n", buf, bytes_recv) == 0)) {
    
    				quit = true;
    				Runtime::getRuntime()->initiateTermination();
    				break;
    
    			const auto start = std::chrono::steady_clock::now();
    			const auto deadline = start + std::chrono::microseconds(computations_us);
    			// TODO: The suppressed linter error below may be a false positive
    			// reported by clang-tidy.
    			// NOLINTNEXTLINE(modernize-use-nullptr)
    			while (std::chrono::steady_clock::now() < deadline) {
    			}
    
    
    			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));
    
    
    		emper::io::closeAndForget(socket);
    
    	});
    
    	if (!listener) {
    		exit(EXIT_FAILURE);
    	}
    
    	runtime.scheduleFromAnywhere(*listener);
    
    	runtime.waitUntilFinished();
    
    	return EXIT_FAILURE;
    }