2001-02-19 21:15:25 +00:00
|
|
|
/*
|
|
|
|
net_loop.c
|
|
|
|
|
2011-07-26 05:15:41 +00:00
|
|
|
Loopback network driver.
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2001-05-31 03:41:35 +00:00
|
|
|
#include "QF/cvar.h"
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/msg.h"
|
2001-05-31 03:41:35 +00:00
|
|
|
#include "QF/sys.h"
|
|
|
|
|
2003-02-11 22:48:57 +00:00
|
|
|
#include "netmain.h"
|
2001-02-19 21:15:25 +00:00
|
|
|
#include "net_loop.h"
|
2003-02-11 22:48:57 +00:00
|
|
|
|
|
|
|
#include "../nq/include/server.h"
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2023-06-13 09:06:11 +00:00
|
|
|
bool localconnectpending = false;
|
2001-02-26 06:48:02 +00:00
|
|
|
qsocket_t *loop_client = NULL;
|
|
|
|
qsocket_t *loop_server = NULL;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2018-10-09 03:35:01 +00:00
|
|
|
__attribute__((pure)) int
|
2001-02-26 06:48:02 +00:00
|
|
|
Loop_Init (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2022-04-06 10:32:55 +00:00
|
|
|
if (net_is_dedicated)
|
2001-02-19 21:15:25 +00:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
Loop_Shutdown (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
2023-06-13 09:06:11 +00:00
|
|
|
Loop_Listen (bool state)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
2023-06-13 09:06:11 +00:00
|
|
|
Loop_SearchForHosts (bool xmit)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
if (!sv.active)
|
|
|
|
return;
|
|
|
|
|
2021-04-04 09:38:14 +00:00
|
|
|
const char *name = "local";
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
if (strcmp (hostname, "UNNAMED") != 0) {
|
|
|
|
name = hostname;
|
2021-04-04 09:38:14 +00:00
|
|
|
}
|
|
|
|
const char *map = sv.name;
|
|
|
|
int users = net_activeconnections;
|
|
|
|
int maxusers = svs.maxclients;
|
|
|
|
const char *cname = "local";
|
|
|
|
NET_AddCachedHost (name, map, cname, users, maxusers, net_driverlevel,
|
|
|
|
0, 0);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
qsocket_t *
|
2001-07-15 07:04:17 +00:00
|
|
|
Loop_Connect (const char *host)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
if (strcmp (host, "local") != 0)
|
2001-02-19 21:15:25 +00:00
|
|
|
return NULL;
|
2001-02-26 06:48:02 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
localconnectpending = true;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (!loop_client) {
|
|
|
|
if ((loop_client = NET_NewQSocket ()) == NULL) {
|
2007-11-06 10:17:14 +00:00
|
|
|
Sys_Printf ("Loop_Connect: no qsocket available\n");
|
2001-02-19 21:15:25 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
strcpy (loop_client->address, "localhost");
|
|
|
|
}
|
|
|
|
loop_client->receiveMessageLength = 0;
|
|
|
|
loop_client->sendMessageLength = 0;
|
|
|
|
loop_client->canSend = true;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (!loop_server) {
|
|
|
|
if ((loop_server = NET_NewQSocket ()) == NULL) {
|
2007-11-06 10:17:14 +00:00
|
|
|
Sys_Printf ("Loop_Connect: no qsocket available\n");
|
2001-02-19 21:15:25 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
strcpy (loop_server->address, "LOCAL");
|
|
|
|
}
|
|
|
|
loop_server->receiveMessageLength = 0;
|
|
|
|
loop_server->sendMessageLength = 0;
|
|
|
|
loop_server->canSend = true;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
loop_client->driverdata = (void *) loop_server;
|
|
|
|
loop_server->driverdata = (void *) loop_client;
|
|
|
|
|
|
|
|
return loop_client;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
qsocket_t *
|
|
|
|
Loop_CheckNewConnections (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
if (!localconnectpending)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
localconnectpending = false;
|
|
|
|
loop_server->sendMessageLength = 0;
|
|
|
|
loop_server->receiveMessageLength = 0;
|
|
|
|
loop_server->canSend = true;
|
|
|
|
loop_client->sendMessageLength = 0;
|
|
|
|
loop_client->receiveMessageLength = 0;
|
|
|
|
loop_client->canSend = true;
|
|
|
|
return loop_server;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
static int
|
|
|
|
IntAlign (int value)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
return (value + (sizeof (int) - 1)) & (~(sizeof (int) - 1));
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
Loop_GetMessage (qsocket_t * sock)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int ret;
|
|
|
|
int length;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (sock->receiveMessageLength == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = sock->receiveMessage[0];
|
|
|
|
length = sock->receiveMessage[1] + (sock->receiveMessage[2] << 8);
|
|
|
|
// alignment byte skipped here
|
2001-02-23 23:16:13 +00:00
|
|
|
SZ_Clear (net_message->message);
|
|
|
|
SZ_Write (net_message->message, &sock->receiveMessage[4], length);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
length = IntAlign (length + 4);
|
2001-02-19 21:15:25 +00:00
|
|
|
sock->receiveMessageLength -= length;
|
|
|
|
|
|
|
|
if (sock->receiveMessageLength)
|
2009-12-20 05:59:22 +00:00
|
|
|
memmove (sock->receiveMessage, &sock->receiveMessage[length],
|
|
|
|
sock->receiveMessageLength);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (sock->driverdata && ret == 1)
|
2001-02-26 06:48:02 +00:00
|
|
|
((qsocket_t *) sock->driverdata)->canSend = true;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
Loop_SendMessage (qsocket_t * sock, sizebuf_t *data)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
byte *buffer;
|
|
|
|
int *bufferLength;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (!sock->driverdata)
|
|
|
|
return -1;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
bufferLength = &((qsocket_t *) sock->driverdata)->receiveMessageLength;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if ((*bufferLength + data->cursize + 4) > NET_MAXMESSAGE)
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("Loop_SendMessage: overflow");
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
buffer = ((qsocket_t *) sock->driverdata)->receiveMessage + *bufferLength;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
// message type
|
|
|
|
*buffer++ = 1;
|
|
|
|
|
|
|
|
// length
|
|
|
|
*buffer++ = data->cursize & 0xff;
|
|
|
|
*buffer++ = data->cursize >> 8;
|
|
|
|
|
|
|
|
// align
|
|
|
|
buffer++;
|
|
|
|
|
|
|
|
// message
|
2001-02-26 06:48:02 +00:00
|
|
|
memcpy (buffer, data->data, data->cursize);
|
|
|
|
*bufferLength = IntAlign (*bufferLength + data->cursize + 4);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
sock->canSend = false;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
Loop_SendUnreliableMessage (qsocket_t * sock, sizebuf_t *data)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
byte *buffer;
|
|
|
|
int *bufferLength;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (!sock->driverdata)
|
|
|
|
return -1;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
bufferLength = &((qsocket_t *) sock->driverdata)->receiveMessageLength;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if ((*bufferLength + data->cursize + sizeof (byte) + sizeof (short)) >
|
|
|
|
NET_MAXMESSAGE) return 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
buffer = ((qsocket_t *) sock->driverdata)->receiveMessage + *bufferLength;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
// message type
|
|
|
|
*buffer++ = 2;
|
|
|
|
|
|
|
|
// length
|
|
|
|
*buffer++ = data->cursize & 0xff;
|
|
|
|
*buffer++ = data->cursize >> 8;
|
|
|
|
|
|
|
|
// align
|
|
|
|
buffer++;
|
|
|
|
|
|
|
|
// message
|
2001-02-26 06:48:02 +00:00
|
|
|
memcpy (buffer, data->data, data->cursize);
|
|
|
|
*bufferLength = IntAlign (*bufferLength + data->cursize + 4);
|
2001-02-19 21:15:25 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-13 09:06:11 +00:00
|
|
|
__attribute__((pure)) bool
|
2001-02-26 06:48:02 +00:00
|
|
|
Loop_CanSendMessage (qsocket_t * sock)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
if (!sock->driverdata)
|
|
|
|
return false;
|
|
|
|
return sock->canSend;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-13 09:06:11 +00:00
|
|
|
__attribute__((const)) bool
|
2001-02-26 06:48:02 +00:00
|
|
|
Loop_CanSendUnreliableMessage (qsocket_t * sock)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
Loop_Close (qsocket_t * sock)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
if (sock->driverdata)
|
2001-02-26 06:48:02 +00:00
|
|
|
((qsocket_t *) sock->driverdata)->driverdata = NULL;
|
2001-02-19 21:15:25 +00:00
|
|
|
sock->receiveMessageLength = 0;
|
|
|
|
sock->sendMessageLength = 0;
|
|
|
|
sock->canSend = true;
|
|
|
|
if (sock == loop_client)
|
|
|
|
loop_client = NULL;
|
|
|
|
else
|
|
|
|
loop_server = NULL;
|
|
|
|
}
|