From 62133aa50589cbe3d5f9cc7e96d0c0df3005a595 Mon Sep 17 00:00:00 2001 From: Edo Date: Tue, 10 Jan 2023 19:37:20 +0000 Subject: [PATCH] [General]: Cleanup the code (#713) --- src/Components/Modules/Threading.cpp | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/Components/Modules/Threading.cpp b/src/Components/Modules/Threading.cpp index 0f77dea2..b8d2347e 100644 --- a/src/Components/Modules/Threading.cpp +++ b/src/Components/Modules/Threading.cpp @@ -5,38 +5,37 @@ namespace Components { namespace FrameTime { - void NetSleep(int mSec) + void NetSleep(int ms) { - if (mSec < 0) mSec = 0; + if (ms < 0) ms = 0; fd_set fdr; FD_ZERO(&fdr); - auto highestFd = INVALID_SOCKET; + auto maxfd = INVALID_SOCKET; if (*Game::ip_socket != INVALID_SOCKET) { FD_SET(*Game::ip_socket, &fdr); - highestFd = *Game::ip_socket; + maxfd = *Game::ip_socket; } - if (highestFd == INVALID_SOCKET) + if (maxfd == INVALID_SOCKET) { - // windows ain't happy when select is called without valid FDs - std::this_thread::sleep_for(std::chrono::milliseconds(mSec)); + // On Windows, select fails if no sockets + Game::Sys_Sleep(ms); return; } - timeval timeout; - timeout.tv_sec = mSec / 1000; - timeout.tv_usec = (mSec % 1000) * 1000; + timeval tv = { ms / 1000, (ms % 1000) * 1000 }; + const auto result = ::select(maxfd + 1, &fdr, nullptr, nullptr, &tv); - const auto retVal = ::select(highestFd + 1, &fdr, nullptr, nullptr, &timeout); - - if (retVal == SOCKET_ERROR) + if (result == SOCKET_ERROR) { Logger::Warning(Game::CON_CHANNEL_SYSTEM, "WinAPI: select failed: {}\n", Game::NET_ErrorString()); + return; } - else if (retVal > 0) + + if (result > 0) { if (Dedicated::IsRunning()) {