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_Listen (qboolean state)
47 void Loop_SearchForHosts (qboolean xmit)
53 if (strcmp(hostname.string, "UNNAMED") == 0)
54 strcpy(hostcache[0].name, "local");
56 strcpy(hostcache[0].name, hostname.string);
57 strcpy(hostcache[0].map, sv.name);
58 hostcache[0].users = net_activeconnections;
59 hostcache[0].maxusers = svs.maxclients;
60 hostcache[0].driver = net_driverlevel;
61 strcpy(hostcache[0].cname, "local");
65 qsocket_t *Loop_Connect (char *host)
67 if (strcmp(host,"local") != 0)
70 localconnectpending = true;
74 if ((loop_client = NET_NewQSocket ()) == NULL)
76 Con_Printf("Loop_Connect: no qsocket available\n");
79 strcpy (loop_client->address, "localhost");
81 loop_client->receiveMessageLength = 0;
82 loop_client->sendMessageLength = 0;
83 loop_client->canSend = true;
87 if ((loop_server = NET_NewQSocket ()) == NULL)
89 Con_Printf("Loop_Connect: no qsocket available\n");
92 strcpy (loop_server->address, "LOCAL");
94 loop_server->receiveMessageLength = 0;
95 loop_server->sendMessageLength = 0;
96 loop_server->canSend = true;
98 loop_client->driverdata = (void *)loop_server;
99 loop_server->driverdata = (void *)loop_client;
105 qsocket_t *Loop_CheckNewConnections (void)
107 if (!localconnectpending)
110 localconnectpending = false;
111 loop_server->sendMessageLength = 0;
112 loop_server->receiveMessageLength = 0;
113 loop_server->canSend = true;
114 loop_client->sendMessageLength = 0;
115 loop_client->receiveMessageLength = 0;
116 loop_client->canSend = true;
121 static int IntAlign(int value)
123 return (value + (sizeof(int) - 1)) & (~(sizeof(int) - 1));
127 int Loop_GetMessage (qsocket_t *sock)
132 if (sock->receiveMessageLength == 0)
135 ret = sock->receiveMessage[0];
136 length = sock->receiveMessage[1] + (sock->receiveMessage[2] << 8);
137 // alignment byte skipped here
138 SZ_Clear (&net_message);
139 SZ_Write (&net_message, &sock->receiveMessage[4], length);
141 length = IntAlign(length + 4);
142 sock->receiveMessageLength -= length;
144 if (sock->receiveMessageLength)
145 memcpy(sock->receiveMessage, &sock->receiveMessage[length], sock->receiveMessageLength);
147 if (sock->driverdata && ret == 1)
148 ((qsocket_t *)sock->driverdata)->canSend = true;
154 int Loop_SendMessage (qsocket_t *sock, sizebuf_t *data)
159 if (!sock->driverdata)
162 bufferLength = &((qsocket_t *)sock->driverdata)->receiveMessageLength;
164 if ((*bufferLength + data->cursize + 4) > NET_MAXMESSAGE)
165 Host_Error("Loop_SendMessage: overflow\n");
167 buffer = ((qsocket_t *)sock->driverdata)->receiveMessage + *bufferLength;
173 *buffer++ = data->cursize & 0xff;
174 *buffer++ = data->cursize >> 8;
180 memcpy(buffer, data->data, data->cursize);
181 *bufferLength = IntAlign(*bufferLength + data->cursize + 4);
183 sock->canSend = false;
188 int Loop_SendUnreliableMessage (qsocket_t *sock, sizebuf_t *data)
193 if (!sock->driverdata)
196 bufferLength = &((qsocket_t *)sock->driverdata)->receiveMessageLength;
198 // LordHavoc: added an extra sizeof(qbyte) to account for alignment
199 if ((*bufferLength + data->cursize + sizeof(qbyte) + sizeof(short) + sizeof(qbyte)) > NET_MAXMESSAGE)
202 buffer = ((qsocket_t *)sock->driverdata)->receiveMessage + *bufferLength;
208 *buffer++ = data->cursize & 0xff;
209 *buffer++ = data->cursize >> 8;
215 memcpy(buffer, data->data, data->cursize);
216 *bufferLength = IntAlign(*bufferLength + data->cursize + 4);
221 qboolean Loop_CanSendMessage (qsocket_t *sock)
223 if (!sock->driverdata)
225 return sock->canSend;
229 qboolean Loop_CanSendUnreliableMessage (qsocket_t *sock)
235 void Loop_Close (qsocket_t *sock)
237 if (sock->driverdata)
238 ((qsocket_t *)sock->driverdata)->driverdata = NULL;
239 sock->receiveMessageLength = 0;
240 sock->sendMessageLength = 0;
241 sock->canSend = true;
242 if (sock == loop_client)