forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcwgjobmgr.cpp
210 lines (166 loc) · 7.04 KB
/
gcwgjobmgr.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
//========= Copyright Valve Corporation, All rights reserved. ============//
#include "stdafx.h"
#include "enumutils.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
namespace GCSDK
{
ENUMSTRINGS_START( EGCWebApiPrivilege )
{ k_EGCWebApiPriv_None, "None" },
{ k_EGCWebApiPriv_Account, "Account" },
{ k_EGCWebApiPriv_Approved, "Approved" },
{ k_EGCWebApiPriv_Session, "Session" },
{ k_EGCWebApiPriv_Support, "Support" },
{ k_EGCWebApiPriv_Admin, "Admin" },
{ k_EGCWebApiPriv_EditApp, "EditApp" },
{ k_EGCWebApiPriv_MemberPublisher, "MemberPublisher" },
{ k_EGCWebApiPriv_EditPublisher, "EditPublisher" },
{ k_EGCWebApiPriv_AccountOptional, "AccountOptional" },
ENUMSTRINGS_END( EGCWebApiPrivilege );
//-----------------------------------------------------------------------------
// Purpose: Singleton accessor to registered WG jobs
//-----------------------------------------------------------------------------
CUtlDict< const JobCreationFunc_t* > &GMapGCWGJobCreationFuncs()
{
static CUtlDict< const JobCreationFunc_t* > s_MapWGJobCreationFuncs;
return s_MapWGJobCreationFuncs;
}
//-----------------------------------------------------------------------------
// Purpose: Singleton accessor to registered WG jobs
//-----------------------------------------------------------------------------
CUtlDict< const WebApiFunc_t* > &GMapGCWGRequestInfo()
{
static CUtlDict< const WebApiFunc_t* > s_MapWGRequestInfo;
return s_MapWGRequestInfo;
}
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CGCWGJobMgr::CGCWGJobMgr( )
{
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
CGCWGJobMgr::~CGCWGJobMgr()
{
}
//-----------------------------------------------------------------------------
// Purpose: Hub has receieved a k_EMsgWGRequest message, find & dispatch WG Job
//-----------------------------------------------------------------------------
bool CGCWGJobMgr::BHandleMsg( IMsgNetPacket *pNetPacket )
{
CGCMsg<MsgGCWGRequest_t> msg( pNetPacket );
AssertMsg( msg.GetEMsg() == k_EGCMsgWGRequest, "GCWGJobMgr asked to route a message, but it is not a MsgWGRequest" );
CUtlString strRequestName;
msg.BReadStr( &strRequestName );
int iJobType = GMapGCWGRequestInfo().Find( strRequestName.Get() );
if ( !GMapGCWGRequestInfo().IsValidIndex( iJobType ) )
{
EmitError( SPEW_GC, "Unable to find GC WG handler %s", strRequestName.Get() );
SendErrorMessage( msg, "Unknown method", k_EResultInvalidParam );
return false;
}
const WebApiFunc_t * pFunc = GMapGCWGRequestInfo()[iJobType];
if( !BVerifyPrivileges( msg, pFunc ) )
return false;
CGCWGJob *job = (CGCWGJob *)(*((GMapGCWGJobCreationFuncs())[iJobType]))( GGCBase(), NULL );
Assert( job );
job->SetWebApiFunc( pFunc );
job->StartJobFromNetworkMsg( pNetPacket, msg.Hdr().m_JobIDSource );
return true;
}
bool CGCWGJobMgr::BVerifyPrivileges( const CGCMsg<MsgGCWGRequest_t> & msg, const WebApiFunc_t * pFunc )
{
if(msg.Body().m_unPrivilege != (uint32)pFunc->m_eRequiredPrivilege )
{
SendErrorMessage( msg,
CFmtStr( "Privilege mismatch in %s gc call. Expected: %s, actual: %s", pFunc->m_pchRequestName, PchNameFromEGCWebApiPrivilege( pFunc->m_eRequiredPrivilege ), PchNameFromEGCWebApiPrivilege( (EGCWebApiPrivilege)msg.Body().m_unPrivilege ) ),
k_EResultAccessDenied );
return false;
}
else
{
return true;
}
}
//-----------------------------------------------------------------------------
// Purpose: Sends an error message in response to a WG request
// Inputs: msg - The message we're responding to. This causes the error to
// be routed to the right place and eventually back to the right
// browser.
// pchErrorMsg - The message to display
// nResult - The error code to return
//-----------------------------------------------------------------------------
void CGCWGJobMgr::SendErrorMessage( const CGCMsg<MsgGCWGRequest_t> & msg, const char *pchErrorMsg, int32 nResult )
{
KeyValuesAD pkvErr( "error" );
pkvErr->SetString( "error", pchErrorMsg );
pkvErr->SetInt( "success", nResult );
SendResponse( msg, pkvErr, false );
}
//-----------------------------------------------------------------------------
// Purpose: Sets an error message in a response to a WG request
// Inputs: pkvResponse - The response KV block to set the error in
// pchErrorMsg - The message to display
// nResult - The error code to return
//-----------------------------------------------------------------------------
void CGCWGJobMgr::SetErrorMessage( KeyValues *pkvErr, const char *pchErrorMsg, int32 nResult )
{
pkvErr->SetString( "error", pchErrorMsg );
pkvErr->SetInt( "success", nResult );
}
//-----------------------------------------------------------------------------
// Purpose: Sends a message in response to a WG request
// Inputs: msg - The message we're responding to. This causes the error to
// be routed to the right place and eventually back to the right
// browser.
// pkvResponse - The KeyValues containing the response data
//-----------------------------------------------------------------------------
void CGCWGJobMgr::SendResponse( const CGCMsg<MsgGCWGRequest_t> & msg, KeyValues *pkvResponse, bool bResult )
{
//prepare response msg
CGCMsg<MsgGCWGResponse_t> msgResponse( k_EGCMsgWGResponse, msg );
CUtlBuffer bufResponse;
KVPacker packer;
if ( packer.WriteAsBinary( pkvResponse, bufResponse ) )
{
msgResponse.AddVariableLenData( bufResponse.Base(), bufResponse.TellPut() );
msgResponse.Body().m_cubKeyValues = bufResponse.TellPut();
msgResponse.Body().m_bResult = bResult;
}
else
{
msgResponse.Body().m_cubKeyValues = 0;
AssertMsg( false, "Failed to serialize WG response" );
}
msgResponse.Hdr().m_JobIDTarget = msg.Hdr().m_JobIDSource;
GGCBase()->BSendSystemMessage( msgResponse );
}
//-----------------------------------------------------------------------------
// Purpose: Adds a WG job handler to the global list
//-----------------------------------------------------------------------------
void CGCWGJobMgr::RegisterWGJob( const WebApiFunc_t *pWGJobType, const JobType_t *pJobCreationFunc )
{
const char *pchRequestName = pWGJobType->m_pchRequestName;
GMapGCWGJobCreationFuncs().Insert( pchRequestName, &(pJobCreationFunc->m_pJobFactory) );
GMapGCWGRequestInfo().Insert( pchRequestName, pWGJobType );
}
//-----------------------------------------------------------------------------
// Purpose: Get the list of registered WG jobs
//-----------------------------------------------------------------------------
CUtlDict< const WebApiFunc_t* > &CGCWGJobMgr::GetWGRequestMap()
{
return GMapGCWGRequestInfo();
}
#ifdef DBGFLAG_VALIDATE
void CGCWGJobMgr::Validate( CValidator &validator, const char *pchName )
{
}
void CGCWGJobMgr::ValidateStatics( CValidator &validator )
{
ValidateObj( GMapGCWGJobCreationFuncs() );
ValidateObj( GMapGCWGRequestInfo() );
}
#endif // DBGFLAG_VALIDATE
} // namespace GCSDK