Debian Codegraph @

code/server/sv_net_chan.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22
23 #include "../qcommon/q_shared.h"
24 #include "../qcommon/qcommon.h"
25 #include "server.h"
26
27 #ifdef LEGACY_PROTOCOL
28 /*
29 ==============
30 SV_Netchan_Encode
31
32 // first four bytes of the data are always:
33 long reliableAcknowledge;
34
35 ==============
36 */
37 static void SV_Netchan_Encode(client_t *client, msg_t *msg, const char *clientCommandString)
38 {
39 long i, index;
40 byte key, *string;
41 int srdc, sbit;
42 qboolean soob;
43
44 if ( msg->cursize < SV_ENCODE_START ) {
45 return;
46 }
47
48 srdc = msg->readcount;
49 sbit = msg->bit;
50 soob = msg->oob;
51
52 msg->bit = 0;
53 msg->readcount = 0;
54 msg->oob = qfalse;
55
56 /* reliableAcknowledge = */ MSG_ReadLong(msg);
57
58 msg->oob = soob;
59 msg->bit = sbit;
60 msg->readcount = srdc;
61
62 string = (byte *) clientCommandString;
63 index = 0;
64 // xor the client challenge with the netchan sequence number
65 key = client->challenge ^ client->netchan.outgoingSequence;
66 for (i = SV_ENCODE_START; i < msg->cursize; i++) {
67 // modify the key with the last received and with this message acknowledged client command
68 if (!string[index])
69 index = 0;
70 if (string[index] > 127 || string[index] == '%') {
71 key ^= '.' << (i & 1);
72 }
73 else {
74 key ^= string[index] << (i & 1);
75 }
76 index++;
77 // encode the data with this key
78 *(msg->data + i) = *(msg->data + i) ^ key;
79 }
80 }
81
82 /*
83 ==============
84 SV_Netchan_Decode
85
86 // first 12 bytes of the data are always:
87 long serverId;
88 long messageAcknowledge;
89 long reliableAcknowledge;
90
91 ==============
92 */
93 static void SV_Netchan_Decode( client_t *client, msg_t *msg ) {
94 int serverId, messageAcknowledge, reliableAcknowledge;
95 int i, index, srdc, sbit;
96 qboolean soob;
97 byte key, *string;
98
99 srdc = msg->readcount;
100 sbit = msg->bit;
101 soob = msg->oob;
102
103 msg->oob = qfalse;
104
105 serverId = MSG_ReadLong(msg);
106 messageAcknowledge = MSG_ReadLong(msg);
107 reliableAcknowledge = MSG_ReadLong(msg);
108
109 msg->oob = soob;
110 msg->bit = sbit;
111 msg->readcount = srdc;
112
113 string = (byte *)client->reliableCommands[ reliableAcknowledge & (MAX_RELIABLE_COMMANDS-1) ];
114 index = 0;
115 //
116 key = client->challenge ^ serverId ^ messageAcknowledge;
117 for (i = msg->readcount + SV_DECODE_START; i < msg->cursize; i++) {
118 // modify the key with the last sent and acknowledged server command
119 if (!string[index])
120 index = 0;
121 if (string[index] > 127 || string[index] == '%') {
122 key ^= '.' << (i & 1);
123 }
124 else {
125 key ^= string[index] << (i & 1);
126 }
127 index++;
128 // decode the data with this key
129 *(msg->data + i) = *(msg->data + i) ^ key;
130 }
131 }
132 #endif
133
134
135
136 /*
137 =================
138 SV_Netchan_FreeQueue
139 =================
140 */
141 void SV_Netchan_FreeQueue(client_t *client)
142 {
143 netchan_buffer_t *netbuf, *next;
144
145 for(netbuf = client->netchan_start_queue; netbuf; netbuf = next)
146 {
147 next = netbuf->next;
148 Z_Free(netbuf);
149 }
150
151 client->netchan_start_queue = NULL;
152 client->netchan_end_queue = &client->netchan_start_queue;
153 }
154
155 /*
156 =================
157 SV_Netchan_TransmitNextInQueue
158 =================
159 */
160 void SV_Netchan_TransmitNextInQueue(client_t *client)
161 {
162 netchan_buffer_t *netbuf;
163
164 Com_DPrintf("#462 Netchan_TransmitNextFragment: popping a queued message for transmit\n");
165 netbuf = client->netchan_start_queue;
166
167 #ifdef LEGACY_PROTOCOL
168 if(client->compat)
169 SV_Netchan_Encode(client, &netbuf->msg, netbuf->clientCommandString);
170 #endif
171
172 Netchan_Transmit(&client->netchan, netbuf->msg.cursize, netbuf->msg.data);
173
174 // pop from queue
175 client->netchan_start_queue = netbuf->next;
176 if(!client->netchan_start_queue)
177 {
178 Com_DPrintf("#462 Netchan_TransmitNextFragment: emptied queue\n");
179 client->netchan_end_queue = &client->netchan_start_queue;
180 }
181 else
182 Com_DPrintf("#462 Netchan_TransmitNextFragment: remaining queued message\n");
183
184 Z_Free(netbuf);
185 }
186
187 /*
188 =================
189 SV_Netchan_TransmitNextFragment
190 Transmit the next fragment and the next queued packet
191 Return number of ms until next message can be sent based on throughput given by client rate,
192 -1 if no packet was sent.
193 =================
194 */
195
196 int SV_Netchan_TransmitNextFragment(client_t *client)
197 {
198 if(client->netchan.unsentFragments)
199 {
200 Netchan_TransmitNextFragment(&client->netchan);
201 return SV_RateMsec(client);
202 }
203 else if(client->netchan_start_queue)
204 {
205 SV_Netchan_TransmitNextInQueue(client);
206 return SV_RateMsec(client);
207 }
208
209 return -1;
210 }
211
212
213 /*
214 ===============
215 SV_Netchan_Transmit
216 TTimo
217 https://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=462
218 if there are some unsent fragments (which may happen if the snapshots
219 and the gamestate are fragmenting, and collide on send for instance)
220 then buffer them and make sure they get sent in correct order
221 ================
222 */
223
224 void SV_Netchan_Transmit( client_t *client, msg_t *msg)
225 {
226 MSG_WriteByte( msg, svc_EOF );
227
228 if(client->netchan.unsentFragments || client->netchan_start_queue)
229 {
230 netchan_buffer_t *netbuf;
231 Com_DPrintf("#462 SV_Netchan_Transmit: unsent fragments, stacked\n");
232 netbuf = (netchan_buffer_t *) Z_Malloc(sizeof(netchan_buffer_t));
233 // store the msg, we can't store it encoded, as the encoding depends on stuff we still have to finish sending
234 MSG_Copy(&netbuf->msg, netbuf->msgBuffer, sizeof( netbuf->msgBuffer ), msg);
235 #ifdef LEGACY_PROTOCOL
236 if(client->compat)
237 {
238 Q_strncpyz(netbuf->clientCommandString, client->lastClientCommandString,
239 sizeof(netbuf->clientCommandString));
240 }
241 #endif
242 netbuf->next = NULL;
243 // insert it in the queue, the message will be encoded and sent later
244 *client->netchan_end_queue = netbuf;
245 client->netchan_end_queue = &(*client->netchan_end_queue)->next;
246 }
247 else
248 {
249 #ifdef LEGACY_PROTOCOL
250 if(client->compat)
251 SV_Netchan_Encode(client, msg, client->lastClientCommandString);
252 #endif
253 Netchan_Transmit( &client->netchan, msg->cursize, msg->data );
254 }
255 }
256
257 /*
258 =================
259 Netchan_SV_Process
260 =================
261 */
262 qboolean SV_Netchan_Process( client_t *client, msg_t *msg ) {
263 int ret;
264 ret = Netchan_Process( &client->netchan, msg );
265 if (!ret)
266 return qfalse;
267
268 #ifdef LEGACY_PROTOCOL
269 if(client->compat)
270 SV_Netchan_Decode(client, msg);
271 #endif
272
273 return qtrue;
274 }
275