2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 qboolean localconnectpending = false;
26 qsocket_t *loop_client = NULL;
27 qsocket_t *loop_server = NULL;
31 if (cls.state == ca_dedicated)
37 void Loop_Shutdown (void)
42 void Loop_Heartbeat (const char *master)
47 void Loop_Listen (qboolean state)
52 void Loop_SearchForHosts (qboolean xmit)
58 if (strcmp(hostname.string, "UNNAMED") == 0)
59 strcpy(hostcache[0].name, "local");
61 strcpy(hostcache[0].name, hostname.string);
62 strcpy(hostcache[0].map, sv.name);
63 hostcache[0].users = net_activeconnections;
64 hostcache[0].maxusers = svs.maxclients;
65 hostcache[0].driver = net_driverlevel;
66 strcpy(hostcache[0].cname, "local");
70 qboolean Loop_SearchForInetHosts (const char *master)
76 qsocket_t *Loop_Connect (const char *host)
78 if (strcmp(host,"local") != 0)
81 localconnectpending = true;
85 if ((loop_client = NET_NewQSocket ()) == NULL)
87 Con_Printf("Loop_Connect: no qsocket available\n");
90 strcpy (loop_client->address, "localhost");
92 loop_client->receiveMessageLength = 0;
93 loop_client->sendMessageLength = 0;
94 loop_client->canSend = true;
98 if ((loop_server = NET_NewQSocket ()) == NULL)
100 Con_Printf("Loop_Connect: no qsocket available\n");
103 strcpy (loop_server->address, "LOCAL");
105 loop_server->receiveMessageLength = 0;
106 loop_server->sendMessageLength = 0;
107 loop_server->canSend = true;
109 loop_client->driverdata = (void *)loop_server;
110 loop_server->driverdata = (void *)loop_client;
116 qsocket_t *Loop_CheckNewConnections (void)
118 if (!localconnectpending)
121 localconnectpending = false;
122 loop_server->sendMessageLength = 0;
123 loop_server->receiveMessageLength = 0;
124 loop_server->canSend = true;
125 loop_client->sendMessageLength = 0;
126 loop_client->receiveMessageLength = 0;
127 loop_client->canSend = true;
132 static int IntAlign(int value)
134 return (value + (sizeof(int) - 1)) & (~(sizeof(int) - 1));
138 int Loop_GetMessage (qsocket_t *sock)
143 if (sock->receiveMessageLength == 0)
146 ret = sock->receiveMessage[0];
147 length = sock->receiveMessage[1] + (sock->receiveMessage[2] << 8);
148 // alignment byte skipped here
149 SZ_Clear (&net_message);
150 SZ_Write (&net_message, &sock->receiveMessage[4], length);
152 length = IntAlign(length + 4);
153 sock->receiveMessageLength -= length;
155 if (sock->receiveMessageLength)
156 memcpy(sock->receiveMessage, &sock->receiveMessage[length], sock->receiveMessageLength);
158 if (sock->driverdata && ret == 1)
159 ((qsocket_t *)sock->driverdata)->canSend = true;
165 int Loop_SendMessage (qsocket_t *sock, sizebuf_t *data)
170 if (!sock->driverdata)
173 bufferLength = &((qsocket_t *)sock->driverdata)->receiveMessageLength;
175 if ((*bufferLength + data->cursize + 4) > NET_MAXMESSAGE)
176 Host_Error("Loop_SendMessage: overflow\n");
178 buffer = ((qsocket_t *)sock->driverdata)->receiveMessage + *bufferLength;
184 *buffer++ = data->cursize & 0xff;
185 *buffer++ = data->cursize >> 8;
191 memcpy(buffer, data->data, data->cursize);
192 *bufferLength = IntAlign(*bufferLength + data->cursize + 4);
194 sock->canSend = false;
199 int Loop_SendUnreliableMessage (qsocket_t *sock, sizebuf_t *data)
204 if (!sock->driverdata)
207 bufferLength = &((qsocket_t *)sock->driverdata)->receiveMessageLength;
209 // LordHavoc: added an extra sizeof(qbyte) to account for alignment
210 if ((*bufferLength + data->cursize + sizeof(qbyte) + sizeof(short) + sizeof(qbyte)) > NET_MAXMESSAGE)
213 buffer = ((qsocket_t *)sock->driverdata)->receiveMessage + *bufferLength;
219 *buffer++ = data->cursize & 0xff;
220 *buffer++ = data->cursize >> 8;
226 memcpy(buffer, data->data, data->cursize);
227 *bufferLength = IntAlign(*bufferLength + data->cursize + 4);
232 qboolean Loop_CanSendMessage (qsocket_t *sock)
234 if (!sock->driverdata)
236 return sock->canSend;
240 qboolean Loop_CanSendUnreliableMessage (qsocket_t *sock)
246 void Loop_Close (qsocket_t *sock)
248 if (sock->driverdata)
249 ((qsocket_t *)sock->driverdata)->driverdata = NULL;
250 sock->receiveMessageLength = 0;
251 sock->sendMessageLength = 0;
252 sock->canSend = true;
253 if (sock == loop_client)