Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NET_Sleep_Timeout (-pingboost 3) #391

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 53 additions & 10 deletions rehlds/engine/net_ws.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -1054,15 +1057,31 @@ qboolean NET_QueuePacket(netsrc_t sock)

return NET_GetLong(in_message.data, ret, &in_message.cursize);
}

DLL_EXPORT int NET_Sleep_Timeout(void)
{
static int32 lasttime;
static int numFrames;
static int staggerFrames;
#ifdef REHLDS_FIXES
static int32 lasttime = 0;
int32 curtime = (int32)Sys_FloatTime();

if(!lasttime)
lasttime = curtime;

static int acceleratedFrames = 0;
if (curtime - lasttime >= 1)
{
acceleratedFrames = 0;
lasttime = 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)
Expand All @@ -1076,21 +1095,31 @@ DLL_EXPORT int NET_Sleep_Timeout(void)
{
lasttime = curtime;
}

fd_set fdset;
FD_ZERO(&fdset);
#endif

struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = (1000 / fps) * 1000; // TODO: entirely bad code, fix it completely
#ifdef REHLDS_FIXES
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)
tv.tv_usec = 1;
#endif

int res;
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
{
SOCKET number = 0;

fd_set fdset;
FD_ZERO(&fdset);

for (int sock = 0; sock < NS_MAX; sock++)
{
SOCKET net_socket = ip_sockets[sock];
Expand All @@ -1112,14 +1141,25 @@ DLL_EXPORT int NET_Sleep_Timeout(void)
number = net_socket;
}
#endif // _WIN32
}
}
#ifdef REHLDS_FIXES
auto previousUsec = tv.tv_usec; // select(...) changes tv variable to indicate that event happened before timeout
#endif
res = select((int)(number + 1), &fdset, NULL, NULL, &tv);
#ifdef REHLDS_FIXES
if(res > 0 && (previousUsec - tv.tv_usec > 1000) ) // res is greater zero if socket became readable
{
acceleratedFrames++;
}
#endif
}
else
{
res = select(0, NULL, NULL, NULL, &tv);
}
#ifndef REHLDS_FIXES
--numFrames;
#endif
return res;
}

Expand Down Expand Up @@ -2010,6 +2050,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"))
{
Expand Down