/
cl_frame.c
442 lines (369 loc) · 11.9 KB
/
cl_frame.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/**\file
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2011 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2006-2009 Daniel Swanson <danij@dengine.net>
*
* 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/**
* cl_frame.c: Frame Reception
*/
// HEADER FILES ------------------------------------------------------------
#include "de_base.h"
#include "de_console.h"
#include "de_network.h"
// MACROS ------------------------------------------------------------------
#if 0
#define SET_HISTORY_SIZE 50
#define RESEND_HISTORY_SIZE 50
#endif
// TYPES -------------------------------------------------------------------
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
extern int gotFrame;
// PUBLIC DATA DEFINITIONS -------------------------------------------------
// Set to true when the PSV_FIRST_FRAME2 packet is received.
// Until then, all PSV_FRAME2 packets are ignored (they must be
// from the wrong map).
boolean gotFirstFrame;
//int predicted_tics;
// PRIVATE DATA DEFINITIONS ------------------------------------------------
#if 0
// Ordinal of the latest set received by the client. Used for detecting deltas
// that arrive out of order. The ordinal is the logical equivalent of the set
// identifier (which is 0...255).
static uint latestSetOrdinal;
static byte latestSet;
// The ordinal base is added to the set number to convert it from a set
// identifier to an ordinal. Every time the set numbers wrap around from 255
// to zero, the ordinal is incremented by 256.
static uint setOrdinalBase;
// The set history keeps track of received sets and is used to detect
// duplicate frames.
static short setHistory[SET_HISTORY_SIZE];
static int historyIdx;
// The resend ID history keeps track of received resend deltas. Used
// to detect duplicate resends.
static byte resendHistory[RESEND_HISTORY_SIZE];
static int resendHistoryIdx;
#endif
// CODE --------------------------------------------------------------------
/**
* Clear the history of received set numbers.
*/
void Cl_InitFrame(void)
{
Cl_ResetFrame();
#if 0
// -1 denotes an invalid entry.
memset(setHistory, -1, sizeof(setHistory));
historyIdx = 0;
// Clear the resend ID history.
memset(resendHistory, 0, sizeof(resendHistory));
resendHistoryIdx = 0;
// Reset ordinal counters.
latestSet = 0;
latestSetOrdinal = 0;
setOrdinalBase = 256;
#endif
}
/**
* Called when the map changes.
*/
void Cl_ResetFrame(void)
{
gotFrame = false;
// All frames received before the PSV_FIRST_FRAME2 are ignored.
// They must be from the wrong map.
gotFirstFrame = false;
}
#if 0
/**
* Add a set number to the history.
*/
void Cl_HistoryAdd(byte set)
{
setHistory[historyIdx++] = set;
if(historyIdx >= SET_HISTORY_SIZE)
historyIdx -= SET_HISTORY_SIZE;
}
/**
* @return @c true, if the set is found in the recent set
* history.
*/
boolean Cl_HistoryCheck(byte set)
{
uint i;
for(i = 0; i < SET_HISTORY_SIZE; ++i)
{
if(setHistory[i] == set)
return true;
}
return false;
}
/**
* Add a resend ID number to the resend history.
*/
void Cl_ResendHistoryAdd(byte id)
{
resendHistory[resendHistoryIdx++] = id;
if(resendHistoryIdx >= RESEND_HISTORY_SIZE)
resendHistoryIdx -= RESEND_HISTORY_SIZE;
}
/**
* @return @c true, if the resend ID is found in the history.
*/
boolean Cl_ResendHistoryCheck(byte id)
{
uint i;
for(i = 0; i < RESEND_HISTORY_SIZE; ++i)
{
if(resendHistory[i] == id)
return true;
}
return false;
}
/**
* Converts a set identifier, which ranges from 0...255, into a logical
* ordinal. Checks for set identifier wraparounds and updates the set
* ordinal base accordingly.
*/
uint Cl_ConvertSetToOrdinal(byte set)
{
uint ordinal = 0;
if(latestSet > 185 && set < 70)
{
// We must conclude that wraparound has occured.
setOrdinalBase += 256;
#if _NETDEBUG
VERBOSE2( Con_Printf("Cl_ConvertSetToOrdinal: Wraparound, now base is %i.\n",
setOrdinalBase) );
#endif
}
ordinal = set + setOrdinalBase;
if(latestSet < 35 && set > 220)
{
// This is most likely a set that came in before wraparound.
ordinal -= 256;
}
else
{
latestSet = set;
}
return ordinal;
}
#endif
/**
* Read a PSV_FRAME2/PSV_FIRST_FRAME2 packet.
*/
void Cl_Frame2Received(int packetType)
{
byte set = Msg_ReadByte(), deltaType;
//byte resendAcks[300];
//int i, numResendAcks = 0;
boolean skip = false;
#ifdef _NETDEBUG
int deltaCount = 0;
int startOffset;
int deltaLength;
#endif
// All frames that arrive before the first frame are ignored.
// They are most likely from the wrong map.
if(packetType == PSV_FIRST_FRAME2)
{
gotFirstFrame = true;
#ifdef _DEBUG
VERBOSE( Con_Printf("*** GOT THE FIRST FRAME (%i) ***\n", set) );
#endif
}
else if(!gotFirstFrame)
{
// Just ignore. If this was a legitimate frame, the server will
// send it again when it notices no ack is coming.
#ifdef _DEBUG
VERBOSE( Con_Printf("==> Ignored set %i\n", set) );
#endif
return;
}
#ifdef _DEBUG
VERBOSE2( Con_Printf("Cl_Frame2Received: Processing delta set %i.\n", set) );
#endif
#if 0
if(packetType != PSV_FIRST_FRAME2)
{
// If this is not the first frame, it will be ignored if it arrives
// out of order.
uint ordinal = Cl_ConvertSetToOrdinal(set);
if(ordinal < latestSetOrdinal)
{
VERBOSE2( Con_Printf("==> Ignored set %i because it arrived out of order.\n",
set) );
return;
}
latestSetOrdinal = ordinal;
VERBOSE2( Con_Printf("Latest set ordinal is %i.\n", latestSetOrdinal) );
}
#endif
// Check for duplicates (might happen if ack doesn't get through
// or ack arrives too late).
//if(!Cl_HistoryCheck(set))
{
// It isn't yet in the history, so add it there.
//Cl_HistoryAdd(set);
VERBOSE2( Con_Printf("Starting to process deltas in set %i.\n", set) );
#ifdef _NETDEBUG
deltaCount = Msg_ReadLong();
VERBOSE2( Con_Message("Set contains %i deltas.\n", deltaCount) );
#endif
// Read and process the message.
while(!Msg_End())
{
#ifdef _NETDEBUG
/*Con_Message("Starting to read delta %i of %i...\n", ++readCount,
deltaCount);*/
// Check length field.
startOffset = Msg_Offset();
deltaLength = Msg_ReadLong();
//Con_Message("Incoming delta length %i bytes.\n", deltaLength);
#endif
deltaType = Msg_ReadByte();
#ifdef _NETDEBUG
//Con_Message(" Delta type is %i.\n", deltaType & ~DT_RESENT);
#endif
skip = false;
VERBOSE2( Con_Printf("Received delta %i.\n", deltaType & ~DT_RESENT) );
/*
// Is this a resent delta?
if(deltaType & DT_RESENT)
{
VERBOSE2( Con_Printf(" This is a resent delta.\n") );
deltaType &= ~DT_RESENT;
// Read the set number this was originally in.
oldSet = Msg_ReadByte();
// Read the resend ID.
resend = Msg_ReadByte();
// Did we already receive this delta?
{
boolean historyChecked = Cl_HistoryCheck(oldSet);
boolean resendChecked = Cl_ResendHistoryCheck(resend);
if(historyChecked || resendChecked)
{
// Yes, we've already got this. Must skip.
skip = true;
Con_Message("CL_Frame2Received: Skipping delta %i (oldset=%i, rsid=%i), because history=%i resend=%i\n",
deltaType, oldSet, resend, historyChecked, resendChecked);
}
}
// We must acknowledge that we've received this.
resendAcks[numResendAcks++] = resend;
Cl_ResendHistoryAdd(resend);
}
else
{
VERBOSE2( Con_Printf(" Not resent.\n") );
}
*/
switch(deltaType)
{
case DT_CREATE_MOBJ:
// The mobj will be created/shown.
ClMobj_ReadDelta2(skip);
break;
case DT_MOBJ:
// The mobj will be hidden if it's not yet Created.
ClMobj_ReadDelta2(skip);
break;
case DT_NULL_MOBJ:
// The mobj will be removed.
ClMobj_ReadNullDelta2(skip);
break;
case DT_PLAYER:
ClPlayer_ReadDelta2(skip);
break;
//case DT_SECTOR_R6: // Old format.
case DT_SECTOR:
Cl_ReadSectorDelta2(deltaType, skip);
break;
//case DT_SIDE_R6: // Old format.
case DT_SIDE:
Cl_ReadSideDelta2(deltaType, skip);
break;
case DT_POLY:
Cl_ReadPolyDelta2(skip);
break;
case DT_SOUND:
case DT_MOBJ_SOUND:
case DT_SECTOR_SOUND:
case DT_POLY_SOUND:
Cl_ReadSoundDelta2(deltaType, skip);
break;
default:
Con_Error("Cl_Frame2Received: Unknown delta type %i.\n",
deltaType);
}
#ifdef _NETDEBUG
// Check that we didn't misread.
if(Msg_Offset() - startOffset != deltaLength)
{
Con_Error("Cl_Frame2Received: Misinterpreted delta! Real length was "
"%i bytes, but we read %i bytes.\n",
deltaLength, Msg_Offset() - startOffset);
}
#endif
}
// We have now received a frame.
gotFrame = true;
// Reset the predict counter.
//predicted_tics = 0;
}
/*
if(numResendAcks == 0)
{
// Acknowledge the set.
Msg_Begin(PCL_ACK_SETS);
Msg_WriteByte(set);
#ifdef _DEBUG
VERBOSE2( Con_Printf("Cl_Frame2Received: Ack set %i. Nothing was resent.\n", set) );
#endif
}
else
{
// Acknowledge the set and the resent deltas.
Msg_Begin(PCL_ACKS);
Msg_WriteByte(set);
#ifdef _DEBUG
VERBOSE2( Con_Printf("Cl_Frame2Received: Ack set %i. Contained %i resent deltas: \n",
set, numResendAcks) );
#endif
for(i = 0; i < numResendAcks; ++i)
{
Msg_WriteByte(resendAcks[i]);
#ifdef _DEBUG
VERBOSE2( Con_Printf("%i ", resendAcks[i]) );
#endif
}
#ifdef _DEBUG
VERBOSE2( Con_Printf("\n") );
#endif
}
Net_SendBuffer(0, 0);
*/
}