-
Notifications
You must be signed in to change notification settings - Fork 270
/
Copy pathgcclient_sharedobjectcache.cpp
586 lines (497 loc) · 18.4 KB
/
gcclient_sharedobjectcache.cpp
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Extra functionality on top of CGCClientSharedObjectCache for GCClients
//
//=============================================================================
#include "stdafx.h"
#include <time.h>
#include "gcsdk/gcclient_sharedobjectcache.h"
#include "gcsdk_gcmessages.pb.h"
#include <typeinfo>
namespace GCSDK
{
//#define SOCDebug(...) Msg( __VA_ARGS__ )
#define SOCDebug(...) ((void)0)
//----------------------------------------------------------------------------
// Purpose: constructor
//----------------------------------------------------------------------------
CGCClientSharedObjectContext::CGCClientSharedObjectContext( const CSteamID & steamIDOwner )
: m_steamIDOwner( steamIDOwner )
{
}
//----------------------------------------------------------------------------
// Purpose: Adds a new Listener to the cache. All objects in the cache will
// be sent as create messages to the new Listener
//----------------------------------------------------------------------------
bool CGCClientSharedObjectContext::BAddListener( ISharedObjectListener *pListener )
{
if( m_vecListeners.HasElement( pListener ) )
return false;
m_vecListeners.AddToTail( pListener );
return true;
}
//----------------------------------------------------------------------------
// Purpose: Removes a Listener from the cache. All objects in the cache
// will have destroy messages sent for them to the new Listener.
//----------------------------------------------------------------------------
bool CGCClientSharedObjectContext::BRemoveListener( ISharedObjectListener *pListener )
{
return m_vecListeners.FindAndRemove( pListener );
}
//----------------------------------------------------------------------------
// Purpose: Send created/updated/destroyed calls on to all the listeners in the
// context
//----------------------------------------------------------------------------
void CGCClientSharedObjectContext::SOCreated( const CSharedObject *pObject, ESOCacheEvent eEvent ) const
{
tmZone( TELEMETRY_LEVEL0, TMZF_NONE, "%s", __FUNCTION__ );
FOR_EACH_VEC( m_vecListeners, nListener )
{
m_vecListeners[nListener]->SOCreated( m_steamIDOwner, pObject, eEvent );
}
}
void CGCClientSharedObjectContext::PreSOUpdate( ESOCacheEvent eEvent ) const
{
tmZone( TELEMETRY_LEVEL0, TMZF_NONE, "%s", __FUNCTION__ );
FOR_EACH_VEC( m_vecListeners, nListener )
{
m_vecListeners[nListener]->PreSOUpdate( m_steamIDOwner, eEvent );
}
}
void CGCClientSharedObjectContext::SOUpdated( const CSharedObject *pObject, ESOCacheEvent eEvent ) const
{
tmZone( TELEMETRY_LEVEL0, TMZF_NONE, "%s", __FUNCTION__ );
FOR_EACH_VEC( m_vecListeners, nListener )
{
m_vecListeners[nListener]->SOUpdated( m_steamIDOwner, pObject, eEvent );
}
}
void CGCClientSharedObjectContext::PostSOUpdate( ESOCacheEvent eEvent ) const
{
tmZone( TELEMETRY_LEVEL0, TMZF_NONE, "%s", __FUNCTION__ );
FOR_EACH_VEC( m_vecListeners, nListener )
{
m_vecListeners[nListener]->PostSOUpdate( m_steamIDOwner, eEvent );
}
}
void CGCClientSharedObjectContext::SODestroyed( const CSharedObject *pObject, ESOCacheEvent eEvent ) const
{
tmZone( TELEMETRY_LEVEL0, TMZF_NONE, "%s", __FUNCTION__ );
FOR_EACH_VEC( m_vecListeners, nListener )
{
m_vecListeners[nListener]->SODestroyed( m_steamIDOwner, pObject, eEvent );
}
}
void CGCClientSharedObjectContext::SOCacheSubscribed( const CSteamID & steamIDOwner, ESOCacheEvent eEvent ) const
{
tmZone( TELEMETRY_LEVEL0, TMZF_NONE, "%s", __FUNCTION__ );
FOR_EACH_VEC( m_vecListeners, nListener )
{
m_vecListeners[nListener]->SOCacheSubscribed( steamIDOwner, eEvent );
}
}
void CGCClientSharedObjectContext::SOCacheUnsubscribed( const CSteamID & steamIDOwner, ESOCacheEvent eEvent ) const
{
tmZone( TELEMETRY_LEVEL0, TMZF_NONE, "%s", __FUNCTION__ );
FOR_EACH_VEC( m_vecListeners, nListener )
{
m_vecListeners[nListener]->SOCacheUnsubscribed( steamIDOwner, eEvent );
}
}
//----------------------------------------------------------------------------
// Purpose: Constructor
//----------------------------------------------------------------------------
CGCClientSharedObjectTypeCache::CGCClientSharedObjectTypeCache( int nTypeID, const CGCClientSharedObjectContext & context )
: m_context( context ), CSharedObjectTypeCache( nTypeID )
{
}
//----------------------------------------------------------------------------
// Purpose: Destructor
//----------------------------------------------------------------------------
CGCClientSharedObjectTypeCache::~CGCClientSharedObjectTypeCache()
{
}
//----------------------------------------------------------------------------
// Purpose: Parses a cache subscribed message.
//----------------------------------------------------------------------------
bool CGCClientSharedObjectTypeCache::BParseCacheSubscribedMsg( const CMsgSOCacheSubscribed_SubscribedType & msg, CUtlVector<CSharedObject*> &vecCreatedObjects, CUtlVector<CSharedObject*> &vecUpdatedObjects, CUtlVector<CSharedObject*> &vecObjectsToDestroy )
{
CSharedObjectVec vecUntouchedObjects;
for ( uint32 i = 0; i < GetCount(); i++ )
{
vecUntouchedObjects.AddToTail( GetObject( i ) );
}
for( uint16 usObject = 0; usObject < msg.object_data_size(); usObject++ )
{
bool bUpdatedExisting = false;
CSharedObject *pObject = BCreateFromMsg( msg.object_data( usObject ).data(), msg.object_data( usObject ).size(), &bUpdatedExisting );
if ( pObject == NULL)
{
Assert( pObject );
return false;
}
// if an object was updated, remove it from the untouched list
if ( bUpdatedExisting )
{
int index = vecUntouchedObjects.Find( pObject );
if ( index != vecUntouchedObjects.InvalidIndex() )
{
vecUntouchedObjects[index] = NULL;
}
vecUpdatedObjects.AddToTail( pObject );
}
else
{
vecCreatedObjects.AddToTail( pObject );
}
}
// all objects that weren't in the SubscribedMsg should be destroyed
for ( int i = 0; i < vecUntouchedObjects.Count(); i++ )
{
if ( vecUntouchedObjects[i] == NULL )
continue;
CSharedObject *pObject = RemoveObject( *vecUntouchedObjects[i] );
Assert( pObject );
if( pObject )
vecObjectsToDestroy.AddToTail( pObject );
}
return true;
}
void CGCClientSharedObjectTypeCache::RemoveAllObjects( CUtlVector<CSharedObject*> &vecObjects )
{
// Go in reverse order to avoid O(n^2) shifting the items in the array
for ( int i = GetCount() - 1; i >= 0; i-- )
{
CSharedObject *pObject = RemoveObjectByIndex( i );
Assert( pObject );
if ( pObject )
vecObjects.AddToTail( pObject );
}
}
//----------------------------------------------------------------------------
// Purpose: Processes a received create message for an object of this type on
// the client/gameserver
//----------------------------------------------------------------------------
CSharedObject *CGCClientSharedObjectTypeCache::BCreateFromMsg( const void *pvData, uint32 unSize, bool *bUpdatedExisting )
{
CUtlBuffer bufCreate( pvData, unSize, CUtlBuffer::READ_ONLY );
CSharedObject *pNewObj = CSharedObject::Create( GetTypeID() );
Assert( pNewObj );
if( !pNewObj )
{
EmitError( SPEW_SHAREDOBJ, "Unable to create object of type %d\n", GetTypeID() );
return NULL;
}
if( !pNewObj->BParseFromMessage( bufCreate ) )
{
delete pNewObj;
return NULL;
}
// Existing object?
CSharedObject *pObj = FindSharedObject( *pNewObj );
if( pObj )
{
pObj->Copy( *pNewObj );
delete pNewObj;
if ( bUpdatedExisting )
{
*bUpdatedExisting = true;
}
return pObj;
}
// New object
AddObject( pNewObj );
if ( bUpdatedExisting )
{
*bUpdatedExisting = false;
}
return pNewObj;
}
//----------------------------------------------------------------------------
// Purpose: Processes a received destroy message for an object of this type on
// the client/gameserver
//----------------------------------------------------------------------------
bool CGCClientSharedObjectTypeCache::BDestroyFromMsg( const void *pvData, uint32 unSize )
{
CUtlBuffer bufDestroy( pvData, unSize, CUtlBuffer::READ_ONLY );
CSharedObject *pIndexObj = CSharedObject::Create( GetTypeID() );
if( !pIndexObj->BParseFromMessage( bufDestroy ) )
{
delete pIndexObj;
return false;
}
CSharedObject *pObject = RemoveObject( *pIndexObj );
if( pObject )
{
m_context.SODestroyed( pObject, eSOCacheEvent_Incremental );
delete pObject;
}
delete pIndexObj;
return true;
}
//----------------------------------------------------------------------------
// Purpose: Processes a received destroy message for an object of this type on
// the client/gameserver
//----------------------------------------------------------------------------
bool CGCClientSharedObjectTypeCache::BUpdateFromMsg( const void *pvData, uint32 unSize )
{
CUtlBuffer bufUpdate( pvData, unSize, CUtlBuffer::READ_ONLY );
CSharedObject *pIndexObj = CSharedObject::Create( GetTypeID() );
AssertMsg1( pIndexObj, "Unable to create index object of type %d", GetTypeID() );
if( !pIndexObj )
return false;
if( !pIndexObj->BParseFromMessage( bufUpdate ) )
{
delete pIndexObj;
return false;
}
CSharedObject *pObj = FindSharedObject( *pIndexObj );
bool bRet = false;
if( pObj )
{
bufUpdate.SeekGet( CUtlBuffer::SEEK_HEAD, 0 );
bRet = pObj->BUpdateFromNetwork( *pIndexObj );
m_context.SOUpdated( pObj, eSOCacheEvent_Incremental );
}
delete pIndexObj;
return bRet;
}
//----------------------------------------------------------------------------
// Purpose: Constructor
//----------------------------------------------------------------------------
CGCClientSharedObjectCache::CGCClientSharedObjectCache( const CSteamID & steamIDOwner )
: m_context( steamIDOwner ),
m_bInitialized( false ),
m_bSubscribed( false )
{
}
//----------------------------------------------------------------------------
// Purpose: Destructor
//----------------------------------------------------------------------------
CGCClientSharedObjectCache::~CGCClientSharedObjectCache()
{
}
//----------------------------------------------------------------------------
// Purpose: Process an incoming create message on a client/gameserver.
//----------------------------------------------------------------------------
bool CGCClientSharedObjectCache::BParseCacheSubscribedMsg( const CMsgSOCacheSubscribed & msg )
{
// Assume all type caches will be untouched
CUtlVector<int> vecUntouchedTypes;
for ( int i = FirstTypeCacheIndex(); i != InvalidTypeCacheIndex(); i = NextTypeCacheIndex( i ) )
{
CSharedObjectTypeCache *pTypeCache = GetTypeCacheByIndex( i );
if ( pTypeCache )
{
vecUntouchedTypes.AddToTail( pTypeCache->GetTypeID() );
}
}
// List of objects created, updated, and removed
CUtlVector<CSharedObject*> vecCreatedObjects;
CUtlVector<CSharedObject*> vecUpdatedObjects;
CUtlVector<CSharedObject*> vecObjectsToDestroy;
bool bResult = true;
// Scan types in message
for( uint16 usObject = 0; usObject < msg.objects_size(); usObject++ )
{
const CMsgSOCacheSubscribed_SubscribedType & msgType = msg.objects( usObject );
// Find or create the type
CGCClientSharedObjectTypeCache *pTypeCache = CreateTypeCache( msgType.type_id() );
if ( pTypeCache )
{
int index = vecUntouchedTypes.Find( pTypeCache->GetTypeID() );
if ( index != vecUntouchedTypes.InvalidIndex() )
{
vecUntouchedTypes[index] = -1;
}
}
Assert( pTypeCache );
if( !pTypeCache || !pTypeCache->BParseCacheSubscribedMsg( msgType, vecCreatedObjects, vecUpdatedObjects, vecObjectsToDestroy ) )
bResult = false;
}
// any type caches that weren't in the SubscribedMsg should be cleared
for ( int i = FirstTypeCacheIndex(); i != InvalidTypeCacheIndex(); i = NextTypeCacheIndex( i ) )
{
CGCClientSharedObjectTypeCache *pTypeCache = GetTypeCacheByIndex( i );
if ( vecUntouchedTypes.Find( pTypeCache->GetTypeID() ) != vecUntouchedTypes.InvalidIndex() )
{
pTypeCache->RemoveAllObjects( vecObjectsToDestroy );
}
}
// Which event is happening?
ESOCacheEvent eNotificationEvent = eSOCacheEvent_Subscribed;
if ( m_bSubscribed )
eNotificationEvent = eSOCacheEvent_Resubscribed;
// Set version, assuming we didn't have any problems. If we hit any problems,
// we want to force a refresh
if ( bResult )
SetVersion( msg.version() );
// Mark that the cache has been initialized by the server
m_bInitialized = true;
m_bSubscribed = true;
//
// Send notifications
//
// Initial cache subscribed
m_context.SOCacheSubscribed( GetOwner(), eNotificationEvent );
// Deletions
for ( int i = 0 ; i < vecObjectsToDestroy.Count() ; ++i )
{
m_context.SODestroyed( vecObjectsToDestroy[i], eNotificationEvent );
delete vecObjectsToDestroy[i];
}
// Updates
for ( int i = 0 ; i < vecUpdatedObjects.Count() ; ++i )
{
m_context.SOUpdated( vecUpdatedObjects[i], eNotificationEvent );
}
// Created
for ( int i = 0 ; i < vecCreatedObjects.Count() ; ++i )
{
m_context.SOUpdated( vecCreatedObjects[i], eNotificationEvent );
}
// Return true if everything parsed OK, or false
// if we had at least one failure
return bResult;
}
//----------------------------------------------------------------------------
// Purpose: Process an incoming create message on a client/gameserver.
//----------------------------------------------------------------------------
void CGCClientSharedObjectCache::NotifyUnsubscribe()
{
if ( m_bSubscribed )
{
m_bSubscribed = false;
m_context.SOCacheUnsubscribed( GetOwner(), eSOCacheEvent_Unsubscribed );
}
else
{
AssertMsg( m_bSubscribed, "GC Sending us Unsubscribed message when we weren't subscribed" ); // Might not be a bug, but something worth checking
}
}
//----------------------------------------------------------------------------
// Purpose: GC is telling us that the version we have is up-to-date,a nd we are subscribed
//----------------------------------------------------------------------------
void CGCClientSharedObjectCache::NotifyResubscribedUpToDate()
{
if ( !m_bSubscribed )
{
Assert( m_bInitialized );
m_bSubscribed = true;
m_context.SOCacheSubscribed( GetOwner(), eSOCacheEvent_Subscribed );
}
else
{
AssertMsg( m_bSubscribed, "Got NotifyResubscribedUpToDate when we were already subscribed?" ); // Might not be a bug, but something worth checking
}
}
//----------------------------------------------------------------------------
// Purpose: Process an incoming create message on a client/gameserver.
//----------------------------------------------------------------------------
bool CGCClientSharedObjectCache::BCreateFromMsg( int nTypeID, const void *pvData, uint32 unSize )
{
// We should be subscribed
if ( !m_bInitialized || !m_bSubscribed )
{
// Note: We can go down and come back up without the GC knowing this.
// So this can happen
//Assert( m_bInitialized );
//Assert( m_bSubscribed );
//EmitWarning( SPEW_SHAREDOBJ, 1, "Received SOCache incremental update for cache we were not subscribed to (object type %d)\n", nTypeID );
}
// Locate / create the type cache
CGCClientSharedObjectTypeCache *pTypeCache = CreateTypeCache( nTypeID );
// Create the message or update existing
bool bUpdatedExisting = false;
CSharedObject *pObject = pTypeCache->BCreateFromMsg( pvData, unSize, &bUpdatedExisting );
if ( pObject == NULL )
return false;
// Send notifications to listeners
if ( bUpdatedExisting )
{
// This can happen --- see comment at the top of this function
//Assert( !bUpdatedExisting ); // shouldn't the GC know what it's already sent us? This is weird
m_context.SOUpdated( pObject, eSOCacheEvent_Incremental );
}
else
{
m_context.SOCreated( pObject, eSOCacheEvent_Incremental );
}
return true;
}
//----------------------------------------------------------------------------
// Purpose: Processes an incoming destroy message on a client/gameserver.
//----------------------------------------------------------------------------
bool CGCClientSharedObjectCache::BDestroyFromMsg( int nTypeID, const void *pvData, uint32 unSize )
{
CGCClientSharedObjectTypeCache *pTypeCache = FindTypeCache( nTypeID );
if( pTypeCache )
{
return pTypeCache->BDestroyFromMsg( pvData, unSize );
}
else
{
return false;
}
}
//----------------------------------------------------------------------------
// Purpose: Processes an incoming update message on a client/gameserver.
//----------------------------------------------------------------------------
bool CGCClientSharedObjectCache::BUpdateFromMsg( int nTypeID, const void *pvData, uint32 unSize )
{
CGCClientSharedObjectTypeCache *pTypeCache = FindTypeCache( nTypeID );
if( pTypeCache )
{
return pTypeCache->BUpdateFromMsg( pvData, unSize );
}
else
{
return false;
}
}
//----------------------------------------------------------------------------
// Purpose: Adds a listener object to be notified of object changes in this
// cache. The shared object cache does not own this object and will
// not free it.
//----------------------------------------------------------------------------
void CGCClientSharedObjectCache::AddListener( ISharedObjectListener *pListener )
{
Assert( pListener );
if ( !m_context.BAddListener( pListener ) )
return; // was already listening, no action needed
SOCDebug( "[%s] Adding listener %s\n", GetOwner().Render(), typeid( *pListener ).name() );
// If we're already subscribed, then immediately send notifications
if( BIsSubscribed() )
{
pListener->SOCacheSubscribed( GetOwner(), eSOCacheEvent_ListenerAdded );
for ( int i = FirstTypeCacheIndex(); i != InvalidTypeCacheIndex(); i = NextTypeCacheIndex( i ) )
{
CGCClientSharedObjectTypeCache *pTypeCache = GetTypeCacheByIndex( i );
for ( uint32 j = 0 ; j < pTypeCache->GetCount() ; ++j )
{
CSharedObject *pObject = pTypeCache->GetObject( j );
pListener->SOCreated( GetOwner(), pObject, eSOCacheEvent_ListenerAdded );
}
}
}
}
//----------------------------------------------------------------------------
// Purpose: Removes a listener object from the list to be notified of changes
// to this object cache.
//----------------------------------------------------------------------------
bool CGCClientSharedObjectCache::RemoveListener( ISharedObjectListener *pListener )
{
Assert( pListener );
if ( !m_context.BRemoveListener( pListener ) )
return false; // wasn't already listening, nothing to do
SOCDebug( "[%s] Removing listener %s\n", GetOwner().Render(), typeid( *pListener ).name() );
// If we were subscribed, then the listener's last subscribe notification
// was a "you are subscribed." Send him an unsubscribed notification
// so he doesn't think he's still subscribed.
if( BIsSubscribed() )
{
pListener->SOCacheUnsubscribed( GetOwner(), eSOCacheEvent_ListenerRemoved );
}
return true;
}
} // namespace GCSDK