-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_loop.c
258 lines (195 loc) · 5.81 KB
/
net_loop.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
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 the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// net_loop.c
#include "quakedef.h"
#include "net_loop.h"
qboolean localconnectpending = false;
qsocket_t *loop_client = NULL;
qsocket_t *loop_server = NULL;
int Loop_Init (void)
{
if (cls.state == ca_dedicated)
return -1;
return 0;
}
void Loop_Shutdown (void)
{
}
void Loop_Listen (qboolean state)
{
}
void Loop_SearchForHosts (qboolean xmit)
{
if (!sv.active)
return;
hostCacheCount = 1;
if (strcmp(hostname.string, "UNNAMED") == 0)
strcpy(hostcache[0].name, "local");
else
strcpy(hostcache[0].name, hostname.string);
strcpy(hostcache[0].map, sv.name);
hostcache[0].users = net_activeconnections;
hostcache[0].maxusers = svs.maxclients;
hostcache[0].driver = net_driver;
strcpy(hostcache[0].cname, "local");
}
qsocket_t *Loop_Connect (char *host)
{
if (strcmp(host,"local") != 0)
return NULL;
localconnectpending = true;
if (!loop_client)
{
if ((loop_client = NET_NewQSocket ()) == NULL)
{
Con_Printf("Loop_Connect: no qsocket available\n");
return NULL;
}
strcpy (loop_client->address, "localhost");
}
loop_client->receiveMessageLength = 0;
loop_client->sendMessageLength = 0;
loop_client->canSend = true;
loop_client->mtu = Loop_GetDefaultMTU();
loop_client->mod = MOD_PROQUAKE; // (compat. with PQ)
loop_client->client_port = 0;
if (!loop_server)
{
if ((loop_server = NET_NewQSocket ()) == NULL)
{
Con_Printf("Loop_Connect: no qsocket available\n");
return NULL;
}
strcpy (loop_server->address, "LOCAL");
}
loop_server->receiveMessageLength = 0;
loop_server->sendMessageLength = 0;
loop_server->canSend = true;
loop_server->mtu = Loop_GetDefaultMTU();
loop_server->mod = MOD_PROQUAKE; // (compat. with PQ)
loop_server->client_port = 0;
loop_client->driverdata = (void *)loop_server;
loop_server->driverdata = (void *)loop_client;
return loop_client;
}
qsocket_t *Loop_CheckNewConnections (void)
{
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;
}
static int IntAlign(int value)
{
return (value + (sizeof(int) - 1)) & (~(sizeof(int) - 1));
}
int Loop_GetMessage (qsocket_t *sock)
{
int ret;
int length;
if (sock->receiveMessageLength == 0)
return 0;
ret = sock->receiveMessage[0];
length = sock->receiveMessage[1] + (sock->receiveMessage[2] << 8);
// alignment byte skipped here
SZ_Clear (net_message->message);
SZ_Write (net_message->message, &sock->receiveMessage[4], length);
length = IntAlign(length + 4);
sock->receiveMessageLength -= length;
// using memcpy within the same buffer is not safe, replaced by memmove
if (sock->receiveMessageLength)
memmove(sock->receiveMessage, &sock->receiveMessage[length], sock->receiveMessageLength);
if (sock->driverdata && ret == 1)
((qsocket_t *)sock->driverdata)->canSend = true;
return ret;
}
int Loop_SendMessage (qsocket_t *sock, sizebuf_t *data)
{
byte *buffer;
int *bufferLength;
if (!sock->driverdata)
return -1;
bufferLength = &((qsocket_t *)sock->driverdata)->receiveMessageLength;
if ((*bufferLength + data->cursize + 4) > NET_MAXMESSAGE)
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
memcpy(buffer, data->data, data->cursize);
*bufferLength = IntAlign(*bufferLength + data->cursize + 4);
sock->canSend = false;
return 1;
}
int Loop_SendUnreliableMessage (qsocket_t *sock, sizebuf_t *data)
{
byte *buffer;
int *bufferLength;
if (!sock->driverdata)
return -1;
bufferLength = &((qsocket_t *)sock->driverdata)->receiveMessageLength;
if ((*bufferLength + data->cursize + sizeof(byte) + sizeof(short)) > NET_MAXMESSAGE)
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
memcpy(buffer, data->data, data->cursize);
*bufferLength = IntAlign(*bufferLength + data->cursize + 4);
return 1;
}
qboolean Loop_CanSendMessage (qsocket_t *sock)
{
if (!sock->driverdata)
return false;
return sock->canSend;
}
qboolean Loop_CanSendUnreliableMessage (qsocket_t *sock)
{
return true;
}
void Loop_Close (qsocket_t *sock)
{
if (sock->driverdata)
((qsocket_t *)sock->driverdata)->driverdata = NULL;
sock->receiveMessageLength = 0;
sock->sendMessageLength = 0;
sock->canSend = true;
if (sock == loop_client)
loop_client = NULL;
else
loop_server = NULL;
}
int Loop_GetDefaultMTU (void)
{
// The loop driver can send the maximum message size in one packet
return MAX_MSGLEN;
}