From 9a62ec61c96516ee39f21c32ee737275316c297a Mon Sep 17 00:00:00 2001 From: Gent Semaj Date: Thu, 19 Mar 2026 22:00:29 -0700 Subject: [PATCH] Set TCP_NODELAY on clients --- src/core/CNProtocol.cpp | 23 ++++++++++++++++------- src/core/CNProtocol.hpp | 2 ++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/core/CNProtocol.cpp b/src/core/CNProtocol.cpp index c96916d..0d7b22f 100644 --- a/src/core/CNProtocol.cpp +++ b/src/core/CNProtocol.cpp @@ -365,14 +365,8 @@ void CNServer::init() { } // attach socket to the port - int opt = 1; -#ifdef _WIN32 - if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt)) != 0) { -#else - if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) != 0) { -#endif + if (!setSocketOption(sock, SOL_SOCKET, SO_REUSEADDR, 1)) { std::cerr << "[FATAL] OpenFusion: setsockopt failed" << std::endl; - printSocketError("setsockopt"); exit(EXIT_FAILURE); } address.sin_family = AF_INET; @@ -414,6 +408,18 @@ void CNServer::init() { CNServer::CNServer() {}; CNServer::CNServer(uint16_t p): port(p) {} +bool CNServer::setSocketOption(SOCKET s, int level, int option, int value) { +#ifdef _WIN32 + if (setsockopt(s, level, option, (const char*)&value, sizeof(value)) != 0) { +#else + if (setsockopt(s, level, option, &value, sizeof(value)) != 0) { +#endif + printSocketError("setsockopt"); + return false; + } + return true; +} + void CNServer::addPollFD(SOCKET s) { fds.push_back({s, POLLIN}); } @@ -462,6 +468,9 @@ void CNServer::start() { continue; } + if (!setSocketOption(newConnectionSocket, IPPROTO_TCP, TCP_NODELAY, 1)) + std::cout << "[WARN] OpenFusion: failed to set TCP_NODELAY on new connection" << std::endl; + if (!setSockNonblocking(sock, newConnectionSocket)) continue; diff --git a/src/core/CNProtocol.hpp b/src/core/CNProtocol.hpp index 1bf5dbc..ffedd7a 100644 --- a/src/core/CNProtocol.hpp +++ b/src/core/CNProtocol.hpp @@ -28,6 +28,7 @@ // posix platform #include #include + #include #include #include #include @@ -239,6 +240,7 @@ protected: bool active = true; + bool setSocketOption(SOCKET s, int level, int option, int value); void addPollFD(SOCKET s); void removePollFD(int i);