Debian Codegraph @

code/client/cl_parse.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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 // cl_parse.c -- parse a message received from the server
23
24 #include "client.h"
25
26 char *svc_strings[256] = {
27 "svc_bad",
28
29 "svc_nop",
30 "svc_gamestate",
31 "svc_configstring",
32 "svc_baseline",
33 "svc_serverCommand",
34 "svc_download",
35 "svc_snapshot",
36 "svc_EOF",
37 "svc_voipSpeex",
38 "svc_voipOpus",
39 };
40
41 void SHOWNET( msg_t *msg, char *s) {
42 if ( cl_shownet->integer >= 2) {
43 Com_Printf ("%3i:%s\n", msg->readcount-1, s);
44 }
45 }
46
47
48 /*
49 =========================================================================
50
51 MESSAGE PARSING
52
53 =========================================================================
54 */
55
56 /*
57 ==================
58 CL_DeltaEntity
59
60 Parses deltas from the given base and adds the resulting entity
61 to the current frame
62 ==================
63 */
64 void CL_DeltaEntity (msg_t *msg, clSnapshot_t *frame, int newnum, entityState_t *old,
65 qboolean unchanged) {
66 entityState_t *state;
67
68 // save the parsed entity state into the big circular buffer so
69 // it can be used as the source for a later delta
70 state = &cl.parseEntities[cl.parseEntitiesNum & (MAX_PARSE_ENTITIES-1)];
71
72 if ( unchanged ) {
73 *state = *old;
74 } else {
75 MSG_ReadDeltaEntity( msg, old, state, newnum );
76 }
77
78 if ( state->number == (MAX_GENTITIES-1) ) {
79 return; // entity was delta removed
80 }
81 cl.parseEntitiesNum++;
82 frame->numEntities++;
83 }
84
85 /*
86 ==================
87 CL_ParsePacketEntities
88
89 ==================
90 */
91 void CL_ParsePacketEntities( msg_t *msg, clSnapshot_t *oldframe, clSnapshot_t *newframe) {
92 int newnum;
93 entityState_t *oldstate;
94 int oldindex, oldnum;
95
96 newframe->parseEntitiesNum = cl.parseEntitiesNum;
97 newframe->numEntities = 0;
98
99 // delta from the entities present in oldframe
100 oldindex = 0;
101 oldstate = NULL;
102 if (!oldframe) {
103 oldnum = 99999;
104 } else {
105 if ( oldindex >= oldframe->numEntities ) {
106 oldnum = 99999;
107 } else {
108 oldstate = &cl.parseEntities[
109 (oldframe->parseEntitiesNum + oldindex) & (MAX_PARSE_ENTITIES-1)];
110 oldnum = oldstate->number;
111 }
112 }
113
114 while ( 1 ) {
115 // read the entity index number
116 newnum = MSG_ReadBits( msg, GENTITYNUM_BITS );
117
118 if ( newnum == (MAX_GENTITIES-1) ) {
119 break;
120 }
121
122 if ( msg->readcount > msg->cursize ) {
123 Com_Error (ERR_DROP,"CL_ParsePacketEntities: end of message");
124 }
125
126 while ( oldnum < newnum ) {
127 // one or more entities from the old packet are unchanged
128 if ( cl_shownet->integer == 3 ) {
129 Com_Printf ("%3i: unchanged: %i\n", msg->readcount, oldnum);
130 }
131 CL_DeltaEntity( msg, newframe, oldnum, oldstate, qtrue );
132
133 oldindex++;
134
135 if ( oldindex >= oldframe->numEntities ) {
136 oldnum = 99999;
137 } else {
138 oldstate = &cl.parseEntities[
139 (oldframe->parseEntitiesNum + oldindex) & (MAX_PARSE_ENTITIES-1)];
140 oldnum = oldstate->number;
141 }
142 }
143 if (oldnum == newnum) {
144 // delta from previous state
145 if ( cl_shownet->integer == 3 ) {
146 Com_Printf ("%3i: delta: %i\n", msg->readcount, newnum);
147 }
148 CL_DeltaEntity( msg, newframe, newnum, oldstate, qfalse );
149
150 oldindex++;
151
152 if ( oldindex >= oldframe->numEntities ) {
153 oldnum = 99999;
154 } else {
155 oldstate = &cl.parseEntities[
156 (oldframe->parseEntitiesNum + oldindex) & (MAX_PARSE_ENTITIES-1)];
157 oldnum = oldstate->number;
158 }
159 continue;
160 }
161
162 if ( oldnum > newnum ) {
163 // delta from baseline
164 if ( cl_shownet->integer == 3 ) {
165 Com_Printf ("%3i: baseline: %i\n", msg->readcount, newnum);
166 }
167 CL_DeltaEntity( msg, newframe, newnum, &cl.entityBaselines[newnum], qfalse );
168 continue;
169 }
170
171 }
172
173 // any remaining entities in the old frame are copied over
174 while ( oldnum != 99999 ) {
175 // one or more entities from the old packet are unchanged
176 if ( cl_shownet->integer == 3 ) {
177 Com_Printf ("%3i: unchanged: %i\n", msg->readcount, oldnum);
178 }
179 CL_DeltaEntity( msg, newframe, oldnum, oldstate, qtrue );
180
181 oldindex++;
182
183 if ( oldindex >= oldframe->numEntities ) {
184 oldnum = 99999;
185 } else {
186 oldstate = &cl.parseEntities[
187 (oldframe->parseEntitiesNum + oldindex) & (MAX_PARSE_ENTITIES-1)];
188 oldnum = oldstate->number;
189 }
190 }
191 }
192
193
194 /*
195 ================
196 CL_ParseSnapshot
197
198 If the snapshot is parsed properly, it will be copied to
199 cl.snap and saved in cl.snapshots[]. If the snapshot is invalid
200 for any reason, no changes to the state will be made at all.
201 ================
202 */
203 void CL_ParseSnapshot( msg_t *msg ) {
204 int len;
205 clSnapshot_t *old;
206 clSnapshot_t newSnap;
207 int deltaNum;
208 int oldMessageNum;
209 int i, packetNum;
210
211 // get the reliable sequence acknowledge number
212 // NOTE: now sent with all server to client messages
213 //clc.reliableAcknowledge = MSG_ReadLong( msg );
214
215 // read in the new snapshot to a temporary buffer
216 // we will only copy to cl.snap if it is valid
217 Com_Memset (&newSnap, 0, sizeof(newSnap));
218
219 // we will have read any new server commands in this
220 // message before we got to svc_snapshot
221 newSnap.serverCommandNum = clc.serverCommandSequence;
222
223 newSnap.serverTime = MSG_ReadLong( msg );
224
225 // if we were just unpaused, we can only *now* really let the
226 // change come into effect or the client hangs.
227 cl_paused->modified = 0;
228
229 newSnap.messageNum = clc.serverMessageSequence;
230
231 deltaNum = MSG_ReadByte( msg );
232 if ( !deltaNum ) {
233 newSnap.deltaNum = -1;
234 } else {
235 newSnap.deltaNum = newSnap.messageNum - deltaNum;
236 }
237 newSnap.snapFlags = MSG_ReadByte( msg );
238
239 // If the frame is delta compressed from data that we
240 // no longer have available, we must suck up the rest of
241 // the frame, but not use it, then ask for a non-compressed
242 // message
243 if ( newSnap.deltaNum <= 0 ) {
244 newSnap.valid = qtrue; // uncompressed frame
245 old = NULL;
246 clc.demowaiting = qfalse; // we can start recording now
247 } else {
248 old = &cl.snapshots[newSnap.deltaNum & PACKET_MASK];
249 if ( !old->valid ) {
250 // should never happen
251 Com_Printf ("Delta from invalid frame (not supposed to happen!).\n");
252 } else if ( old->messageNum != newSnap.deltaNum ) {
253 // The frame that the server did the delta from
254 // is too old, so we can't reconstruct it properly.
255 Com_Printf ("Delta frame too old.\n");
256 } else if ( cl.parseEntitiesNum - old->parseEntitiesNum > MAX_PARSE_ENTITIES - MAX_SNAPSHOT_ENTITIES ) {
257 Com_Printf ("Delta parseEntitiesNum too old.\n");
258 } else {
259 newSnap.valid = qtrue; // valid delta parse
260 }
261 }
262
263 // read areamask
264 len = MSG_ReadByte( msg );
265
266 if(len > sizeof(newSnap.areamask))
267 {
268 Com_Error (ERR_DROP,"CL_ParseSnapshot: Invalid size %d for areamask", len);
269 return;
270 }
271
272 MSG_ReadData( msg, &newSnap.areamask, len);
273
274 // read playerinfo
275 SHOWNET( msg, "playerstate" );
276 if ( old ) {
277 MSG_ReadDeltaPlayerstate( msg, &old->ps, &newSnap.ps );
278 } else {
279 MSG_ReadDeltaPlayerstate( msg, NULL, &newSnap.ps );
280 }
281
282 // read packet entities
283 SHOWNET( msg, "packet entities" );
284 CL_ParsePacketEntities( msg, old, &newSnap );
285
286 // if not valid, dump the entire thing now that it has
287 // been properly read
288 if ( !newSnap.valid ) {
289 return;
290 }
291
292 // clear the valid flags of any snapshots between the last
293 // received and this one, so if there was a dropped packet
294 // it won't look like something valid to delta from next
295 // time we wrap around in the buffer
296 oldMessageNum = cl.snap.messageNum + 1;
297
298 if ( newSnap.messageNum - oldMessageNum >= PACKET_BACKUP ) {
299 oldMessageNum = newSnap.messageNum - ( PACKET_BACKUP - 1 );
300 }
301 for ( ; oldMessageNum < newSnap.messageNum ; oldMessageNum++ ) {
302 cl.snapshots[oldMessageNum & PACKET_MASK].valid = qfalse;
303 }
304
305 // copy to the current good spot
306 cl.snap = newSnap;
307 cl.snap.ping = 999;
308 // calculate ping time
309 for ( i = 0 ; i < PACKET_BACKUP ; i++ ) {
310 packetNum = ( clc.netchan.outgoingSequence - 1 - i ) & PACKET_MASK;
311 if ( cl.snap.ps.commandTime >= cl.outPackets[ packetNum ].p_serverTime ) {
312 cl.snap.ping = cls.realtime - cl.outPackets[ packetNum ].p_realtime;
313 break;
314 }
315 }
316 // save the frame off in the backup array for later delta comparisons
317 cl.snapshots[cl.snap.messageNum & PACKET_MASK] = cl.snap;
318
319 if (cl_shownet->integer == 3) {
320 Com_Printf( " snapshot:%i delta:%i ping:%i\n", cl.snap.messageNum,
321 cl.snap.deltaNum, cl.snap.ping );
322 }
323
324 cl.newSnapshots = qtrue;
325 }
326
327
328 //=====================================================================
329
330 int cl_connectedToPureServer;
331 int cl_connectedToCheatServer;
332
333 /*
334 ==================
335 CL_SystemInfoChanged
336
337 The systeminfo configstring has been changed, so parse
338 new information out of it. This will happen at every
339 gamestate, and possibly during gameplay.
340 ==================
341 */
342 void CL_SystemInfoChanged( void ) {
343 char *systemInfo;
344 const char *s, *t;
345 char key[BIG_INFO_KEY];
346 char value[BIG_INFO_VALUE];
347 qboolean gameSet;
348
349 systemInfo = cl.gameState.stringData + cl.gameState.stringOffsets[ CS_SYSTEMINFO ];
350 // NOTE TTimo:
351 // when the serverId changes, any further messages we send to the server will use this new serverId
352 // https://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=475
353 // in some cases, outdated cp commands might get sent with this news serverId
354 cl.serverId = atoi( Info_ValueForKey( systemInfo, "sv_serverid" ) );
355
356 #ifdef USE_VOIP
357 #ifdef LEGACY_PROTOCOL
358 if(clc.compat)
359 clc.voipEnabled = qfalse;
360 else
361 #endif
362 {
363 s = Info_ValueForKey( systemInfo, "sv_voipProtocol" );
364 clc.voipEnabled = !Q_stricmp(s, "opus");
365 }
366 #endif
367
368 // don't set any vars when playing a demo
369 if ( clc.demoplaying ) {
370 return;
371 }
372
373 s = Info_ValueForKey( systemInfo, "sv_cheats" );
374 cl_connectedToCheatServer = atoi( s );
375 if ( !cl_connectedToCheatServer ) {
376 Cvar_SetCheatState();
377 }
378
379 // check pure server string
380 s = Info_ValueForKey( systemInfo, "sv_paks" );
381 t = Info_ValueForKey( systemInfo, "sv_pakNames" );
382 FS_PureServerSetLoadedPaks( s, t );
383
384 s = Info_ValueForKey( systemInfo, "sv_referencedPaks" );
385 t = Info_ValueForKey( systemInfo, "sv_referencedPakNames" );
386 FS_PureServerSetReferencedPaks( s, t );
387
388 gameSet = qfalse;
389 // scan through all the variables in the systeminfo and locally set cvars to match
390 s = systemInfo;
391 while ( s ) {
392 int cvar_flags;
393
394 Info_NextPair( &s, key, value );
395 if ( !key[0] ) {
396 break;
397 }
398
399 // ehw!
400 if (!Q_stricmp(key, "fs_game"))
401 {
402 if(FS_InvalidGameDir(value))
403 {
404 Com_Printf(S_COLOR_YELLOW "WARNING: Server sent invalid fs_game value %s\n", value);
405 continue;
406 }
407
408 gameSet = qtrue;
409 }
410
411 if((cvar_flags = Cvar_Flags(key)) == CVAR_NONEXISTENT)
412 Cvar_Get(key, value, CVAR_SERVER_CREATED | CVAR_ROM);
413 else
414 {
415 // If this cvar may not be modified by a server discard the value.
416 if(!(cvar_flags & (CVAR_SYSTEMINFO | CVAR_SERVER_CREATED | CVAR_USER_CREATED)))
417 {
•418 #if !defined(STANDALONE) || defined(WITH_OPENARENA)
419 if(Q_stricmp(key, "g_synchronousClients") && Q_stricmp(key, "pmove_fixed") &&
•420 Q_stricmp(key, "pmove_msec") && Q_stricmp(key, "sv_fps"))
421 #endif
422 {
423 Com_Printf(S_COLOR_YELLOW "WARNING: server is not allowed to set %s=%s\n", key, value);
424 continue;
425 }
426 }
427
428 Cvar_SetSafe(key, value);
429 }
430 }
431 // if game folder should not be set and it is set at the client side
432 if ( !gameSet && *Cvar_VariableString("fs_game") ) {
433 Cvar_Set( "fs_game", "" );
434 }
435 cl_connectedToPureServer = Cvar_VariableValue( "sv_pure" );
436 }
437
438 /*
439 ==================
440 CL_ParseServerInfo
441 ==================
442 */
443 static void CL_ParseServerInfo(void)
444 {
445 const char *serverInfo;
446
447 serverInfo = cl.gameState.stringData
448 + cl.gameState.stringOffsets[ CS_SERVERINFO ];
449
450 clc.sv_allowDownload = atoi(Info_ValueForKey(serverInfo,
451 "sv_allowDownload"));
452 Q_strncpyz(clc.sv_dlURL,
453 Info_ValueForKey(serverInfo, "sv_dlURL"),
454 sizeof(clc.sv_dlURL));
455 }
456
457 /*
458 ==================
459 CL_ParseGamestate
460 ==================
461 */
462 void CL_ParseGamestate( msg_t *msg ) {
463 int i;
464 entityState_t *es;
465 int newnum;
466 entityState_t nullstate;
467 int cmd;
468 char *s;
469 char oldGame[MAX_QPATH];
470
471 Con_Close();
472
473 clc.connectPacketCount = 0;
474
475 // wipe local client state
476 CL_ClearState();
477
478 // a gamestate always marks a server command sequence
479 clc.serverCommandSequence = MSG_ReadLong( msg );
480
481 // parse all the configstrings and baselines
482 cl.gameState.dataCount = 1; // leave a 0 at the beginning for uninitialized configstrings
483 while ( 1 ) {
484 cmd = MSG_ReadByte( msg );
485
486 if ( cmd == svc_EOF ) {
487 break;
488 }
489
490 if ( cmd == svc_configstring ) {
491 int len;
492
493 i = MSG_ReadShort( msg );
494 if ( i < 0 || i >= MAX_CONFIGSTRINGS ) {
495 Com_Error( ERR_DROP, "configstring > MAX_CONFIGSTRINGS" );
496 }
497 s = MSG_ReadBigString( msg );
498 len = strlen( s );
499
500 if ( len + 1 + cl.gameState.dataCount > MAX_GAMESTATE_CHARS ) {
501 Com_Error( ERR_DROP, "MAX_GAMESTATE_CHARS exceeded" );
502 }
503
504 // append it to the gameState string buffer
505 cl.gameState.stringOffsets[ i ] = cl.gameState.dataCount;
506 Com_Memcpy( cl.gameState.stringData + cl.gameState.dataCount, s, len + 1 );
507 cl.gameState.dataCount += len + 1;
508 } else if ( cmd == svc_baseline ) {
509 newnum = MSG_ReadBits( msg, GENTITYNUM_BITS );
510 if ( newnum < 0 || newnum >= MAX_GENTITIES ) {
511 Com_Error( ERR_DROP, "Baseline number out of range: %i", newnum );
512 }
513 Com_Memset (&nullstate, 0, sizeof(nullstate));
514 es = &cl.entityBaselines[ newnum ];
515 MSG_ReadDeltaEntity( msg, &nullstate, es, newnum );
516 } else {
517 Com_Error( ERR_DROP, "CL_ParseGamestate: bad command byte" );
518 }
519 }
520
521 clc.clientNum = MSG_ReadLong(msg);
522 // read the checksum feed
523 clc.checksumFeed = MSG_ReadLong( msg );
524
525 // save old gamedir
526 Cvar_VariableStringBuffer("fs_game", oldGame, sizeof(oldGame));
527
528 // parse useful values out of CS_SERVERINFO
529 CL_ParseServerInfo();
530
531 // parse serverId and other cvars
532 CL_SystemInfoChanged();
533
534 // stop recording now so the demo won't have an unnecessary level load at the end.
535 if(cl_autoRecordDemo->integer && clc.demorecording)
536 CL_StopRecord_f();
537
538 // reinitialize the filesystem if the game directory has changed
539 if(!cl_oldGameSet && (Cvar_Flags("fs_game") & CVAR_MODIFIED))
540 {
541 cl_oldGameSet = qtrue;
542 Q_strncpyz(cl_oldGame, oldGame, sizeof(cl_oldGame));
543 }
544
545 FS_ConditionalRestart(clc.checksumFeed, qfalse);
546
547 // This used to call CL_StartHunkUsers, but now we enter the download state before loading the
548 // cgame
549 CL_InitDownloads();
550
551 // make sure the game starts
552 Cvar_Set( "cl_paused", "0" );
553 }
554
555
556 //=====================================================================
557
558 /*
559 =====================
560 CL_ParseDownload
561
562 A download message has been received from the server
563 =====================
564 */
565 void CL_ParseDownload ( msg_t *msg ) {
566 int size;
567 unsigned char data[MAX_MSGLEN];
568 uint16_t block;
569
570 if (!*clc.downloadTempName) {
571 Com_Printf("Server sending download, but no download was requested\n");
572 CL_AddReliableCommand("stopdl", qfalse);
573 return;
574 }
575
576 // read the data
577 block = MSG_ReadShort ( msg );
578
579 if(!block && !clc.downloadBlock)
580 {
581 // block zero is special, contains file size
582 clc.downloadSize = MSG_ReadLong ( msg );
583
584 Cvar_SetValue( "cl_downloadSize", clc.downloadSize );
585
586 if (clc.downloadSize < 0)
587 {
588 Com_Error( ERR_DROP, "%s", MSG_ReadString( msg ) );
589 return;
590 }
591 }
592
593 size = MSG_ReadShort ( msg );
594 if (size < 0 || size > sizeof(data))
595 {
596 Com_Error(ERR_DROP, "CL_ParseDownload: Invalid size %d for download chunk", size);
597 return;
598 }
599
600 MSG_ReadData(msg, data, size);
601
602 if((clc.downloadBlock & 0xFFFF) != block)
603 {
604 Com_DPrintf( "CL_ParseDownload: Expected block %d, got %d\n", (clc.downloadBlock & 0xFFFF), block);
605 return;
606 }
607
608 // open the file if not opened yet
609 if (!clc.download)
610 {
611 clc.download = FS_BaseDir_FOpenFileWrite_HomeData( clc.downloadTempName );
612
613 if (!clc.download) {
614 Com_Printf( "Could not create %s\n", clc.downloadTempName );
615 CL_AddReliableCommand("stopdl", qfalse);
616 CL_NextDownload();
617 return;
618 }
619 }
620
621 if (size)
622 FS_Write( data, size, clc.download );
623
624 CL_AddReliableCommand(va("nextdl %d", clc.downloadBlock), qfalse);
625 clc.downloadBlock++;
626
627 clc.downloadCount += size;
628
629 // So UI gets access to it
630 Cvar_SetValue( "cl_downloadCount", clc.downloadCount );
631
632 if (!size) { // A zero length block means EOF
633 if (clc.download) {
634 FS_FCloseFile( clc.download );
635 clc.download = 0;
636
637 // rename the file
638 FS_BaseDir_Rename_HomeData ( clc.downloadTempName, clc.downloadName, qfalse );
639 }
640
641 // send intentions now
642 // We need this because without it, we would hold the last nextdl and then start
643 // loading right away. If we take a while to load, the server is happily trying
644 // to send us that last block over and over.
645 // Write it twice to help make sure we acknowledge the download
646 CL_WritePacket();
647 CL_WritePacket();
648
649 // get another file if needed
650 CL_NextDownload ();
651 }
652 }
653
654 #ifdef USE_VOIP
655 static
656 qboolean CL_ShouldIgnoreVoipSender(int sender)
657 {
658 if (!cl_voip->integer)
659 return qtrue; // VoIP is disabled.
660 else if ((sender == clc.clientNum) && (!clc.demoplaying))
661 return qtrue; // ignore own voice (unless playing back a demo).
662 else if (clc.voipMuteAll)
663 return qtrue; // all channels are muted with extreme prejudice.
664 else if (clc.voipIgnore[sender])
665 return qtrue; // just ignoring this guy.
666 else if (clc.voipGain[sender] == 0.0f)
667 return qtrue; // too quiet to play.
668
669 return qfalse;
670 }
671
672 /*
673 =====================
674 CL_PlayVoip
675
676 Play raw data
677 =====================
678 */
679
680 static void CL_PlayVoip(int sender, int samplecnt, const byte *data, int flags)
681 {
682 if(flags & VOIP_DIRECT)
683 {
684 S_RawSamples(sender + 1, samplecnt, 48000, 2, 1,
685 data, clc.voipGain[sender], -1);
686 }
687
688 if(flags & VOIP_SPATIAL)
689 {
690 S_RawSamples(sender + MAX_CLIENTS + 1, samplecnt, 48000, 2, 1,
691 data, 1.0f, sender);
692 }
693 }
694
695 /*
696 =====================
697 CL_ParseVoip
698
699 A VoIP message has been received from the server
700 =====================
701 */
702 static
703 void CL_ParseVoip ( msg_t *msg, qboolean ignoreData ) {
704 static short decoded[VOIP_MAX_PACKET_SAMPLES*4]; // !!! FIXME: don't hard code
705
706 const int sender = MSG_ReadShort(msg);
707 const int generation = MSG_ReadByte(msg);
708 const int sequence = MSG_ReadLong(msg);
709 const int frames = MSG_ReadByte(msg);
710 const int packetsize = MSG_ReadShort(msg);
711 const int flags = MSG_ReadBits(msg, VOIP_FLAGCNT);
712 unsigned char encoded[4000];
713 int numSamples;
714 int seqdiff;
715 int written = 0;
716 int i;
717
718 Com_DPrintf("VoIP: %d-byte packet from client %d\n", packetsize, sender);
719
720 if (sender < 0)
721 return; // short/invalid packet, bail.
722 else if (generation < 0)
723 return; // short/invalid packet, bail.
724 else if (sequence < 0)
725 return; // short/invalid packet, bail.
726 else if (frames < 0)
727 return; // short/invalid packet, bail.
728 else if (packetsize < 0)
729 return; // short/invalid packet, bail.
730
731 if (packetsize > sizeof (encoded)) { // overlarge packet?
732 int bytesleft = packetsize;
733 while (bytesleft) {
734 int br = bytesleft;
735 if (br > sizeof (encoded))
736 br = sizeof (encoded);
737 MSG_ReadData(msg, encoded, br);
738 bytesleft -= br;
739 }
740 return; // overlarge packet, bail.
741 }
742
743 MSG_ReadData(msg, encoded, packetsize);
744
745 if (ignoreData) {
746 return; // just ignore legacy speex voip data
747 } else if (!clc.voipCodecInitialized) {
748 return; // can't handle VoIP without libopus!
749 } else if (sender >= MAX_CLIENTS) {
750 return; // bogus sender.
751 } else if (CL_ShouldIgnoreVoipSender(sender)) {
752 return; // Channel is muted, bail.
753 }
754
755 // !!! FIXME: make sure data is narrowband? Does decoder handle this?
756
757 Com_DPrintf("VoIP: packet accepted!\n");
758
759 seqdiff = sequence - clc.voipIncomingSequence[sender];
760
761 // This is a new "generation" ... a new recording started, reset the bits.
762 if (generation != clc.voipIncomingGeneration[sender]) {
763 Com_DPrintf("VoIP: new generation %d!\n", generation);
764 opus_decoder_ctl(clc.opusDecoder[sender], OPUS_RESET_STATE);
765 clc.voipIncomingGeneration[sender] = generation;
766 seqdiff = 0;
767 } else if (seqdiff < 0) { // we're ahead of the sequence?!
768 // This shouldn't happen unless the packet is corrupted or something.
769 Com_DPrintf("VoIP: misordered sequence! %d < %d!\n",
770 sequence, clc.voipIncomingSequence[sender]);
771 // reset the decoder just in case.
772 opus_decoder_ctl(clc.opusDecoder[sender], OPUS_RESET_STATE);
773 seqdiff = 0;
774 } else if (seqdiff * VOIP_MAX_PACKET_SAMPLES*2 >= sizeof (decoded)) { // dropped more than we can handle?
775 // just start over.
776 Com_DPrintf("VoIP: Dropped way too many (%d) frames from client #%d\n",
777 seqdiff, sender);
778 opus_decoder_ctl(clc.opusDecoder[sender], OPUS_RESET_STATE);
779 seqdiff = 0;
780 }
781
782 if (seqdiff != 0) {
783 Com_DPrintf("VoIP: Dropped %d frames from client #%d\n",
784 seqdiff, sender);
785 // tell opus that we're missing frames...
786 for (i = 0; i < seqdiff; i++) {
787 assert((written + VOIP_MAX_PACKET_SAMPLES) * 2 < sizeof (decoded));
788 numSamples = opus_decode(clc.opusDecoder[sender], NULL, 0, decoded + written, VOIP_MAX_PACKET_SAMPLES, 0);
789 if ( numSamples <= 0 ) {
790 Com_DPrintf("VoIP: Error decoding frame %d from client #%d\n", i, sender);
791 continue;
792 }
793 written += numSamples;
794 }
795 }
796
797 numSamples = opus_decode(clc.opusDecoder[sender], encoded, packetsize, decoded + written, ARRAY_LEN(decoded) - written, 0);
798
799 if ( numSamples <= 0 ) {
800 Com_DPrintf("VoIP: Error decoding voip data from client #%d\n", sender);
801 numSamples = 0;
802 }
803
804 #if 0
805 static FILE *encio = NULL;
806 if (encio == NULL) encio = fopen("voip-incoming-encoded.bin", "wb");
807 if (encio != NULL) { fwrite(encoded, packetsize, 1, encio); fflush(encio); }
808 static FILE *decio = NULL;
809 if (decio == NULL) decio = fopen("voip-incoming-decoded.bin", "wb");
810 if (decio != NULL) { fwrite(decoded+written, numSamples*2, 1, decio); fflush(decio); }
811 #endif
812
813 written += numSamples;
814
815 Com_DPrintf("VoIP: playback %d bytes, %d samples, %d frames\n",
816 written * 2, written, frames);
817
818 if(written > 0)
819 CL_PlayVoip(sender, written, (const byte *) decoded, flags);
820
821 clc.voipIncomingSequence[sender] = sequence + frames;
822 }
823 #endif
824
825
826 /*
827 =====================
828 CL_ParseCommandString
829
830 Command strings are just saved off until cgame asks for them
831 when it transitions a snapshot
832 =====================
833 */
834 void CL_ParseCommandString( msg_t *msg ) {
835 char *s;
836 int seq;
837 int index;
838
839 seq = MSG_ReadLong( msg );
840 s = MSG_ReadString( msg );
841
842 // see if we have already executed stored it off
843 if ( clc.serverCommandSequence >= seq ) {
844 return;
845 }
846 clc.serverCommandSequence = seq;
847
848 index = seq & (MAX_RELIABLE_COMMANDS-1);
849 Q_strncpyz( clc.serverCommands[ index ], s, sizeof( clc.serverCommands[ index ] ) );
850 }
851
852
853 /*
854 =====================
855 CL_ParseServerMessage
856 =====================
857 */
858 void CL_ParseServerMessage( msg_t *msg ) {
859 int cmd;
860
861 if ( cl_shownet->integer == 1 ) {
862 Com_Printf ("%i ",msg->cursize);
863 } else if ( cl_shownet->integer >= 2 ) {
864 Com_Printf ("------------------\n");
865 }
866
867 MSG_Bitstream(msg);
868
869 // get the reliable sequence acknowledge number
870 clc.reliableAcknowledge = MSG_ReadLong( msg );
871 //
872 if ( clc.reliableAcknowledge < clc.reliableSequence - MAX_RELIABLE_COMMANDS ) {
873 clc.reliableAcknowledge = clc.reliableSequence;
874 }
875
876 //
877 // parse the message
878 //
879 while ( 1 ) {
880 if ( msg->readcount > msg->cursize ) {
881 Com_Error (ERR_DROP,"CL_ParseServerMessage: read past end of server message");
882 break;
883 }
884
885 cmd = MSG_ReadByte( msg );
886
887 if (cmd == svc_EOF) {
888 SHOWNET( msg, "END OF MESSAGE" );
889 break;
890 }
891
892 if ( cl_shownet->integer >= 2 ) {
893 if ( (cmd < 0) || (!svc_strings[cmd]) ) {
894 Com_Printf( "%3i:BAD CMD %i\n", msg->readcount-1, cmd );
895 } else {
896 SHOWNET( msg, svc_strings[cmd] );
897 }
898 }
899
900 // other commands
901 switch ( cmd ) {
902 default:
903 Com_Error (ERR_DROP,"CL_ParseServerMessage: Illegible server message");
904 break;
905 case svc_nop:
906 break;
907 case svc_serverCommand:
908 CL_ParseCommandString( msg );
909 break;
910 case svc_gamestate:
911 CL_ParseGamestate( msg );
912 break;
913 case svc_snapshot:
914 CL_ParseSnapshot( msg );
915 break;
916 case svc_download:
917 CL_ParseDownload( msg );
918 break;
919 case svc_voipSpeex:
920 #ifdef USE_VOIP
921 CL_ParseVoip( msg, qtrue );
922 #endif
923 break;
924 case svc_voipOpus:
925 #ifdef USE_VOIP
926 CL_ParseVoip( msg, !clc.voipEnabled );
927 #endif
928 break;
929 }
930 }
931 }
932
933