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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// hltvclient.cpp: implementation of the CHLTVClient class.
//
// $NoKeywords: $
//
//===========================================================================//
#include <tier0/vprof.h>
#include "hltvclient.h"
#include "netmessages.h"
#include "hltvserver.h"
#include "framesnapshot.h"
#include "networkstringtable.h"
#include "dt_send_eng.h"
#include "GameEventManager.h"
#include "cmd.h"
#include "ihltvdirector.h"
#include "host.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
static ConVar tv_maxrate( "tv_maxrate", "8000", 0, "Max SourceTV spectator bandwidth rate allowed, 0 == unlimited" );
static ConVar tv_relaypassword( "tv_relaypassword", "", FCVAR_NOTIFY | FCVAR_PROTECTED | FCVAR_DONTRECORD, "SourceTV password for relay proxies" );
static ConVar tv_chattimelimit( "tv_chattimelimit", "8", 0, "Limits spectators to chat only every n seconds" );
static ConVar tv_chatgroupsize( "tv_chatgroupsize", "0", 0, "Set the default chat group size" );
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CHLTVClient::CHLTVClient(int slot, CBaseServer *pServer)
{
Clear();
Assert( hltv == pServer );
m_nClientSlot = slot;
m_Server = pServer;
m_pHLTV = dynamic_cast<CHLTVServer*>(pServer);
m_nEntityIndex = m_pHLTV->GetHLTVSlot() + 1;
m_nLastSendTick = 0;
m_fLastSendTime = 0.0f;
m_flLastChatTime = 0.0f;
m_bNoChat = false;
if ( tv_chatgroupsize.GetInt() > 0 )
{
Q_snprintf( m_szChatGroup, sizeof(m_szChatGroup), "group%d", slot%tv_chatgroupsize.GetInt() );
}
else
{
Q_strncpy( m_szChatGroup, "all", sizeof(m_szChatGroup) );
}
}
CHLTVClient::~CHLTVClient()
{
}
bool CHLTVClient::SendSignonData( void )
{
// check class table CRCs
if ( m_nSendtableCRC != SendTable_GetCRC() )
{
Disconnect( "Server uses different class tables" );
return false;
}
else
{
// use your class infos, CRC is correct
SVC_ClassInfo classmsg( true, m_Server->serverclasses );
m_NetChannel->SendNetMsg( classmsg );
}
return CBaseClient::SendSignonData();
}
bool CHLTVClient::ProcessClientInfo( CLC_ClientInfo *msg )
{
if ( !CBaseClient::ProcessClientInfo( msg ) )
return false;
return true;
}
bool CHLTVClient::ProcessMove(CLC_Move *msg)
{
// HLTV clients can't move
return true;
}
bool CHLTVClient::ProcessListenEvents( CLC_ListenEvents *msg )
{
// HLTV clients can't subscribe to events, we just send them
return true;
}
bool CHLTVClient::ProcessRespondCvarValue( CLC_RespondCvarValue *msg )
{
return true;
}
bool CHLTVClient::ProcessFileCRCCheck( CLC_FileCRCCheck *msg )
{
return true;
}
bool CHLTVClient::ProcessSaveReplay( CLC_SaveReplay *msg )
{
return true;
}
bool CHLTVClient::ProcessVoiceData(CLC_VoiceData *msg)
{
// HLTV clients can't speak
return true;
}
void CHLTVClient::ConnectionClosing(const char *reason)
{
Disconnect ( (reason!=NULL)?reason:"Connection closing" );
}
void CHLTVClient::ConnectionCrashed(const char *reason)
{
DebuggerBreakIfDebugging_StagingOnly();
Disconnect ( (reason!=NULL)?reason:"Connection lost" );
}
void CHLTVClient::PacketStart(int incoming_sequence, int outgoing_acknowledged)
{
// During connection, only respond if client sends a packet
m_bReceivedPacket = true;
}
void CHLTVClient::PacketEnd()
{
}
void CHLTVClient::FileRequested(const char *fileName, unsigned int transferID )
{
DevMsg( "CHLTVClient::FileRequested: %s.\n", fileName );
m_NetChannel->DenyFile( fileName, transferID );
}
void CHLTVClient::FileDenied(const char *fileName, unsigned int transferID )
{
DevMsg( "CHLTVClient::FileDenied: %s.\n", fileName );
}
void CHLTVClient::FileReceived( const char *fileName, unsigned int transferID )
{
DevMsg( "CHLTVClient::FileReceived: %s.\n", fileName );
}
void CHLTVClient::FileSent(const char *fileName, unsigned int transferID )
{
}
CClientFrame *CHLTVClient::GetDeltaFrame( int nTick )
{
return m_pHLTV->GetDeltaFrame( nTick );
}
bool CHLTVClient::ExecuteStringCommand( const char *pCommandString )
{
// first let the baseclass handle it
if ( CBaseClient::ExecuteStringCommand( pCommandString ) )
return true;
if ( !pCommandString || !pCommandString[0] )
return true;
CCommand args;
if ( !args.Tokenize( pCommandString ) )
return true;
const char *cmd = args[ 0 ];
if ( !Q_stricmp( cmd, "spec_next" ) ||
!Q_stricmp( cmd, "spec_prev" ) ||
!Q_stricmp( cmd, "spec_mode" ) )
{
ClientPrintf("Camera settings can't be changed during a live broadcast.\n");
return true;
}
if ( !Q_stricmp( cmd, "say" ) && args.ArgC() > 1 )
{
// if tv_chattimelimit = 0, chat is turned off
if ( tv_chattimelimit.GetFloat() <= 0 )
return true;
if ( (m_flLastChatTime + tv_chattimelimit.GetFloat()) > net_time )
return true;
m_flLastChatTime = net_time;
char chattext[128];
Q_snprintf( chattext, sizeof(chattext), "%s : %s", GetClientName(), args[1] );
m_pHLTV->BroadcastLocalChat( chattext, m_szChatGroup );
return true;
}
else if ( !Q_strcmp( cmd, "tv_chatgroup" ) )
{
if ( args.ArgC() > 1 )
{
Q_strncpy( m_szChatGroup, args[1], sizeof(m_szChatGroup) );
}
else
{
ClientPrintf("Your current chat group is \"%s\"\n", m_szChatGroup );
}
return true;
}
else if ( !Q_strcmp( cmd, "status" ) )
{
int slots, proxies, clients;
char gd[MAX_OSPATH];
Q_FileBase( com_gamedir, gd, sizeof( gd ) );
if ( m_pHLTV->IsMasterProxy() )
{
ClientPrintf("SourceTV Master \"%s\", delay %.0f\n",
m_pHLTV->GetName(), m_pHLTV->GetDirector()->GetDelay() );
}
else // if ( m_Server->IsRelayProxy() )
{
if ( m_pHLTV->GetRelayAddress() )
{
ClientPrintf("SourceTV Relay \"%s\", connected.\n",
m_pHLTV->GetName() );
}
else
{
ClientPrintf("SourceTV Relay \"%s\", not connect.\n", m_pHLTV->GetName() );
}
}
ClientPrintf("IP %s:%i, Online %s, Version %i (%s)\n",
net_local_adr.ToString( true ), m_pHLTV->GetUDPPort(),
COM_FormatSeconds( m_pHLTV->GetOnlineTime() ), build_number(),
#ifdef _WIN32
"Win32" );
#else
"Linux" );
#endif
ClientPrintf("Game Time %s, Mod \"%s\", Map \"%s\", Players %i\n", COM_FormatSeconds( m_pHLTV->GetTime() ),
gd, m_pHLTV->GetMapName(), m_pHLTV->GetNumPlayers() );
m_pHLTV->GetLocalStats( proxies, slots, clients );
ClientPrintf("Local Slots %i, Spectators %i, Proxies %i\n",
slots, clients-proxies, proxies );
m_pHLTV->GetGlobalStats( proxies, slots, clients);
ClientPrintf("Total Slots %i, Spectators %i, Proxies %i\n",
slots, clients-proxies, proxies);
}
else
{
DevMsg( "CHLTVClient::ExecuteStringCommand: Unknown command %s.\n", pCommandString );
}
return true;
}
bool CHLTVClient::ShouldSendMessages( void )
{
if ( !IsActive() )
{
// during signon behave like normal client
return CBaseClient::ShouldSendMessages();
}
// HLTV clients use snapshot rate used by HLTV server, not given by HLTV client
// if the reliable message overflowed, drop the client
if ( m_NetChannel->IsOverflowed() )
{
m_NetChannel->Reset();
Disconnect ("%s overflowed reliable buffer\n", m_Name );
return false;
}
// send a packet if server has a new tick we didn't already send
bool bSendMessage = ( m_nLastSendTick != m_Server->m_nTickCount );
// send a packet at least every 2 seconds
if ( !bSendMessage && (m_fLastSendTime + 2.0f) < net_time )
{
bSendMessage = true; // force sending a message even if server didn't update
}
if ( bSendMessage && !m_NetChannel->CanPacket() )
{
// we would like to send a message, but bandwidth isn't available yet
// in HLTV we don't send choke information, doesn't matter
bSendMessage = false;
}
return bSendMessage;
}
void CHLTVClient::SpawnPlayer( void )
{
// set view entity
SVC_SetView setView( m_pHLTV->m_nViewEntity );
SendNetMsg( setView );
m_pHLTV->BroadcastLocalTitle( this );
m_flLastChatTime = net_time;
CBaseClient::SpawnPlayer();
}
void CHLTVClient::SetRate(int nRate, bool bForce )
{
if ( !bForce )
{
if ( m_bIsHLTV )
{
// allow higher bandwidth rates for HLTV proxies
nRate = clamp( nRate, MIN_RATE, MAX_RATE );
}
else if ( tv_maxrate.GetInt() > 0 )
{
// restrict rate for normal clients to hltv_maxrate
nRate = clamp( nRate, MIN_RATE, tv_maxrate.GetInt() );
}
}
CBaseClient::SetRate( nRate, bForce );
}
void CHLTVClient::SetUpdateRate(int udpaterate, bool bForce)
{
// for HLTV clients ignore update rate settings, speed is tv_snapshotrate
m_fSnapshotInterval = 1.0f / 100.0f;
}
bool CHLTVClient::ProcessSetConVar(NET_SetConVar *msg)
{
if ( !CBaseClient::ProcessSetConVar( msg ) )
return false;
// if this is the first time we get user settings, check password etc
if ( m_nSignonState == SIGNONSTATE_CONNECTED )
{
const char *checkpwd = NULL;
m_bIsHLTV = m_ConVars->GetInt( "tv_relay", 0 ) != 0;
if ( m_bIsHLTV )
{
// if the connecting client is a TV relay, check the password
checkpwd = tv_relaypassword.GetString();
if ( checkpwd && checkpwd[0] && Q_stricmp( checkpwd, "none") )
{
if ( Q_stricmp( m_szPassword, checkpwd ) )
{
Disconnect("Bad relay password");
return false;
}
}
}
else
{
// if client is a normal spectator, check if we can to forward him to other relays
if ( m_pHLTV->DispatchToRelay( this ) )
{
return false;
}
// if client stays here, check the normal password
checkpwd = m_pHLTV->GetPassword();
if ( checkpwd )
{
if ( Q_stricmp( m_szPassword, checkpwd ) )
{
Disconnect("Bad spectator password");
return false;
}
}
// check if server is LAN only
if ( !m_pHLTV->CheckIPRestrictions( m_NetChannel->GetRemoteAddress(), PROTOCOL_HASHEDCDKEY ) )
{
Disconnect( "SourceTV server is restricted to local spectators (class C).\n" );
return false;
}
}
}
return true;
}
void CHLTVClient::UpdateUserSettings()
{
// set voice loopback
m_bNoChat = m_ConVars->GetInt( "tv_nochat", 0 ) != 0;
CBaseClient::UpdateUserSettings();
}
void CHLTVClient::SendSnapshot( CClientFrame * pFrame )
{
VPROF_BUDGET( "CHLTVClient::SendSnapshot", "HLTV" );
ALIGN4 byte buf[NET_MAX_PAYLOAD] ALIGN4_POST;
bf_write msg( "CHLTVClient::SendSnapshot", buf, sizeof(buf) );
// if we send a full snapshot (no delta-compression) before, wait until client
// received and acknowledge that update. don't spam client with full updates
if ( m_pLastSnapshot == pFrame->GetSnapshot() )
{
// never send the same snapshot twice
m_NetChannel->Transmit();
return;
}
if ( m_nForceWaitForTick > 0 )
{
// just continue transmitting reliable data
Assert( !m_bFakePlayer ); // Should never happen
m_NetChannel->Transmit();
return;
}
CClientFrame *pDeltaFrame = GetDeltaFrame( m_nDeltaTick ); // NULL if delta_tick is not found
CHLTVFrame *pLastFrame = (CHLTVFrame*) GetDeltaFrame( m_nLastSendTick );
if ( pLastFrame )
{
// start first frame after last send
pLastFrame = (CHLTVFrame*) pLastFrame->m_pNext;
}
// add all reliable messages between ]lastframe,currentframe]
// add all tempent & sound messages between ]lastframe,currentframe]
while ( pLastFrame && pLastFrame->tick_count <= pFrame->tick_count )
{
m_NetChannel->SendData( pLastFrame->m_Messages[HLTV_BUFFER_RELIABLE], true );
if ( pDeltaFrame )
{
// if we send entities delta compressed, also send unreliable data
m_NetChannel->SendData( pLastFrame->m_Messages[HLTV_BUFFER_UNRELIABLE], false );
}
pLastFrame = (CHLTVFrame*) pLastFrame->m_pNext;
}
// now create client snapshot packet
// send tick time
NET_Tick tickmsg( pFrame->tick_count, host_frametime_unbounded, host_frametime_stddeviation );
tickmsg.WriteToBuffer( msg );
// Update shared client/server string tables. Must be done before sending entities
m_Server->m_StringTables->WriteUpdateMessage( NULL, GetMaxAckTickCount(), msg );
// TODO delta cache whole snapshots, not just packet entities. then use net_Align
// send entity update, delta compressed if deltaFrame != NULL
m_Server->WriteDeltaEntities( this, pFrame, pDeltaFrame, msg );
// write message to packet and check for overflow
if ( msg.IsOverflowed() )
{
if ( !pDeltaFrame )
{
// if this is a reliable snapshot, drop the client
Disconnect( "ERROR! Reliable snapshot overflow." );
return;
}
else
{
// unreliable snapshots may be dropped
ConMsg ("WARNING: msg overflowed for %s\n", m_Name);
msg.Reset();
}
}
// remember this snapshot
m_pLastSnapshot = pFrame->GetSnapshot();
m_nLastSendTick = pFrame->tick_count;
// Don't send the datagram to fakeplayers
if ( m_bFakePlayer )
{
m_nDeltaTick = pFrame->tick_count;
return;
}
bool bSendOK;
// is this is a full entity update (no delta) ?
if ( !pDeltaFrame )
{
// transmit snapshot as reliable data chunk
bSendOK = m_NetChannel->SendData( msg );
bSendOK = bSendOK && m_NetChannel->Transmit();
// remember this tickcount we send the reliable snapshot
// so we can continue sending other updates if this has been acknowledged
m_nForceWaitForTick = pFrame->tick_count;
}
else
{
// just send it as unreliable snapshot
bSendOK = m_NetChannel->SendDatagram( &msg ) > 0;
}
if ( !bSendOK )
{
Disconnect( "ERROR! Couldn't send snapshot." );
}
}
|