From b79f547017e3e47c8e3340d918185c83a3eaf9d3 Mon Sep 17 00:00:00 2001
From: Florian Fischer <florian.fischer@muhq.space>
Date: Fri, 24 Sep 2021 14:11:23 +0200
Subject: [PATCH] [EchoServer] set SO_REUSEPORT on the listen socket

This is needed by emper-io-eval because apparently our startup/termination
times are shorter than the OS allows the rebinding of the same tcp tuple.

Also make all global variables static because they don't have to be exported.
---
 apps/EchoServer.cpp | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/apps/EchoServer.cpp b/apps/EchoServer.cpp
index b88560b6..c64a5949 100644
--- a/apps/EchoServer.cpp
+++ b/apps/EchoServer.cpp
@@ -21,12 +21,13 @@
 #include <compare>
 #endif
 
-const std::string HOST = "::";
-const std::string PORT = "12345";
+static const std::string HOST = "::";
+static const std::string PORT = "12345";
+static const int BACKLOG = 1024;
 
-unsigned int computations_us = 0;
+static unsigned int computations_us = 0;
 
-std::atomic<bool> quit = false;
+static std::atomic<bool> quit = false;
 
 auto main(int argc, char* argv[]) -> int {
 	std::string host = HOST;
@@ -48,7 +49,7 @@ auto main(int argc, char* argv[]) -> int {
 	std::cout << "Echoserver listening on " << host << ":" << port << std::endl;
 
 	Runtime runtime;
-	auto* listener = emper::io::tcp_listener(host, port, [](int socket) {
+	auto serverFunc = [](int socket) {
 		// NOLINTNEXTLINE(modernize-avoid-c-arrays)
 		char buf[1024];
 		while (!quit.load(std::memory_order_consume)) {
@@ -83,7 +84,10 @@ auto main(int argc, char* argv[]) -> int {
 		}
 
 		emper::io::closeAndForget(socket);
-	});
+	};
+
+	auto* listener =
+			emper::io::tcp_listener(host, port, serverFunc, BACKLOG, {emper::io::SockOpt::ReusePort});
 
 	if (!listener) {
 		exit(EXIT_FAILURE);
-- 
GitLab