Set TCP_NODELAY on clients

This commit is contained in:
2026-03-19 22:00:29 -07:00
parent 51d3cfbb3b
commit 9a62ec61c9
2 changed files with 18 additions and 7 deletions

View File

@@ -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;

View File

@@ -28,6 +28,7 @@
// posix platform
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <poll.h>
#include <unistd.h>
@@ -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);