From 53d8cb67ba4a067da2e0dcc8d848f340b30003e5 Mon Sep 17 00:00:00 2001 From: dongresource Date: Sat, 5 Dec 2020 23:16:09 +0100 Subject: [PATCH] Set monitor connections to non-blocking Better to just drop monitor connections when the quality is bad than to risk blocking the shard thread. --- src/CNProtocol.cpp | 2 +- src/CNProtocol.hpp | 2 ++ src/Monitor.cpp | 12 ++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/CNProtocol.cpp b/src/CNProtocol.cpp index 00e4799..fc61ac3 100644 --- a/src/CNProtocol.cpp +++ b/src/CNProtocol.cpp @@ -214,7 +214,7 @@ void CNSocket::step() { } } -static bool setSockNonblocking(SOCKET listener, SOCKET newSock) { +bool setSockNonblocking(SOCKET listener, SOCKET newSock) { #ifdef _WIN32 unsigned long mode = 1; if (ioctlsocket(newSock, FIONBIO, &mode) != 0) { diff --git a/src/CNProtocol.hpp b/src/CNProtocol.hpp index 80c2ac2..4300c34 100644 --- a/src/CNProtocol.hpp +++ b/src/CNProtocol.hpp @@ -117,6 +117,8 @@ inline bool validInVarPacket(size_t base, int32_t npayloads, size_t plsize, size return true; } +bool setSockNonblocking(SOCKET listener, SOCKET newSock); + namespace CNSocketEncryption { // you won't believe how complicated they made it in the client :facepalm: static constexpr const char* defaultKey = "m@rQn~W#"; diff --git a/src/Monitor.cpp b/src/Monitor.cpp index f939672..7ec471a 100644 --- a/src/Monitor.cpp +++ b/src/Monitor.cpp @@ -46,6 +46,16 @@ void Monitor::init() { exit(1); } +#ifdef _WIN32 + unsigned long mode = 1; + if (ioctlsocket(listener, FIONBIO, &mode) != 0) { +#else + if (fcntl(listener, F_SETFL, (fcntl(listener, F_GETFL, 0) | O_NONBLOCK)) != 0) { +#endif + std::cerr << "[FATAL] OpenFusion: fcntl failed" << std::endl; + exit(EXIT_FAILURE); + } + std::cout << "Monitor listening on *:" << settings::MONITORPORT << std::endl; REGISTER_SHARD_TIMER(tick, settings::MONITORINTERVAL); @@ -117,6 +127,8 @@ void Monitor::start(void *unused) { if (SOCKETERROR(sock)) continue; + setSockNonblocking(listener, sock); + std::cout << "[INFO] New monitor connection from " << inet_ntoa(address.sin_addr) << std::endl; {