From 940d824be0a4d0f62f6819cf890c4b1a05a104c4 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Sun, 26 Sep 2021 14:54:24 +0900 Subject: [PATCH] [util] Split out the select code to Sys_Select While select itself is fairly portable, it's not super convenient. --- include/QF/sys.h | 2 ++ libs/util/sys.c | 35 ++++++++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/include/QF/sys.h b/include/QF/sys.h index abf7d4e37..a012062bc 100644 --- a/include/QF/sys.h +++ b/include/QF/sys.h @@ -37,6 +37,7 @@ #include #include #include +#include extern struct cvar_s *sys_nostdout; extern struct cvar_s *sys_extrasleep; @@ -100,6 +101,7 @@ enum { #include "QF/sys_developer.h" }; +int Sys_Select (int maxfd, fd_set *fdset, int64_t usec); int Sys_CheckInput (int idle, int net_socket); const char *Sys_ConsoleInput (void); diff --git a/libs/util/sys.c b/libs/util/sys.c index 9aad2ee6b..5f1de8986 100644 --- a/libs/util/sys.c +++ b/libs/util/sys.c @@ -720,13 +720,32 @@ Sys_DebugLog (const char *file, const char *fmt, ...) } } +VISIBLE int +Sys_Select (int maxfd, fd_set *fdset, int64_t usec) +{ + struct timeval _timeout; + struct timeval *timeout = 0; + + if (usec >= 0) { + timeout = &_timeout; + if (usec < 1000000) { + _timeout.tv_sec = 0; + _timeout.tv_usec = usec; + } else { + _timeout.tv_sec = usec / 1000000; + _timeout.tv_usec = usec % 1000000; + } + } + + return select (maxfd + 1, fdset, NULL, NULL, timeout); +} + VISIBLE int Sys_CheckInput (int idle, int net_socket) { fd_set fdset; int res; - struct timeval _timeout; - struct timeval *timeout = 0; + int64_t usec; #ifdef _WIN32 int sleep_msec; @@ -739,11 +758,9 @@ Sys_CheckInput (int idle, int net_socket) Sleep (sleep_msec); } - _timeout.tv_sec = 0; - _timeout.tv_usec = net_socket < 0 ? 0 : 20; + usec = net_socket < 0 ? 0 : 20; #else - _timeout.tv_sec = 0; - _timeout.tv_usec = net_socket < 0 ? 0 : 2000; + usec = net_socket < 0 ? 0 : 2000; #endif // select on the net socket and stdin // the only reason we have a timeout at all is so that if the last @@ -757,10 +774,10 @@ Sys_CheckInput (int idle, int net_socket) if (net_socket >= 0) FD_SET (((unsigned) net_socket), &fdset); // cast needed for windows - if (!idle || !sys_dead_sleep->int_val) - timeout = &_timeout; + if (idle && sys_dead_sleep->int_val) + usec = -1; - res = select (max (net_socket, 0) + 1, &fdset, NULL, NULL, timeout); + res = Sys_Select (max (net_socket, 0), &fdset, usec); if (res == 0 || res == -1) return 0; #ifndef _WIN32