spingle/source/net_loop.c

255 lines
5.3 KiB
C
Raw Normal View History

2019-11-24 20:45:15 -08:00
/*
Copyright (C) 1996-2001 Id Software, Inc.
Copyright (C) 2010-2014 QuakeSpasm developers
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 the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "q_stdinc.h"
#include "arch_def.h"
#include "net_sys.h"
#include "quakedef.h"
#include "net_defs.h"
#include "net_loop.h"
2019-11-25 17:40:18 -08:00
static bool localconnectpending = false;
static qsocket_t *loop_client = NULL;
static qsocket_t *loop_server = NULL;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
int32_t Loop_Init(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(cls.state == ca_dedicated)
2019-11-24 20:45:15 -08:00
return -1;
return 0;
}
2019-11-25 17:40:18 -08:00
void Loop_Shutdown(void)
2019-11-24 20:45:15 -08:00
{
}
2019-11-25 17:40:18 -08:00
void Loop_Listen(bool state)
2019-11-24 20:45:15 -08:00
{
2019-11-25 13:20:03 -08:00
(void)state;
2019-11-24 20:45:15 -08:00
}
2019-11-25 17:40:18 -08:00
void Loop_SearchForHosts(bool xmit)
2019-11-24 20:45:15 -08:00
{
2019-11-25 13:20:03 -08:00
(void)xmit;
2019-11-25 17:40:18 -08:00
if(!sv.active)
2019-11-24 20:45:15 -08:00
return;
hostCacheCount = 1;
2019-11-25 17:40:18 -08:00
if(Q_strcmp(hostname.string, "UNNAMED") == 0)
2019-11-24 20:45:15 -08:00
Q_strcpy(hostcache[0].name, "local");
else
Q_strcpy(hostcache[0].name, hostname.string);
Q_strcpy(hostcache[0].map, sv.name);
hostcache[0].users = net_activeconnections;
hostcache[0].maxusers = svs.maxclients;
hostcache[0].driver = net_driverlevel;
Q_strcpy(hostcache[0].cname, "local");
}
2019-11-25 17:40:18 -08:00
qsocket_t *Loop_Connect(const char *host)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(Q_strcmp(host, "local") != 0)
2019-11-24 20:45:15 -08:00
return NULL;
localconnectpending = true;
2019-11-25 17:40:18 -08:00
if(!loop_client)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if((loop_client = NET_NewQSocket()) == NULL)
2019-11-24 20:45:15 -08:00
{
Con_Printf("Loop_Connect: no qsocket available\n");
return NULL;
}
2019-11-25 17:40:18 -08:00
Q_strcpy(loop_client->address, "localhost");
2019-11-24 20:45:15 -08:00
}
loop_client->receiveMessageLength = 0;
loop_client->sendMessageLength = 0;
loop_client->canSend = true;
2019-11-25 17:40:18 -08:00
if(!loop_server)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if((loop_server = NET_NewQSocket()) == NULL)
2019-11-24 20:45:15 -08:00
{
Con_Printf("Loop_Connect: no qsocket available\n");
return NULL;
}
2019-11-25 17:40:18 -08:00
Q_strcpy(loop_server->address, "LOCAL");
2019-11-24 20:45:15 -08:00
}
loop_server->receiveMessageLength = 0;
loop_server->sendMessageLength = 0;
loop_server->canSend = true;
loop_client->driverdata = (void *)loop_server;
loop_server->driverdata = (void *)loop_client;
return loop_client;
}
2019-11-25 17:40:18 -08:00
qsocket_t *Loop_CheckNewConnections(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(!localconnectpending)
2019-11-24 20:45:15 -08:00
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;
}
2019-11-25 16:49:58 -08:00
static int32_t IntAlign(int32_t value)
2019-11-24 20:45:15 -08:00
{
2019-11-25 16:49:58 -08:00
return (value + (sizeof(int32_t) - 1)) & (~(sizeof(int32_t) - 1));
2019-11-24 20:45:15 -08:00
}
2019-11-25 17:40:18 -08:00
int32_t Loop_GetMessage(qsocket_t *sock)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
int32_t ret;
int32_t length;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(sock->receiveMessageLength == 0)
2019-11-24 20:45:15 -08:00
return 0;
ret = sock->receiveMessage[0];
length = sock->receiveMessage[1] + (sock->receiveMessage[2] << 8);
// alignment byte skipped here
2019-11-25 17:40:18 -08:00
SZ_Clear(&net_message);
SZ_Write(&net_message, &sock->receiveMessage[4], length);
2019-11-24 20:45:15 -08:00
length = IntAlign(length + 4);
sock->receiveMessageLength -= length;
2019-11-25 17:40:18 -08:00
if(sock->receiveMessageLength)
memmove(sock->receiveMessage, &sock->receiveMessage[length], sock->receiveMessageLength);
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(sock->driverdata && ret == 1)
2019-11-24 20:45:15 -08:00
((qsocket_t *)sock->driverdata)->canSend = true;
return ret;
}
2019-11-25 17:40:18 -08:00
int32_t Loop_SendMessage(qsocket_t *sock, sizebuf_t *data)
2019-11-24 20:45:15 -08:00
{
byte *buffer;
2019-11-25 16:49:58 -08:00
int32_t *bufferLength;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(!sock->driverdata)
2019-11-24 20:45:15 -08:00
return -1;
bufferLength = &((qsocket_t *)sock->driverdata)->receiveMessageLength;
2019-11-25 17:40:18 -08:00
if((*bufferLength + data->cursize + 4) > NET_MAXMESSAGE)
2019-11-24 20:45:15 -08:00
Sys_Error("Loop_SendMessage: overflow");
buffer = ((qsocket_t *)sock->driverdata)->receiveMessage + *bufferLength;
// message type
*buffer++ = 1;
// length
*buffer++ = data->cursize & 0xff;
*buffer++ = data->cursize >> 8;
// align
buffer++;
// message
Q_memcpy(buffer, data->data, data->cursize);
*bufferLength = IntAlign(*bufferLength + data->cursize + 4);
sock->canSend = false;
return 1;
}
2019-11-25 17:40:18 -08:00
int32_t Loop_SendUnreliableMessage(qsocket_t *sock, sizebuf_t *data)
2019-11-24 20:45:15 -08:00
{
byte *buffer;
2019-11-25 16:49:58 -08:00
int32_t *bufferLength;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(!sock->driverdata)
2019-11-24 20:45:15 -08:00
return -1;
bufferLength = &((qsocket_t *)sock->driverdata)->receiveMessageLength;
2019-11-25 17:40:18 -08:00
if((*bufferLength + data->cursize + sizeof(byte) + sizeof(int16_t)) > NET_MAXMESSAGE)
2019-11-24 20:45:15 -08:00
return 0;
buffer = ((qsocket_t *)sock->driverdata)->receiveMessage + *bufferLength;
// message type
*buffer++ = 2;
// length
*buffer++ = data->cursize & 0xff;
*buffer++ = data->cursize >> 8;
// align
buffer++;
// message
Q_memcpy(buffer, data->data, data->cursize);
*bufferLength = IntAlign(*bufferLength + data->cursize + 4);
return 1;
}
2019-11-25 17:40:18 -08:00
bool Loop_CanSendMessage(qsocket_t *sock)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(!sock->driverdata)
2019-11-24 20:45:15 -08:00
return false;
return sock->canSend;
}
2019-11-25 17:40:18 -08:00
bool Loop_CanSendUnreliableMessage(qsocket_t *sock)
2019-11-24 20:45:15 -08:00
{
2019-11-25 13:20:03 -08:00
(void)sock;
2019-11-24 20:45:15 -08:00
return true;
}
2019-11-25 17:40:18 -08:00
void Loop_Close(qsocket_t *sock)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(sock->driverdata)
2019-11-24 20:45:15 -08:00
((qsocket_t *)sock->driverdata)->driverdata = NULL;
sock->receiveMessageLength = 0;
sock->sendMessageLength = 0;
sock->canSend = true;
2019-11-25 17:40:18 -08:00
if(sock == loop_client)
2019-11-24 20:45:15 -08:00
loop_client = NULL;
else
loop_server = NULL;
}