summaryrefslogtreecommitdiff
path: root/public/gcsdk/gc_sharedobjectcache.h
blob: e7cdfc4cc986f34db4fc7d58f86afb93d52634a3 (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Additional shared object cache functionality for the GC
//
//=============================================================================

#ifndef GC_SHAREDOBJECTCACHE_H
#define GC_SHAREDOBJECTCACHE_H
#ifdef _WIN32
#pragma once
#endif

#include "sharedobjectcache.h"
#include "tier1/utlhashtable.h"
#include "gcdirtyfield.h"
#include "gcsdk/gcsystemaccess.h"

class CMsgSOCacheSubscribed_SubscribedType;

#include "tier0/memdbgon.h"

namespace GCSDK
{

class CCachedSubscriptionMessage;

//----------------------------------------------------------------------------
// Purpose: The part of a shared object cache that handles all objects of a 
//			single type.
//----------------------------------------------------------------------------
class CSharedObjectContext
{
public:

	//holds information about a subscriber to the context
	struct Subscriber_t
	{
		CSteamID	m_steamID;		//the steam ID of the subscriber
		int			m_nRefCount;	//the number of references to this subscription
	};

	CSharedObjectContext( const CSteamID & steamIDOwner );

	bool BAddSubscriber( const CSteamID & steamID );
	bool BRemoveSubscriber( const CSteamID & steamID );
	void RemoveAllSubscribers();
	bool BIsSubscribed( const CSteamID & steamID ) const			{ return FindSubscriber( steamID ) != m_vecSubscribers.InvalidIndex(); }
	
	const CUtlVector< Subscriber_t > &	GetSubscribers() const		{ return m_vecSubscribers; }
	const CSteamID &					GetOwner() const			{ return m_steamIDOwner; }

private:

	//finds a steam ID within the subscriber list, returns the index, invalid if it can't be found
	int FindSubscriber( const CSteamID& steamID ) const;

	CUtlVector<Subscriber_t>	m_vecSubscribers;
	CSteamID					m_steamIDOwner;
};

class CGCSharedObjectTypeCache;

enum ESOTypeFlags
{
	k_ESOFlag_SendToNobody					= 0,
	k_ESOFlag_SendToOwner					= 1 << 0,  // will go to the owner of the cache (user or gameserver), if he is subscribed
	k_ESOFlag_SendToOtherUsers				= 1 << 1,  // will go to subscribed users who are not the owner of the cache
	k_ESOFlag_SendToOtherGameservers		= 1 << 2,  // will go to subscribed gameservers who are not the owner of the cache
	k_ESOFlag_SendToQuestObjectiveTrackers	= 1 << 3,
	k_ESOFlag_LastFlag						= k_ESOFlag_SendToQuestObjectiveTrackers,
};

//----------------------------------------------------------------------------
// Purpose: Filter object used to determine whether a type cache's objects should
//	should be sent to subscribers and whether each object should be sent
//----------------------------------------------------------------------------
class CISubscriberMessageFilter
{
public:
	virtual bool BShouldSendAnyObjectsInCache( CGCSharedObjectTypeCache *pTypeCache, uint32 unFlags ) const = 0;
	virtual bool BShouldSendObject( CSharedObject *pSharedObject, uint32 unFlags ) const = 0;
};


//----------------------------------------------------------------------------
// Purpose: The part of a shared object cache that handles all objects of a 
//			single type.
//----------------------------------------------------------------------------
class CGCSharedObjectTypeCache : public CSharedObjectTypeCache
{
public:

	typedef CSharedObjectTypeCache Base;

	CGCSharedObjectTypeCache( int nTypeID, const CSharedObjectContext & context );
	virtual ~CGCSharedObjectTypeCache();

	virtual bool AddObject( CSharedObject *pObject );
	virtual CSharedObject *RemoveObject( const CSharedObject & soIndex );

	inline CSteamID GetOwner() const { return m_context.GetOwner(); }

	void BuildCacheSubscribedMsg( CMsgSOCacheSubscribed_SubscribedType *pMsgType, uint32 unFlags, const CISubscriberMessageFilter &filter );
	virtual void EnsureCapacity( uint32 nItems );

#ifdef DBGFLAG_VALIDATE
	virtual void Validate( CValidator &validator, const char *pchName );
#endif

private:

	const CSharedObjectContext & m_context;
};



//----------------------------------------------------------------------------
// Purpose: A cache of a bunch of shared objects of different types. This class
//			is shared between clients, gameservers, and the GC and is 
//			responsible for sending messages from the GC to cause object 
//			creation/destruction/updating on the clients/gameservers.
//----------------------------------------------------------------------------

class CGCSharedObjectCache : public CSharedObjectCache
{
public:
	CGCSharedObjectCache( const CSteamID & steamIDOwner = CSteamID() );
	virtual ~CGCSharedObjectCache();

	const CSteamID & GetOwner() const { return m_context.GetOwner(); }
	const CUtlVector< CSharedObjectContext::Subscriber_t > & GetSubscribers() const { return m_context.GetSubscribers(); }

	CGCSharedObjectTypeCache *FindTypeCache( int nClassID ) const { return (CGCSharedObjectTypeCache *)FindBaseTypeCache( nClassID ); }
	CGCSharedObjectTypeCache *CreateTypeCache( int nClassID ) { return (CGCSharedObjectTypeCache *)CreateBaseTypeCache( nClassID ); }

	virtual uint32 CalcSendFlags( const CSteamID &steamID ) const;
	virtual const CISubscriberMessageFilter &GetSubscriberMessageFilter();
	virtual bool AddObject( CSharedObject *pSharedObject );
	virtual bool AddObjectClean( CSharedObject *pSharedObject );

	bool BDestroyObject( const CSharedObject & soIndex, bool bRemoveFromDatabase );
	virtual CSharedObject *RemoveObject( const CSharedObject & soIndex );

	template< typename SOClass_t >
	bool BYieldingLoadSchObjects( IGCSQLResultSet *pResultSet, const CColumnSet & csRead, const SOClass_t & schDefaults );

	template< typename SOClass_t >
	bool BYieldingLoadSchSingleton( IGCSQLResultSet *pResultSet, const CColumnSet & csRead, const SOClass_t & schDefaults );

	template< typename SOClass_t, typename SchClass_t >
	bool BYieldingLoadProtoBufObjects( IGCSQLResultSet *pResultSet, const CColumnSet & csRead );

	template< typename SOClass_t, typename SchClass_t >
	bool BYieldingLoadProtoBufSingleton( IGCSQLResultSet *pResultSet, const CColumnSet & csRead, const SchClass_t & schDefaults );

	// @todo temporary for trading and item subscriptions (to be removed once we get cross-game trading)
	virtual void SetTradingPartner( const CSteamID &steamID );
	const CSteamID &GetTradingPartner() const { return m_steamIDTradingPartner; }

	void AddSubscriber( const CSteamID & steamID, bool bForceSendSubscriptionMsg = false );
	void RemoveSubscriber( const CSteamID & steamID );
	void RemoveAllSubscribers();
	void SendSubscriberMessage( const CSteamID & steamID );
	bool BIsSubscribed( const CSteamID & steamID ) { return m_context.BIsSubscribed( steamID ); }
	void ClearCachedSubscriptionMessage();

	bool BIsDatabaseDirty() const { return m_databaseDirtyList.NumDirtyObjects() > 0; }

	// This will mark the field as dirty for both network and database
	void DirtyObjectField( CSharedObject *pObj, int nFieldIndex );

	// Marks only dirty for network
	void DirtyNetworkObject( CSharedObject *pObj );
	void DirtyNetworkObjectCreate( CSharedObject *pObj );

	// Mark dirty for database
	void DirtyDatabaseObjectField( CSharedObject *pObj, int nFieldIndex );

	void SendNetworkUpdates( CSharedObject *pObj );

	// Add a specific object write to a transaction. The cache is expected to remain locked until this transaction is
	// closed.  If the transaction is rolled back, the object will be returned to the dirty list.
	bool BYieldingAddWriteToTransaction( CSharedObject *pObj, CSQLAccess & sqlAccess );

	// Add all pending object writes to a transaction.  The cache is expected to remain locked until this transaction is
	// closed.  If the transaction successfully commits, the dirty list will be flushed.
	//
	// This is intended for use in writeback -- no changes to the dirty objects list may occur until this transaction
	// closes.
	uint32 YieldingStageAllWrites( CSQLAccess & sqlAccess );

	void SendAllNetworkUpdates();
	void FlushInventoryCache();
	void YieldingWriteToDatabase( CSharedObject *pObj );

	void SetInWriteback( bool bInWriteback );
	bool GetInWriteback() const { return m_bInWriteback; }
	RTime32 GetWritebackTime() const { return m_unWritebackTime; }

	void SetLRUHandle( uint32 unLRUHandle ) { m_unLRUHandle = unLRUHandle; }
	uint32 GetLRUHandle() const { return m_unLRUHandle; }

	void Dump() const;
	void DumpDirtyObjects() const;
#ifdef DBGFLAG_VALIDATE
	virtual void Validate( CValidator &validator, const char *pchName );
#endif

	bool IsObjectCached( const CSharedObject *pObj ) const						{ return IsObjectCached( pObj, pObj->GetTypeID() ); }
	//the same as the above, but takes in the type since if called from a destructor, you can't use virtual functions
	bool IsObjectCached( const CSharedObject *pObj, uint32 nTypeID ) const;
	bool IsObjectDirty( const CSharedObject *pObj ) const;

	//called to mark that we are no longer loading
	void SetDetectVersionChanges( bool bState )		{ m_bDetectVersionChanges = bState; }

protected:

	virtual CSharedObjectTypeCache *AllocateTypeCache( int nClassID ) const OVERRIDE { return new CGCSharedObjectTypeCache( nClassID, m_context ); }

	virtual void MarkDirty();
	virtual bool BShouldSendToAnyClients( uint32 unFlags ) const;
	CCachedSubscriptionMessage *BuildSubscriberMessage( uint32 unFlags );

	CSteamID m_steamIDTradingPartner;

protected:
	void SendNetworkCreateInternal( CSharedObject * pObj );
	void SendNetworkUpdateInternal( CSharedObject * pObj );
	void SendUnsubscribeMessage( const CSteamID & steamID );

	//this is a flag that when set will cause any version changes to trigger an assert. This can be used during times like loading to ensure we don't have inappropriate version changes which
	//can cause inefficiencies
	bool	m_bDetectVersionChanges;

	CSharedObjectContext m_context;
	CUtlHashtable< CSharedObject * > m_networkDirtyObjs;
	CUtlHashtable< CSharedObject * > m_networkDirtyObjsCreate;
	CSharedObjectDirtyList m_databaseDirtyList;
	bool m_bInWriteback;
	RTime32 m_unWritebackTime;
	uint32 m_unLRUHandle;
	uint32 m_unCachedSubscriptionMsgFlags;
	CCachedSubscriptionMessage *m_pCachedSubscriptionMsg;
};




//----------------------------------------------------------------------------
// Purpose: Loads a list of CSchemaSharedObjects from a result list from a
//			query.
// Inputs:	pResultSet - The result set from the SQL query
//			schDefaults - A schema object that defines the values to set in
//				the new objects for fields that were not read in the query.
//				Typically this will be whatever fields were in the WHERE
//				clause of the query.
//			csRead - A columnSet defining the fields that were read in the query.
//----------------------------------------------------------------------------
template< typename SOClass_t >
bool CGCSharedObjectCache::BYieldingLoadSchObjects( IGCSQLResultSet *pResultSet, const CColumnSet & csRead, const SOClass_t & objDefaults )
{
	if ( NULL == pResultSet )
		return false;

	//don't bother creating a cache if we don't have objects to add into it
	if( pResultSet->GetRowCount() > 0 )
	{
		CGCSharedObjectTypeCache *pTypeCache = CreateTypeCache( SOClass_t::k_nTypeID );
		pTypeCache->EnsureCapacity( pResultSet->GetRowCount() );
		for( CSQLRecord record( 0, pResultSet ); record.IsValid(); record.NextRow() )
		{
			SOClass_t *pObj = new SOClass_t();
			pObj->Obj() = objDefaults.Obj();
			record.BWriteToRecord( &pObj->Obj(), csRead );
			pTypeCache->AddObjectClean( pObj );
		}
	}

	return true;
}

//----------------------------------------------------------------------------
// Purpose: Loads a single object of a type. If the object is not available,
//	a new object will be created at default values
// Inputs:	pResultSet - The result set from the SQL query
//			schDefaults - A schema object that defines the values to set in
//				the new objects for fields that were not read in the query.
//				Typically this will be whatever fields were in the WHERE
//				clause of the query.
//			csRead - A columnSet defining the fields that were read in the query.
//----------------------------------------------------------------------------
template< typename SOClass_t >
bool CGCSharedObjectCache::BYieldingLoadSchSingleton( IGCSQLResultSet *pResultSet, const CColumnSet & csRead, const SOClass_t & objDefaults )
{
	if ( NULL == pResultSet )
		return false;

	if ( pResultSet->GetRowCount() > 1 )
	{
		EmitError( SPEW_SHAREDOBJ, "Multiple rows passed to BYieldingLoadSchSingleton() on type %d\n", objDefaults.GetTypeID() );
		return false;
	}
	else if ( pResultSet->GetRowCount() == 1 )
	{
		return BYieldingLoadSchObjects<SOClass_t>( pResultSet, csRead, objDefaults );
	}
	else
	{
		// Create it if there wasn't one
		SOClass_t *pSchObj = new SOClass_t();
		pSchObj->Obj() = objDefaults.Obj();
		if( !pSchObj->BYieldingAddToDatabase() )
		{
			EmitError( SPEW_SHAREDOBJ, "Unable to add singleton type %d for %s\n", pSchObj->GetTypeID(), GetOwner().Render() );
			return false;
		}
		AddObjectClean( pSchObj );
		return true;
	}
}


//----------------------------------------------------------------------------
// Purpose: Loads a list of CProtoBufSharedObjects from a result list from a
//			query.
// Inputs:	pResultSet - The result set from the SQL query
//			schDefaults - A schema object that defines the values to set in
//				the new objects for fields that were not read in the query.
//				Typically this will be whatever fields were in the WHERE
//				clause of the query.
//			csRead - A columnSet defining the fields that were read in the query.
//----------------------------------------------------------------------------
template< typename SOClass_t, typename SchClass_t >
bool CGCSharedObjectCache::BYieldingLoadProtoBufObjects( IGCSQLResultSet *pResultSet, const CColumnSet & csRead )
{
	if ( NULL == pResultSet )
		return false;

	//don't bother creating a cache if we don't have objects to add into it
	if( pResultSet->GetRowCount() > 0 )
	{
		CGCSharedObjectTypeCache *pTypeCache = CreateTypeCache( SOClass_t::k_nTypeID );
		pTypeCache->EnsureCapacity( pResultSet->GetRowCount() );
		for( CSQLRecord record( 0, pResultSet ); record.IsValid(); record.NextRow() )
		{
			SchClass_t schRecord;
			record.BWriteToRecord( &schRecord, csRead );

			SOClass_t *pObj = new SOClass_t();
			pObj->ReadFromRecord( schRecord );
			pTypeCache->AddObjectClean( pObj );
		}
	}

	return true;
}


//----------------------------------------------------------------------------
// Purpose: Loads a single object of a type. If the object is not available,
//	a new object will be created at default values
// Inputs:	pResultSet - The result set from the SQL query
//			schDefaults - A schema object that defines the values to set in
//				the new objects for fields that were not read in the query.
//				Typically this will be whatever fields were in the WHERE
//				clause of the query.
//			csRead - A columnSet defining the fields that were read in the query.
//----------------------------------------------------------------------------
template< typename SOClass_t, typename SchClass_t >
bool CGCSharedObjectCache::BYieldingLoadProtoBufSingleton( IGCSQLResultSet *pResultSet, const CColumnSet & csRead, const SchClass_t & schDefaults )
{
	if ( NULL == pResultSet )
		return false;

	if ( pResultSet->GetRowCount() > 1 )
	{
		EmitError( SPEW_SHAREDOBJ, "Multiple rows passed to BYieldingLoadProtoBufSingleton() on type %d\n", SOClass_t::k_nTypeID );
		return false;
	}

	// load the duel summary
	SchClass_t schRead;
	CSQLRecord record( 0, pResultSet );
	if( record.IsValid() )
	{
		record.BWriteToRecord( &schRead, csRead );
	}
	else
	{
		CSQLAccess sqlAccess;
		if( !sqlAccess.BYieldingInsertRecord( const_cast<SchClass_t *>( &schDefaults ) ) )
			return false;
		schRead = schDefaults;
	}

	SOClass_t *pSharedObject = new SOClass_t();
	pSharedObject->ReadFromRecord( schRead );
	AddObjectClean( pSharedObject );

	return true;
}


} // namespace GCSDK

#include "tier0/memdbgoff.h"

#endif //GC_SHAREDOBJECTCACHE_H