-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathsv_snapshot.cpp
More file actions
1152 lines (928 loc) · 28 KB
/
sv_snapshot.cpp
File metadata and controls
1152 lines (928 loc) · 28 KB
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
===========================================================================
Daemon GPL Source Code
Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company.
This file is part of the Daemon GPL Source Code (Daemon Source Code).
Daemon Source Code 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 3 of the License, or
(at your option) any later version.
Daemon Source Code 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 Daemon Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Daemon Source Code is also subject to certain additional terms.
You should have received a copy of these additional terms immediately following the
terms and conditions of the GNU General Public License which accompanied the Daemon
Source Code. If not, please request a copy in writing from id Software at the address
below.
If you have questions concerning this license or the applicable additional terms, you
may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville,
Maryland 20850 USA.
===========================================================================
*/
#include "server.h"
#include "qcommon/sys.h"
/*
=============================================================================
Delta encode a client frame onto the network channel
A server packet will look something like:
4 sequence number (high bit set if an oversize fragment)
<optional reliable commands>
1 svc_snapshot
4 last client reliable command
4 serverTime
1 lastframe for delta compression
1 snapFlags
1 areaBytes
<areabytes>
<playerstate>
<packetentities>
=============================================================================
*/
static Cvar::Cvar<bool> sv_novis("sv_novis", "skip PVS check when transmitting entities", 0, false);
static Log::Logger bandwidthLog("server.bandwidth");
/*
=============
SV_EmitPacketEntities
Writes a delta update of an entityState_t list to the message.
=============
*/
static void SV_EmitPacketEntities( const clientSnapshot_t *from, clientSnapshot_t *to, msg_t *msg,
client_t* client, int lastframe, int snapFlags )
{
entityState_t *oldent, *newent;
int oldindex, newindex;
int oldnum, newnum;
int from_num_entities;
MSG_WriteShort(msg, to->num_entities);
// generate the delta update
if ( !from )
{
static const clientSnapshot_t nullfrom{};
from = &nullfrom;
from_num_entities = 0;
}
else
{
from_num_entities = from->num_entities;
}
newent = nullptr;
oldent = nullptr;
newindex = 0;
oldindex = 0;
while ( newindex < to->num_entities || oldindex < from_num_entities )
{
if ( MAX_MSGLEN - msg->cursize < 128 ) {
MSG_WriteBits( msg, 0, GENTITYNUM_BITS );
MSG_WriteBits( msg, 1, 1 );
MSG_WriteBits( msg, 1, 1 );
SV_SendMessageToClient( msg, client );
MSG_Init( msg, msg->data, msg->maxsize );
MSG_WriteByte( msg, svc_snapshot );
MSG_WriteLong( msg, sv.time );
// what we are delta'ing from
MSG_WriteByte( msg, lastframe );
snapFlags = svs.snapFlagServerBit;
MSG_WriteByte( msg, snapFlags );
// send over the areabits
MSG_WriteByte( msg, to->areabytes );
MSG_WriteData( msg, to->areabits, to->areabytes );
{
// delta encode the playerstate
if ( from ) {
MSG_WriteDeltaPlayerstate( msg, &from->ps, &to->ps );
} else {
MSG_WriteDeltaPlayerstate( msg, nullptr, &to->ps );
}
}
MSG_WriteShort( msg, to->num_entities - newindex );
}
if ( newindex >= to->num_entities )
{
newnum = MAX_GENTITIES;
}
else
{
newent = &svs.snapshotEntities[( to->first_entity + newindex ) % svs.numSnapshotEntities ];
newnum = newent->number;
}
if ( oldindex >= from_num_entities )
{
oldnum = MAX_GENTITIES;
}
else
{
oldent = &svs.snapshotEntities[( from->first_entity + oldindex ) % svs.numSnapshotEntities ];
oldnum = oldent->number;
}
if ( newnum == oldnum )
{
// delta update from old position
// because the force parm is false, this will not result
// in any bytes being emitted if the entity has not changed at all
MSG_WriteDeltaEntity( msg, oldent, newent, false );
oldindex++;
newindex++;
continue;
}
if ( newnum < oldnum )
{
// this is a new entity, send it from the baseline
MSG_WriteDeltaEntity( msg, &sv.svEntities[ newnum ].baseline, newent, true );
newindex++;
continue;
}
if ( newnum > oldnum )
{
// the old entity isn't present in the new message
MSG_WriteDeltaEntity( msg, oldent, nullptr, true );
oldindex++;
continue;
}
}
MSG_WriteBits( msg, ( MAX_GENTITIES - 1 ), GENTITYNUM_BITS ); // end of packetentities
}
/*
==================
SV_WriteSnapshotToClient
==================
*/
static void SV_WriteSnapshotToClient( client_t *client, msg_t *msg )
{
clientSnapshot_t *frame, *oldframe;
int lastframe;
int i;
int snapFlags;
// this is the snapshot we are creating
frame = &client->frames[ client->netchan.outgoingSequence & PACKET_MASK ];
// try to use a previous frame as the source for delta compressing the snapshot
if ( client->deltaMessage <= 0 || client->state != clientState_t::CS_ACTIVE )
{
// client is asking for a retransmit
oldframe = nullptr;
lastframe = 0;
}
else if ( client->netchan.outgoingSequence - client->deltaMessage >= ( PACKET_BACKUP - 3 ) )
{
// client hasn't gotten a good message through in a long time
Log::Debug( "%s^*: Delta request from out of date packet.", client->name );
oldframe = nullptr;
lastframe = 0;
}
else
{
// we have a valid snapshot to delta from
oldframe = &client->frames[ client->deltaMessage & PACKET_MASK ];
lastframe = client->netchan.outgoingSequence - client->deltaMessage;
// the snapshot's entities may still have rolled off the buffer, though
if ( oldframe->first_entity <= svs.nextSnapshotEntities - svs.numSnapshotEntities )
{
Log::Debug( "%s^*: Delta request from out of date entities.", client->name );
oldframe = nullptr;
lastframe = 0;
}
}
MSG_WriteByte( msg, svc_snapshot );
// NOTE, MRE: now sent at the start of every message from server to client
// let the client know which reliable clientCommands we have received
//MSG_WriteLong( msg, client->lastClientCommand );
// send over the current server time so the client can drift
// its view of time to try to match
MSG_WriteLong( msg, sv.time );
// what we are delta'ing from
MSG_WriteByte( msg, lastframe );
snapFlags = svs.snapFlagServerBit;
if ( client->rateDelayed )
{
snapFlags |= SNAPFLAG_RATE_DELAYED;
}
if ( client->state != clientState_t::CS_ACTIVE )
{
snapFlags |= SNAPFLAG_NOT_ACTIVE;
}
MSG_WriteByte( msg, snapFlags );
// send over the areabits
MSG_WriteByte( msg, frame->areabytes );
MSG_WriteData( msg, frame->areabits, frame->areabytes );
{
// delta encode the playerstate
if ( oldframe )
{
MSG_WriteDeltaPlayerstate( msg, &oldframe->ps, &frame->ps );
}
else
{
MSG_WriteDeltaPlayerstate( msg, nullptr, &frame->ps );
}
}
// delta encode the entities
SV_EmitPacketEntities( oldframe, frame, msg, client, lastframe, snapFlags );
// padding for rate debugging
if ( sv_padPackets.Get() )
{
for ( i = 0; i < sv_padPackets.Get(); i++ )
{
MSG_WriteByte( msg, svc_nop );
}
}
}
/*
==================
SV_UpdateServerCommandsToClient
(re)send all server commands the client hasn't acknowledged yet
==================
*/
void SV_UpdateServerCommandsToClient( client_t *client, msg_t *msg )
{
int i;
// write any unacknowledged serverCommands
for ( i = client->reliableAcknowledge + 1; i <= client->reliableSequence; i++ )
{
MSG_WriteByte( msg, svc_serverCommand );
MSG_WriteLong( msg, i );
MSG_WriteString( msg, client->reliableCommands[ i & ( MAX_RELIABLE_COMMANDS - 1 ) ] );
if ( MAX_MSGLEN - msg->cursize < MAX_STRING_CHARS ) {
MSG_WriteByte( msg, svc_partial );
SV_SendMessageToClient( msg, client );
MSG_Init( msg, msg->data, msg->maxsize );
MSG_WriteLong( msg, client->lastClientCommand );
}
}
client->reliableSent = client->reliableSequence;
}
/*
=============================================================================
Build a client snapshot structure
=============================================================================
*/
//#define MAX_SNAPSHOT_ENTITIES 1024
static const int MAX_SNAPSHOT_ENTITIES = 2048;
struct snapshotEntityNumbers_t
{
int numSnapshotEntities;
int snapshotEntities[ MAX_SNAPSHOT_ENTITIES ];
};
/*
=======================
SV_QsortEntityNumbers
=======================
*/
static int SV_QsortEntityNumbers( const void *a, const void *b )
{
int *ea, *eb;
ea = ( int * ) a;
eb = ( int * ) b;
if ( *ea == *eb )
{
Sys::Drop( "SV_QsortEntityNumbers: duplicated entity" );
}
if ( *ea < *eb )
{
return -1;
}
return 1;
}
/*
===============
SV_AddEntToSnapshot
===============
*/
static void SV_AddEntToSnapshot( svEntity_t *svEnt, sharedEntity_t *gEnt,
snapshotEntityNumbers_t *eNums )
{
// if we have already added this entity to this snapshot, don't add again
if ( svEnt->snapshotCounter == sv.snapshotCounter )
{
return;
}
svEnt->snapshotCounter = sv.snapshotCounter;
// if we are full, silently discard entities
if ( eNums->numSnapshotEntities == MAX_SNAPSHOT_ENTITIES )
{
return;
}
eNums->snapshotEntities[ eNums->numSnapshotEntities ] = gEnt->s.number;
eNums->numSnapshotEntities++;
}
/*
===============
SV_AddEntitiesVisibleFromPoint
===============
*/
static void SV_AddEntitiesVisibleFromPoint( client_t* client, vec3_t origin, clientSnapshot_t *frame,
// snapshotEntityNumbers_t *eNums, bool portal, clientSnapshot_t *oldframe, bool localClient ) {
// snapshotEntityNumbers_t *eNums, bool portal ) {
snapshotEntityNumbers_t *eNums /*, bool portal, bool localClient */ )
{
int e, i;
sharedEntity_t *ent, *playerEnt;
svEntity_t *svEnt;
int l;
int clientarea, clientcluster;
int leafnum;
// int c_fullsend;
byte *clientpvs;
byte *bitvector;
// during an error shutdown message we may need to transmit
// the shutdown message after the server has shutdown, so
// specifically check for it
if (sv.state == serverState_t::SS_DEAD)
{
return;
}
leafnum = CM_PointLeafnum( origin );
clientarea = CM_LeafArea( leafnum );
clientcluster = CM_LeafCluster( leafnum );
// calculate the visible areas
frame->areabytes = CM_WriteAreaBits( frame->areabits, clientarea );
clientpvs = CM_ClusterPVS( clientcluster );
// c_fullsend = 0;
playerEnt = SV_GentityNum( frame->ps.clientNum );
if ( playerEnt->r.svFlags & SVF_SELF_PORTAL )
{
SV_AddEntitiesVisibleFromPoint( client, playerEnt->s.origin2, frame, eNums );
}
for ( e = 0; e < sv.num_entities; e++ )
{
ent = SV_GentityNum( e );
// never send entities that aren't linked in
if ( !ent->r.linked )
{
continue;
}
if ( ent->s.number != e )
{
Log::Debug( "FIXING ENT->S.NUMBER!!!" );
ent->s.number = e;
}
// entities can be flagged to explicitly not be sent to the client
if ( ent->r.svFlags & SVF_NOCLIENT )
{
continue;
}
// entities can be flagged to be sent to only one client
if ( ent->r.svFlags & SVF_SINGLECLIENT )
{
if ( ent->r.singleClient != frame->ps.clientNum )
{
continue;
}
}
// entities can be flagged to be sent to everyone but one client
if ( ent->r.svFlags & SVF_NOTSINGLECLIENT )
{
if ( ent->r.singleClient == frame->ps.clientNum )
{
continue;
}
}
// entities can be flagged to be sent to only a given mask of clients
if ( ent->r.svFlags & SVF_CLIENTMASK )
{
if ( frame->ps.clientNum >= 32 )
{
if ( ~ent->r.hiMask & ( 1 << ( frame->ps.clientNum - 32 ) ) )
{
continue;
}
}
else
{
if ( ~ent->r.loMask & ( 1 << frame->ps.clientNum ) )
{
continue;
}
}
}
svEnt = SV_SvEntityForGentity( ent );
// don't double add an entity through portals
if ( svEnt->snapshotCounter == sv.snapshotCounter )
{
continue;
}
if ( sv_novis.Get() )
{
SV_AddEntToSnapshot( svEnt, ent, eNums );
continue;
}
// broadcast entities are always sent
if ( ent->r.svFlags & SVF_BROADCAST )
{
SV_AddEntToSnapshot( svEnt, ent, eNums );
continue;
}
if ( ( ent->r.svFlags & SVF_BROADCAST_ONCE ) && !client->reliableAcknowledge ) {
SV_AddEntToSnapshot( svEnt, ent, eNums );
continue;
}
// send entity if the client is in range
if ( (ent->r.svFlags & SVF_CLIENTS_IN_RANGE) &&
Distance( ent->s.origin, playerEnt->s.origin ) <= ent->r.clientRadius )
{
SV_AddEntToSnapshot( svEnt, ent, eNums );
continue;
}
bitvector = clientpvs;
// Gordon: just check origin for being in pvs, ignore bmodel extents
if ( ent->r.svFlags & SVF_IGNOREBMODELEXTENTS )
{
if ( bitvector[ ent->r.originCluster >> 3 ] & ( 1 << ( ent->r.originCluster & 7 ) ) )
{
SV_AddEntToSnapshot( svEnt, ent, eNums );
}
continue;
}
// ignore if not touching a PV leaf
// check area
if ( !CM_AreasConnected( clientarea, ent->r.areanum ) )
{
// doors can legally straddle two areas, so
// we may need to check another one
if ( !CM_AreasConnected( clientarea, ent->r.areanum2 ) )
{
continue;
}
}
// check individual leafs
if ( !ent->r.numClusters )
{
continue;
}
l = 0;
for ( i = 0; i < std::min(std::max(0, ent->r.numClusters), MAX_ENT_CLUSTERS); i++ )
{
l = ent->r.clusternums[ i ];
if ( bitvector[ l >> 3 ] & ( 1 << ( l & 7 ) ) )
{
break;
}
}
// if we haven't found it to be visible,
// check the overflow clusters that couldn't be stored
if ( i == ent->r.numClusters )
{
if ( ent->r.lastCluster )
{
for ( ; l <= ent->r.lastCluster; l++ )
{
if ( bitvector[ l >> 3 ] & ( 1 << ( l & 7 ) ) )
{
break;
}
}
if ( l == ent->r.lastCluster )
{
continue;
}
}
else
{
continue;
}
}
//----(SA) added "visibility dummies"
if ( ent->r.svFlags & SVF_VISDUMMY )
{
sharedEntity_t *ment = nullptr;
//find master;
ment = SV_GentityNum( ent->s.otherEntityNum );
if ( ment )
{
svEntity_t *master = nullptr;
master = SV_SvEntityForGentity( ment );
if ( master->snapshotCounter == sv.snapshotCounter || !ment->r.linked )
{
continue;
}
SV_AddEntToSnapshot( master, ment, eNums );
}
continue; // master needs to be added, but not this dummy ent
}
//----(SA) end
else if ( ent->r.svFlags & SVF_VISDUMMY_MULTIPLE )
{
{
int h;
sharedEntity_t *ment = nullptr;
svEntity_t *master = nullptr;
for ( h = 0; h < sv.num_entities; h++ )
{
ment = SV_GentityNum( h );
if ( ment == ent )
{
continue;
}
if ( ment )
{
master = SV_SvEntityForGentity( ment );
}
else
{
continue;
}
if ( !( ment->r.linked ) )
{
continue;
}
if ( ment->s.number != h )
{
Log::Debug( "FIXING vis dummy multiple ment->S.NUMBER!!!" );
ment->s.number = h;
}
if ( ment->r.svFlags & SVF_NOCLIENT )
{
continue;
}
if ( master->snapshotCounter == sv.snapshotCounter )
{
continue;
}
if ( ment->s.otherEntityNum == ent->s.number )
{
SV_AddEntToSnapshot( master, ment, eNums );
}
}
continue;
}
}
// add it
SV_AddEntToSnapshot( svEnt, ent, eNums );
// if it's a portal entity, add everything visible from its camera position
if ( ent->r.svFlags & SVF_PORTAL )
{
if ( ent->s.generic1 )
{
vec3_t dir;
VectorSubtract( ent->s.origin, origin, dir );
if ( VectorLengthSquared( dir ) > ( float ) ent->s.generic1 * ent->s.generic1 )
{
continue;
}
}
// SV_AddEntitiesVisibleFromPoint( ent->s.origin2, frame, eNums, true, oldframe, localClient );
SV_AddEntitiesVisibleFromPoint( client, ent->s.origin2, frame, eNums /*, true, localClient */ );
}
continue;
}
}
/*
=============
SV_BuildClientSnapshot
Decides which entities are going to be visible to the client, and
copies off the playerstate and areabits.
This properly handles multiple recursive portals, but the render
currently doesn't.
For viewing through other player's eyes, clent can be something other than client->gentity
=============
*/
static void SV_BuildClientSnapshot( client_t *client )
{
vec3_t org;
clientSnapshot_t *frame;
snapshotEntityNumbers_t entityNumbers;
int i;
sharedEntity_t *ent;
entityState_t *state;
svEntity_t *svEnt;
sharedEntity_t *clent;
int clientNum;
// bump the counter used to prevent double adding
sv.snapshotCounter++;
// this is the frame we are creating
frame = &client->frames[ client->netchan.outgoingSequence & PACKET_MASK ];
// clear everything in this snapshot
entityNumbers.numSnapshotEntities = 0;
memset( frame->areabits, 0, sizeof( frame->areabits ) );
// show_bug.cgi?id=62
frame->num_entities = 0;
clent = client->gentity;
if ( !clent || client->state == clientState_t::CS_ZOMBIE )
{
return;
}
// grab the current playerState_t
const OpaquePlayerState* ps = SV_GameClientNum( client - svs.clients );
memcpy(&frame->ps, ps, sizeof(frame->ps));
// never send client's own entity, because it can
// be regenerated from the playerstate
clientNum = frame->ps.clientNum;
if ( clientNum < 0 || clientNum >= MAX_GENTITIES )
{
Sys::Drop( "SV_SvEntityForGentity: bad gEnt" );
}
svEnt = &sv.svEntities[ clientNum ];
svEnt->snapshotCounter = sv.snapshotCounter;
if ( clent->r.svFlags & SVF_SELF_PORTAL_EXCLUSIVE )
{
// find the client's viewpoint
VectorCopy( clent->s.origin2, org );
}
else
{
VectorCopy( ps->origin, org );
}
org[ 2 ] += ps->viewheight;
// add all the entities directly visible to the eye, which
// may include portal entities that merge other viewpoints
SV_AddEntitiesVisibleFromPoint( client, org, frame, &entityNumbers /*, false, client->netchan.remoteAddress.type == NA_LOOPBACK */ );
// if there were portals visible, there may be out of order entities
// in the list which will need to be resorted for the delta compression
// to work correctly. This also catches the error condition
// of an entity being included twice.
qsort( entityNumbers.snapshotEntities, entityNumbers.numSnapshotEntities,
sizeof( entityNumbers.snapshotEntities[ 0 ] ), SV_QsortEntityNumbers );
// now that all viewpoint's areabits have been OR'd together, invert
// all of them to make it a mask vector, which is what the renderer wants
for ( i = 0; i < MAX_MAP_AREA_BYTES / 4; i++ )
{
( ( int * ) frame->areabits ) [ i ] = ( ( int * ) frame->areabits ) [ i ] ^ -1;
}
// copy the entity states out
frame->num_entities = 0;
frame->first_entity = svs.nextSnapshotEntities;
for ( i = 0; i < entityNumbers.numSnapshotEntities; i++ )
{
ent = SV_GentityNum( entityNumbers.snapshotEntities[ i ] );
state = &svs.snapshotEntities[ svs.nextSnapshotEntities % svs.numSnapshotEntities ];
*state = ent->s;
svs.nextSnapshotEntities++;
// this should never hit, map should always be restarted first in SV_Frame
if ( svs.nextSnapshotEntities >= 0x7FFFFFFE )
{
Sys::Error( "svs.nextSnapshotEntities wrapped" );
}
frame->num_entities++;
}
}
/*
====================
SV_RateMsec
Return the number of msec a given size message is supposed
to take to clear, based on the current rate
TTimo - use sv_maxRate or sv_dl_maxRate depending on regular or downloading client
====================
*/
static const int HEADER_RATE_BYTES = 48; // include our header, IP header, and some overhead
static int SV_RateMsec( client_t *client, int messageSize )
{
int rate;
int rateMsec;
int maxRate;
// individual messages will never be larger than fragment size
if ( messageSize > 1500 )
{
messageSize = 1500;
}
// low watermark for sv_maxRate, never 0 < sv_maxRate < 1000 (0 is no limitation)
if ( sv_maxRate.Get() > 0 && sv_maxRate.Get() < NETWORK_MIN_RATE )
{
Log::Warn( "sv_maxRate too low, increasing to %d", NETWORK_MIN_RATE );
sv_maxRate.Set( NETWORK_MIN_RATE );
}
rate = client->rate;
// work on the appropriate max rate (client or download)
if ( !*client->downloadName )
{
maxRate = sv_maxRate.Get();
}
else
{
maxRate = sv_dl_maxRate.Get();
}
if ( maxRate > 0 )
{
rate = std::min( rate, maxRate );
}
rateMsec = ( messageSize + HEADER_RATE_BYTES ) * 1000 / rate;
return rateMsec;
}
/*
=======================
SV_SendMessageToClient
Called by SV_SendClientSnapshot and SV_SendClientGameState
=======================
*/
void SV_SendMessageToClient( msg_t *msg, client_t *client )
{
int rateMsec;
// record information about the message
client->frames[ client->netchan.outgoingSequence & PACKET_MASK ].messageSize = msg->cursize;
client->frames[ client->netchan.outgoingSequence & PACKET_MASK ].messageSent = svs.time;
client->frames[ client->netchan.outgoingSequence & PACKET_MASK ].messageAcked = -1;
// send the datagram
SV_Netchan_Transmit( client, msg );
// set nextSnapshotTime based on rate and requested number of updates
// local clients get snapshots every frame
// TTimo - show_bug.cgi?id=491
// added sv_lanForceRate check
if ( client->netchan.remoteAddress.type == netadrtype_t::NA_LOOPBACK ||
( sv_lanForceRate.Get() && Sys_IsLANAddress( client->netchan.remoteAddress ) ) )
{
client->nextSnapshotTime = svs.time - 1;
return;
}
rateMsec = SV_RateMsec( client, msg->cursize );
// TTimo - during a download, ignore the snapshotMsec
// the update server on steroids, with this disabled and sv_fps 60, the download can reach 30 kb/s
// on a regular server, we will still top at 20 kb/s because of sv_fps 20
if ( !*client->downloadName && rateMsec < client->snapshotMsec )
{
// never send more packets than this, no matter what the rate is at
rateMsec = client->snapshotMsec;
client->rateDelayed = false;
}
else
{
client->rateDelayed = true;
}
client->nextSnapshotTime = svs.time + rateMsec;
// don't pile up empty snapshots while connecting
if ( client->state != clientState_t::CS_ACTIVE )
{
// a gigantic connection message may have already put the nextSnapshotTime
// more than a second away, so don't shorten it
// do shorten if client is downloading
if ( !*client->downloadName && client->nextSnapshotTime < svs.time + 1000 )
{
client->nextSnapshotTime = svs.time + 1000;
}
}
}
//bani
/*
=======================
SV_SendClientIdle
There is no need to send full snapshots to clients who are loading a map.
So we send them "idle" packets with the bare minimum required to keep them on the server.
=======================
*/
void SV_SendClientIdle( client_t *client )
{
byte msg_buf[ MAX_MSGLEN ];
msg_t msg;
MSG_Init( &msg, msg_buf, sizeof( msg_buf ) );
// NOTE, MRE: all server->client messages now acknowledge
// let the client know which reliable clientCommands we have received
MSG_WriteLong( &msg, client->lastClientCommand );
// (re)send any reliable server commands
SV_UpdateServerCommandsToClient( client, &msg );
// send over all the relevant entityState_t
// and the playerState_t
// SV_WriteSnapshotToClient( client, &msg );
// Add any download data if the client is downloading
SV_WriteDownloadToClient( client, &msg );
// check for overflow
if ( msg.overflowed )
{
Log::Warn( "msg overflowed for %s", client->name );
MSG_Clear( &msg );
SV_DropClient( client, "Msg overflowed" );
return;
}
SV_SendMessageToClient( &msg, client );
sv.bpsTotalBytes += msg.cursize; // NERVE - SMF - net debugging
sv.ubpsTotalBytes += msg.uncompsize / 8; // NERVE - SMF - net debugging
}
/*
=======================
SV_SendClientSnapshot
Also called by SV_FinalCommand
=======================
*/
void SV_SendClientSnapshot( client_t *client )
{
byte msg_buf[ MAX_MSGLEN ];
msg_t msg;
//bani
if ( client->state < clientState_t::CS_ACTIVE )
{
// bani - #760 - zombie clients need full snaps so they can still process reliable commands
// (eg so they can pick up the disconnect reason)
if ( client->state != clientState_t::CS_ZOMBIE )
{
SV_SendClientIdle( client );
return;
}
}
// build the snapshot
SV_BuildClientSnapshot( client );