From a8821a42e4d71b12fc7962a4c9239ed6373ebdcc Mon Sep 17 00:00:00 2001 From: In-line Date: Mon, 6 Mar 2017 19:24:56 +0400 Subject: [PATCH 1/7] Refactor NET_Sleep_Timeout --- rehlds/engine/net_ws.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/rehlds/engine/net_ws.cpp b/rehlds/engine/net_ws.cpp index 58d50ad22..3fcfa3e20 100644 --- a/rehlds/engine/net_ws.cpp +++ b/rehlds/engine/net_ws.cpp @@ -1053,9 +1053,9 @@ qboolean NET_QueuePacket(netsrc_t sock) DLL_EXPORT int NET_Sleep_Timeout(void) { - static int32 lasttime; - static int numFrames; - static int staggerFrames; + static int32 lasttime = 0; + static int numFrames = 0; + static int staggerFrames = 0; int fps = (int)sys_ticrate.value; int32 curtime = (int)Sys_FloatTime(); @@ -1072,9 +1072,6 @@ DLL_EXPORT int NET_Sleep_Timeout(void) { lasttime = curtime; } - - fd_set fdset; - FD_ZERO(&fdset); struct timeval tv; tv.tv_sec = 0; @@ -1082,11 +1079,14 @@ DLL_EXPORT int NET_Sleep_Timeout(void) if (tv.tv_usec <= 0) tv.tv_usec = 1; - int res; + int res = 0; if (numFrames > 0 && numFrames % staggerFrames) { int number = 0; + fd_set fdset; + FD_ZERO(&fdset); + for (int sock = 0; sock < 3; sock++) { SOCKET net_socket = ip_sockets[sock]; @@ -1109,11 +1109,11 @@ DLL_EXPORT int NET_Sleep_Timeout(void) } #endif // _WIN32 } - res = select(number + 1, &fdset, 0, 0, &tv); + res = select(number + 1, &fdset, NULL, NULL, &tv); } else { - res = select(0, 0, 0, 0, &tv); + res = select(0, NULL, NULL, NULL, &tv); } --numFrames; return res; From 3fa4734fa3800da6d75da3433d4c0eec0b19a7dc Mon Sep 17 00:00:00 2001 From: In-line Date: Mon, 6 Mar 2017 22:28:13 +0400 Subject: [PATCH 2/7] Add limit to NET_Sleep_Timeout --- rehlds/engine/net_ws.cpp | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/rehlds/engine/net_ws.cpp b/rehlds/engine/net_ws.cpp index 3fcfa3e20..8ea4e5ca5 100644 --- a/rehlds/engine/net_ws.cpp +++ b/rehlds/engine/net_ws.cpp @@ -111,6 +111,9 @@ cvar_t net_graph = { "net_graph", "0", FCVAR_ARCHIVE, 0.0f, NULL }; cvar_t net_graphwidth = { "net_graphwidth", "150", 0, 0.0f, NULL }; cvar_t net_scale = { "net_scale", "5", FCVAR_ARCHIVE, 0.0f, NULL }; cvar_t net_graphpos = { "net_graphpos", "1", FCVAR_ARCHIVE, 0.0f, NULL }; +#ifdef REHLDS_FIXES +cvar_t sv_rehlds_max_accelerated_frames = { "sv_rehlds_max_accelerated_frames", "-1", 0, -1.0f, NULL }; +#endif #else // HOOK_ENGINE @@ -1050,15 +1053,31 @@ qboolean NET_QueuePacket(netsrc_t sock) return NET_GetLong(in_message.data, ret, &in_message.cursize); } - DLL_EXPORT int NET_Sleep_Timeout(void) { +#ifdef REHLDS_FIXES + static int64 lasttime = 0; + int64 curtime = (int)Sys_FloatTime(); + + if(!lasttime) + lasttime = curtime; + + static int acceleratedFrames = 0; + if (curtime - lasttime > 1) + { + acceleratedFrames = 0; + lasstime = curtime; + } + + int fps = max(1, (int)sys_ticrate.value); // Prevent division to 0 +#else static int32 lasttime = 0; static int numFrames = 0; static int staggerFrames = 0; int fps = (int)sys_ticrate.value; int32 curtime = (int)Sys_FloatTime(); + if (lasttime) { if (curtime - lasttime > 1) @@ -1072,15 +1091,25 @@ DLL_EXPORT int NET_Sleep_Timeout(void) { lasttime = curtime; } +#endif struct timeval tv; tv.tv_sec = 0; +#ifdef REHLDS_FIXES + tv.tv_usec = Q_clamp((1000 / fps) * 1000, 1, 1000000 - 1); +#else tv.tv_usec = (1000 / fps) * 1000; // TODO: entirely bad code, fix it completely if (tv.tv_usec <= 0) tv.tv_usec = 1; +#endif int res = 0; +#ifdef REHLDS_FIXES + float maxAcceleratedFrames = sv_rehlds_max_accelerated_frames.value; + if (maxAcceleratedFrames < 0 ||acceleratedFrames <= maxAcceleratedFrames) +#else if (numFrames > 0 && numFrames % staggerFrames) +#endif { int number = 0; @@ -1110,12 +1139,18 @@ DLL_EXPORT int NET_Sleep_Timeout(void) #endif // _WIN32 } res = select(number + 1, &fdset, NULL, NULL, &tv); +#ifdef REHLDS_FIXES + if(res > 0) // -1 on error, greater zero if socket became readable + acceleratedFrames++; +#endif } else { res = select(0, NULL, NULL, NULL, &tv); } +#ifndef REHLDS_FIXES --numFrames; +#endif return res; } @@ -2006,6 +2041,9 @@ void NET_Init(void) Cvar_RegisterVariable(&net_graphwidth); Cvar_RegisterVariable(&net_scale); Cvar_RegisterVariable(&net_graphpos); +#ifdef REHLDS_FIXES + Cvar_RegisterVariable(&sv_rehlds_max_accelerated_frames); +#endif if (COM_CheckParm("-netthread")) { From d73181e75cd139e443fd4bd820bb7dc4ca20dced Mon Sep 17 00:00:00 2001 From: In-line Date: Mon, 6 Mar 2017 23:17:32 +0400 Subject: [PATCH 3/7] Fix typo --- rehlds/engine/net_ws.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rehlds/engine/net_ws.cpp b/rehlds/engine/net_ws.cpp index 8ea4e5ca5..a313e3c95 100644 --- a/rehlds/engine/net_ws.cpp +++ b/rehlds/engine/net_ws.cpp @@ -1066,7 +1066,7 @@ DLL_EXPORT int NET_Sleep_Timeout(void) if (curtime - lasttime > 1) { acceleratedFrames = 0; - lasstime = curtime; + lasttime = curtime; } int fps = max(1, (int)sys_ticrate.value); // Prevent division to 0 From d20382895b6165f2f31789ff5c1ce023d32361cf Mon Sep 17 00:00:00 2001 From: In-line Date: Tue, 7 Mar 2017 13:37:15 +0400 Subject: [PATCH 4/7] Use floating point math --- rehlds/engine/net_ws.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rehlds/engine/net_ws.cpp b/rehlds/engine/net_ws.cpp index a313e3c95..ca58dc891 100644 --- a/rehlds/engine/net_ws.cpp +++ b/rehlds/engine/net_ws.cpp @@ -1065,6 +1065,7 @@ DLL_EXPORT int NET_Sleep_Timeout(void) static int acceleratedFrames = 0; if (curtime - lasttime > 1) { + Con_Prinf("I'm called once a sec!\n"); acceleratedFrames = 0; lasttime = curtime; } @@ -1096,7 +1097,7 @@ DLL_EXPORT int NET_Sleep_Timeout(void) struct timeval tv; tv.tv_sec = 0; #ifdef REHLDS_FIXES - tv.tv_usec = Q_clamp((1000 / fps) * 1000, 1, 1000000 - 1); + tv.tv_usec = Q_clamp( (1000.0f / fps ) * 1000.0f, 1, 1000000 - 1); #else tv.tv_usec = (1000 / fps) * 1000; // TODO: entirely bad code, fix it completely if (tv.tv_usec <= 0) From bae3bef587c403f26ea7ee22db72a16aa0bc4fa1 Mon Sep 17 00:00:00 2001 From: In-line Date: Tue, 7 Mar 2017 14:24:22 +0400 Subject: [PATCH 5/7] Add time check, remove garbage --- rehlds/engine/net_ws.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/rehlds/engine/net_ws.cpp b/rehlds/engine/net_ws.cpp index ca58dc891..bb5810493 100644 --- a/rehlds/engine/net_ws.cpp +++ b/rehlds/engine/net_ws.cpp @@ -1056,8 +1056,8 @@ qboolean NET_QueuePacket(netsrc_t sock) DLL_EXPORT int NET_Sleep_Timeout(void) { #ifdef REHLDS_FIXES - static int64 lasttime = 0; - int64 curtime = (int)Sys_FloatTime(); + static int32 lasttime = 0; + int32 curtime = (int32)Sys_FloatTime(); if(!lasttime) lasttime = curtime; @@ -1065,7 +1065,6 @@ DLL_EXPORT int NET_Sleep_Timeout(void) static int acceleratedFrames = 0; if (curtime - lasttime > 1) { - Con_Prinf("I'm called once a sec!\n"); acceleratedFrames = 0; lasttime = curtime; } @@ -1081,7 +1080,7 @@ DLL_EXPORT int NET_Sleep_Timeout(void) if (lasttime) { - if (curtime - lasttime > 1) + if (curtime - lasttime >= 1) { lasttime = curtime; numFrames = fps; @@ -1107,7 +1106,7 @@ DLL_EXPORT int NET_Sleep_Timeout(void) int res = 0; #ifdef REHLDS_FIXES float maxAcceleratedFrames = sv_rehlds_max_accelerated_frames.value; - if (maxAcceleratedFrames < 0 ||acceleratedFrames <= maxAcceleratedFrames) + if (maxAcceleratedFrames < 0 || acceleratedFrames <= maxAcceleratedFrames) #else if (numFrames > 0 && numFrames % staggerFrames) #endif @@ -1139,10 +1138,15 @@ DLL_EXPORT int NET_Sleep_Timeout(void) } #endif // _WIN32 } +#ifdef REHLDS_FIXES + auto previousUsec = tv.tv_usec; // select(...) changes tv variable to indicate that event happened before timeout +#endif res = select(number + 1, &fdset, NULL, NULL, &tv); #ifdef REHLDS_FIXES - if(res > 0) // -1 on error, greater zero if socket became readable - acceleratedFrames++; + if(res > 0 && (previousUsec - tv.tv_usec > 1000) ) // res is greater zero if socket became readable + { + acceleratedFrames++; + } #endif } else From 8852d9c6d468022d46efb423d7287f304b499662 Mon Sep 17 00:00:00 2001 From: In-line Date: Tue, 7 Mar 2017 14:27:51 +0400 Subject: [PATCH 6/7] Cosmetic changes --- rehlds/engine/net_ws.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rehlds/engine/net_ws.cpp b/rehlds/engine/net_ws.cpp index bb5810493..087617157 100644 --- a/rehlds/engine/net_ws.cpp +++ b/rehlds/engine/net_ws.cpp @@ -1063,7 +1063,7 @@ DLL_EXPORT int NET_Sleep_Timeout(void) lasttime = curtime; static int acceleratedFrames = 0; - if (curtime - lasttime > 1) + if (curtime - lasttime >= 1) { acceleratedFrames = 0; lasttime = curtime; @@ -1080,7 +1080,7 @@ DLL_EXPORT int NET_Sleep_Timeout(void) if (lasttime) { - if (curtime - lasttime >= 1) + if (curtime - lasttime > 1) { lasttime = curtime; numFrames = fps; @@ -1098,7 +1098,7 @@ DLL_EXPORT int NET_Sleep_Timeout(void) #ifdef REHLDS_FIXES tv.tv_usec = Q_clamp( (1000.0f / fps ) * 1000.0f, 1, 1000000 - 1); #else - tv.tv_usec = (1000 / fps) * 1000; // TODO: entirely bad code, fix it completely + tv.tv_usec = (1000 / fps) * 1000; // DONE: entirely bad code, fix it completely if (tv.tv_usec <= 0) tv.tv_usec = 1; #endif From ee835d4010791bf7175c6234fa6611d9389af009 Mon Sep 17 00:00:00 2001 From: In-line Date: Tue, 7 Mar 2017 18:54:10 +0400 Subject: [PATCH 7/7] Correct condition without float math --- rehlds/engine/net_ws.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rehlds/engine/net_ws.cpp b/rehlds/engine/net_ws.cpp index 087617157..089e2c4db 100644 --- a/rehlds/engine/net_ws.cpp +++ b/rehlds/engine/net_ws.cpp @@ -1096,7 +1096,7 @@ DLL_EXPORT int NET_Sleep_Timeout(void) struct timeval tv; tv.tv_sec = 0; #ifdef REHLDS_FIXES - tv.tv_usec = Q_clamp( (1000.0f / fps ) * 1000.0f, 1, 1000000 - 1); + tv.tv_usec = Q_clamp( (1000 * 1000) / fps, 1, 1000000 - 1); #else tv.tv_usec = (1000 / fps) * 1000; // DONE: entirely bad code, fix it completely if (tv.tv_usec <= 0)