2
0
mirror of https://github.com/rehlds/rehlds.git synced 2024-12-27 23:25:45 +03:00
Refactoring minor net_ws.cpp
This commit is contained in:
s1lent 2017-12-09 21:00:18 +07:00
parent 5f1672ca85
commit fd64218ecd
No known key found for this signature in database
GPG Key ID: 0FE401DC73916B5C
5 changed files with 112 additions and 60 deletions

View File

@ -94,7 +94,7 @@ void Sleep_Select(int msec)
void Sleep_Net(int msec)
{
NET_Sleep_Timeout();
NET_Sleep_Timeout();
}
// linux runs on a 100Hz scheduling clock, so the minimum latency from

View File

@ -29,7 +29,11 @@
#include "precompiled.h"
#ifdef _WIN32
HANDLE hThread;
DWORD ThreadId;
CRITICAL_SECTION net_cs;
#endif // _WIN32
qboolean net_thread_initialized;
@ -46,8 +50,7 @@ netadr_t net_from;
sizebuf_t net_message;
qboolean noip;
qboolean noipx;
int use_thread;
qboolean use_thread;
unsigned char net_message_buffer[NET_MAX_PAYLOAD];
unsigned char in_message_buf[NET_MAX_PAYLOAD];
@ -141,7 +144,7 @@ cvar_t net_graphpos;
#endif // HOOK_ENGINE
void NET_ThreadLock(void)
void NET_ThreadLock()
{
#ifdef _WIN32
if (use_thread && net_thread_initialized)
@ -151,7 +154,7 @@ void NET_ThreadLock(void)
#endif // _WIN32
}
void NET_ThreadUnlock(void)
void NET_ThreadUnlock()
{
#ifdef _WIN32
if (use_thread && net_thread_initialized)
@ -212,7 +215,7 @@ void SockadrToNetadr(const struct sockaddr *s, netadr_t *a)
else if (s->sa_family == AF_IPX)
{
a->type = NA_IPX;
Q_memcpy(a->ipx, s->sa_data, 10);
Q_memcpy(a->ipx, s->sa_data, sizeof(a->ipx));
a->port = *(unsigned short *)&s->sa_data[10];
}
#endif // _WIN32
@ -727,7 +730,7 @@ void NET_AddToLagged(netsrc_t sock, packetlag_t *pList, packetlag_t *pPacket, ne
Q_memcpy(&pPacket->net_from_, net_from_, sizeof(netadr_t));
}
void NET_AdjustLag(void)
void NET_AdjustLag()
{
static double lasttime = realtime;
double dt;
@ -814,9 +817,11 @@ qboolean NET_LagPacket(qboolean newdata, netsrc_t sock, netadr_t *from, sizebuf_
Cvar_SetValue("fakeloss", 0.0);
}
}
pNewPacketLag = (packetlag_t *)Mem_ZeroMalloc(0x28u);
pNewPacketLag = (packetlag_t *)Mem_ZeroMalloc(sizeof(packetlag_t));
NET_AddToLagged(sock, &g_pLagData[sock], pNewPacketLag, from, *data, curtime);
}
pPacket = g_pLagData[sock].pNext;
while (pPacket != &g_pLagData[sock])
@ -826,6 +831,7 @@ qboolean NET_LagPacket(qboolean newdata, netsrc_t sock, netadr_t *from, sizebuf_
pPacket = pPacket->pNext;
}
if (pPacket == &g_pLagData[sock])
return FALSE;
@ -1055,7 +1061,7 @@ qboolean NET_QueuePacket(netsrc_t sock)
return NET_GetLong(in_message.data, ret, &in_message.cursize);
}
DLL_EXPORT int NET_Sleep_Timeout(void)
DLL_EXPORT int NET_Sleep_Timeout()
{
static int32 lasttime;
static int numFrames;
@ -1076,7 +1082,7 @@ DLL_EXPORT int NET_Sleep_Timeout(void)
{
lasttime = curtime;
}
fd_set fdset;
FD_ZERO(&fdset);
@ -1123,7 +1129,7 @@ DLL_EXPORT int NET_Sleep_Timeout(void)
return res;
}
int NET_Sleep(void)
int NET_Sleep()
{
fd_set fdset;
struct timeval tv;
@ -1161,45 +1167,92 @@ int NET_Sleep(void)
return select((int)(number + 1), &fdset, NULL, NULL, net_sleepforever == 0 ? &tv : NULL);
}
void NET_StartThread(void)
#ifdef _WIN32
DWORD WINAPI NET_ThreadMain(LPVOID lpThreadParameter)
{
while (true)
{
while (NET_Sleep())
{
qboolean bret = FALSE;
for (int sock = 0; sock < NS_MAX; sock++)
{
NET_ThreadLock();
bret = NET_QueuePacket((netsrc_t)sock);
if (bret)
{
net_messages_t *pmsg = NET_AllocMsg(in_message.cursize);
pmsg->next = nullptr;
Q_memcpy(pmsg->buffer, in_message.data, in_message.cursize);
Q_memcpy(&pmsg->from, &in_from, sizeof(pmsg->from));
// add to tail of the list
net_messages_t *p = messages[sock];
if (p)
{
while (p->next)
p = p->next;
p->next = pmsg;
}
// add to head
else
{
messages[sock] = pmsg;
}
}
NET_ThreadUnlock();
}
if (!bret)
break;
}
Sys_Sleep(1);
}
return 0;
}
#endif // _WIN32
void NET_StartThread()
{
if (use_thread)
{
if (!net_thread_initialized)
{
net_thread_initialized = TRUE;
Sys_Error("%s: -netthread is not reversed yet", __func__);
#ifdef _WIN32
/*
InitializeCriticalSection(&net_cs);
hThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)NET_ThreadMain, 0, 0, &ThreadId);
hThread = CreateThread(0, 0, NET_ThreadMain, 0, 0, &ThreadId);
if (!hThread)
{
DeleteCriticalSection(&net_cs);
net_thread_initialized = 0;
use_thread = 0;
net_thread_initialized = FALSE;
use_thread = FALSE;
Sys_Error("%s: Couldn't initialize network thread, run without -netthread\n", __func__);
}
*/
#endif // _WIN32
}
}
}
void NET_StopThread(void)
void NET_StopThread()
{
if (use_thread)
{
if (net_thread_initialized)
{
#ifdef _WIN32
/*
TerminateThread(hThread, 0);
DeleteCriticalSection(&net_cs);
*/
#endif // _WIN32
net_thread_initialized = FALSE;
Sys_Error("%s: -netthread is not reversed yet", __func__);
}
}
}
@ -1214,9 +1267,9 @@ net_messages_t *NET_AllocMsg(int size)
net_messages_t *pmsg;
if (size <= MSG_QUEUE_SIZE && normalqueue)
{
pmsg = normalqueue->next;
normalqueue->buffersize = size;
normalqueue = pmsg;
pmsg = normalqueue;
pmsg->buffersize = size;
normalqueue = pmsg->next;
}
else
{
@ -1287,20 +1340,20 @@ qboolean NET_GetPacket(netsrc_t sock)
net_from = pmsg->from;
msg_readcount = 0;
NET_FreeMsg(pmsg);
bret = 1;
bret = TRUE;
}
NET_ThreadUnlock();
return bret;
}
void NET_AllocateQueues(void)
void NET_AllocateQueues()
{
net_messages_t *p;
for (int i = 0; i < NUM_MSG_QUEUES; i++)
{
p = (net_messages_t *)Mem_ZeroMalloc(sizeof(net_messages_t));
p->buffer = (unsigned char *)Mem_ZeroMalloc(MSG_QUEUE_SIZE);
p->preallocated = 1;
p->preallocated = TRUE;
p->next = normalqueue;
normalqueue = p;
}
@ -1308,8 +1361,10 @@ void NET_AllocateQueues(void)
NET_StartThread();
}
void NET_FlushQueues(void)
void NET_FlushQueues()
{
NET_StopThread();
for (int i = 0; i < NS_MAX; i++)
{
net_messages_t *p = messages[i];
@ -1612,7 +1667,7 @@ SOCKET NET_IPSocket(char *net_interface, int port, qboolean multicast)
return newsocket;
}
void NET_OpenIP(void)
void NET_OpenIP()
{
//cvar_t *ip;//unused?
int port;
@ -1744,7 +1799,7 @@ SOCKET NET_IPXSocket(int hostshort)
return newsocket;
}
void NET_OpenIPX(void)
void NET_OpenIPX()
{
int port;
int dedicated;
@ -1795,7 +1850,7 @@ void NET_OpenIPX(void)
#endif // _WIN32
void NET_GetLocalAddress(void)
void NET_GetLocalAddress()
{
char buff[512];
struct sockaddr_in address;
@ -1888,7 +1943,7 @@ void NET_GetLocalAddress(void)
#endif //_WIN32
}
int NET_IsConfigured(void)
int NET_IsConfigured()
{
return net_configured;
}
@ -1950,7 +2005,7 @@ void NET_Config(qboolean multiplayer)
net_configured = multiplayer ? 1 : 0;
}
void MaxPlayers_f(void)
void MaxPlayers_f()
{
if (Cmd_Argc() != 2)
{
@ -1983,7 +2038,7 @@ void MaxPlayers_f(void)
Cvar_Set("deathmatch", "1");
}
void NET_Init(void)
void NET_Init()
{
#ifdef HOOK_ENGINE
Cmd_AddCommand("maxplayers", (xcommand_t)GetOriginalFuncAddrOrDefault("MaxPlayers_f", (void *)MaxPlayers_f));
@ -2012,10 +2067,8 @@ void NET_Init(void)
Cvar_RegisterVariable(&net_graphpos);
if (COM_CheckParm("-netthread"))
{
use_thread = 1;
Sys_Error("%s: -netthread is not reversed yet", __func__);
}
use_thread = TRUE;
if (COM_CheckParm("-netsleep"))
net_sleepforever = 0;
@ -2073,7 +2126,7 @@ void NET_ClearLagData(qboolean bClient, qboolean bServer)
NET_ThreadUnlock();
}
void NET_Shutdown(void)
void NET_Shutdown()
{
NET_ThreadLock();

View File

@ -187,7 +187,7 @@ extern qboolean noipx;
#endif // _WIN32
extern sizebuf_t net_message;
extern cvar_t clockwindow;
extern int use_thread;
extern qboolean use_thread;
extern cvar_t iphostport;
extern cvar_t hostport;
#ifdef _WIN32
@ -214,8 +214,8 @@ extern net_messages_t *messages[3];
extern net_messages_t *normalqueue;
void NET_ThreadLock(void);
void NET_ThreadUnlock(void);
void NET_ThreadLock();
void NET_ThreadUnlock();
unsigned short Q_ntohs(unsigned short netshort);
void NetadrToSockadr(const netadr_t *a, struct sockaddr *s);
void SockadrToNetadr(const struct sockaddr *s, netadr_t *a);
@ -237,33 +237,33 @@ void NET_RemoveFromPacketList(packetlag_t *pPacket);
NOXREF int NET_CountLaggedList(packetlag_t *pList);
void NET_ClearLaggedList(packetlag_t *pList);
void NET_AddToLagged(netsrc_t sock, packetlag_t *pList, packetlag_t *pPacket, netadr_t *net_from_, sizebuf_t messagedata, float timestamp);
void NET_AdjustLag(void);
void NET_AdjustLag();
qboolean NET_LagPacket(qboolean newdata, netsrc_t sock, netadr_t *from, sizebuf_t *data);
void NET_FlushSocket(netsrc_t sock);
qboolean NET_GetLong(unsigned char *pData, int size, int *outSize);
qboolean NET_QueuePacket(netsrc_t sock);
int NET_Sleep(void);
void NET_StartThread(void);
void NET_StopThread(void);
int NET_Sleep();
void NET_StartThread();
void NET_StopThread();
void *net_malloc(size_t size);
net_messages_t *NET_AllocMsg(int size);
void NET_FreeMsg(net_messages_t *pmsg);
qboolean NET_GetPacket(netsrc_t sock);
void NET_AllocateQueues(void);
void NET_FlushQueues(void);
void NET_AllocateQueues();
void NET_FlushQueues();
int NET_SendLong(netsrc_t sock, SOCKET s, const char *buf, int len, int flags, const struct sockaddr *to, int tolen);
void NET_SendPacket_api(unsigned int length, void *data, const netadr_t &to);
void NET_SendPacket(netsrc_t sock, int length, void *data, const netadr_t& to);
SOCKET NET_IPSocket(char *net_interface, int port, qboolean multicast);
void NET_OpenIP(void);
void NET_OpenIP();
SOCKET NET_IPXSocket(int hostshort);
void NET_OpenIPX(void);
void NET_GetLocalAddress(void);
int NET_IsConfigured(void);
void NET_OpenIPX();
void NET_GetLocalAddress();
int NET_IsConfigured();
void NET_Config(qboolean multiplayer);
void MaxPlayers_f(void);
void NET_Init(void);
void MaxPlayers_f();
void NET_Init();
void NET_ClearLagData(qboolean bClient, qboolean bServer);
void NET_Shutdown(void);
void NET_Shutdown();
qboolean NET_JoinGroup(netsrc_t sock, netadr_t& addr);
qboolean NET_LeaveGroup(netsrc_t sock, netadr_t& addr);

View File

@ -428,13 +428,12 @@ NOBODY void MaskExceptions(void);
NOBODY void Sys_Init(void);
NOXREF void Sys_Sleep(int msec)
void Sys_Sleep(int msec)
{
NOXREFCHECK;
#ifdef _WIN32
Sleep(msec);
#else
usleep(1000 *msec);
usleep(1000 * msec);
#endif // _WIN32
}

View File

@ -150,7 +150,7 @@ NOBODY void Sys_PushFPCW_SetHigh(void);
NOBODY void Sys_PopFPCW(void);
NOBODY void MaskExceptions(void);
NOBODY void Sys_Init(void);
NOXREF void Sys_Sleep(int msec);
void Sys_Sleep(int msec);
NOBODY void Sys_DebugOutStraight(const char *pStr);
void NORETURN Sys_Error(const char *error, ...);
NOXREF void Sys_Warning(const char *pszWarning, ...);