-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.cpp
322 lines (272 loc) · 9.29 KB
/
query.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
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod Client Preferences Extension
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program 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
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#include "query.h"
#define CookieName 0
#define CookieDesc 1
#define CookieAcce 2
#define CookieValue 3
#include <functional>
#include <string>
// Only run on main thread
void TQueryOp::RunThinkPart()
{
switch (m_type) {
case Query_InsertCookie:
{
g_CookieManager.InsertCookieCallback(m_pCookie, m_insertId);
break;
}
case Query_SelectData:
{
g_CookieManager.ClientConnectCallback(m_serial, m_results);
break;
}
case Query_SelectId:
{
g_CookieManager.SelectIdCallback(m_pCookie, m_insertId);
break;
}
case Query_Connect:
{
return;
}
default:
{
break;
}
}
}
void TQueryOp::RunThreadPart()
{
if (m_type == Query_Connect) {
g_ClientPrefs.DatabaseConnect();
return;
}
// assert(m_database != NULL);
/* I don't think this is needed anymore... keeping for now. */
// m_database->LockForFullAtomicOperation();
if (!BindParamsAndRun()) {
g_pSM->LogError(myself,
"Failed Redis Query, Error: \"%s\" (Query id %i - serial %i)",
m_database->GetErrorString(),
m_type,
m_serial);
}
// m_database->UnlockFromFullAtomicOperation();
}
//IDBDriver *TQueryOp::GetDriver()
//{
// return m_driver;
//}
IdentityToken_t *TQueryOp::GetOwner()
{
return myself->GetIdentity();
}
void TQueryOp::Destroy()
{
delete this;
}
TQueryOp::TQueryOp(enum querytype type, int serial)
{
m_type = type;
m_serial = serial;
m_database = NULL;
// m_driver = NULL;
m_insertId = -1;
// m_pResult = NULL;
}
TQueryOp::TQueryOp(enum querytype type, Cookie *cookie)
{
m_type = type;
m_pCookie = cookie;
m_database = NULL;
// m_driver = NULL;
m_insertId = -1;
// m_pResult = NULL;
m_serial = 0;
}
void TQueryOp::SetDatabase(void *db)
{
m_database = (async_redis::client *)db;
}
bool TQueryOp::BindParamsAndRun()
{
switch (m_type) {
case Query_InsertCookie:
{
std::string safe_name = m_params.cookie->name;
std::string safe_desc = m_params.cookie->description;
// check if we have that cookie
{
auto reply = m_database->Command({ "GET", "cookies.id." + safe_name }).get();
if (reply->IsString()) {
m_database->Append({ "SADD", "cookies.list", safe_name }).Commit();
m_insertId = std::stoi(reply->GetString());
return true;
}
}
std::hash<std::string> h;
m_insertId = h(safe_name) & 0x7FFFFFFF;
auto &client = *m_database;
client.Append({ "SADD", "cookies.list", safe_name })
.Append({ "SET", "cookies.access." + safe_name, std::to_string((int)m_params.cookie->access) })
.Append({ "SET", "cookies.desc." + safe_name, safe_desc })
.Append({ "SET", "cookies.id." + safe_name, std::to_string(m_insertId) })
.Commit();
return true;
}
case Query_SelectData:
{
m_results.clear();
std::string steamId = m_params.steamId;
// Try to use cached Lua query first
auto cookies = m_database->Command({ "EVALSHA", GET_CLIENT_COOKIES_SHA, "1", steamId }).get();
if (cookies && cookies->Ok()) {
if (!cookies->IsArrays()) {
return true;
}
for (const auto &cookieData : cookies->GetArray()) {
if (!cookieData.IsArrays() || cookieData.GetArray().size() != 4) {
break;
}
const auto &cookie = cookieData.GetArray();
CookieAccess acce;
try {
acce = (CookieAccess)std::stoi(cookie[CookieAcce].GetString());
} catch (const std::exception&) {
acce = CookieAccess_Public;
}
std::string value;
if (cookie[CookieValue].IsString()) {
value = cookie[CookieValue].GetString();
}
m_results.push_back({
Cookie(
cookie[CookieName].GetString().c_str(),
cookie[CookieDesc].GetString().c_str(),
acce),
value.c_str()
});
}
return true;
}
// Use normal and slow method, if you have too many cookies (>= 10M), you should use the lua method
auto keys = m_database->Command({ "KEYS", "cookies.id.*" }).get();
if (!keys || !keys->IsArrays()) {
return true;
}
for (const auto &item : keys->GetArray()) {
std::string cookieName = item.GetString().substr(11);
auto cookie_id = m_database->Command({ "GET", item.GetString() }).get();
if (!cookie_id->IsString()) {
g_pSM->LogError(myself, "Expect %s to be REDIS_REPLY_STRING but got %d", item.GetString().c_str(), cookie_id->Type());
continue;
}
std::string key_name = steamId + "." + cookie_id->GetString();
std::string key_desc = "cookies.desc." + cookieName;
std::string key_id = "cookies.access." + cookieName;
auto _value = m_database->Command({ "GET", key_name }, false);
auto _desc = m_database->Command({ "GET", key_desc }, false);
auto _access = m_database->Command({ "GET", key_id }, false);
m_database->Commit();
auto value = _value.get();
auto desc = _desc.get();
auto access = _access.get();
CookieAccess acce;
try {
acce = (CookieAccess)std::stoi(access->GetString());
} catch (const std::exception&) {
acce = CookieAccess_Public;
}
std::string cookie_value;
if (value->IsString()) {
cookie_value = value->GetString();
}
m_results.push_back({
Cookie(
cookieName.c_str(),
desc->GetString().c_str(),
acce),
cookie_value.c_str()
});
}
return true;
}
case Query_InsertData:
{
std::string safe_id = m_params.steamId;
std::string safe_val = m_params.data->value;
int cookieId = m_params.cookieId;
safe_id = safe_id + "." + std::to_string(cookieId);
m_database->Append({ "SET", safe_id, safe_val, "EX", "1209600" }).Commit(); // Set this key expire in 2 weeks
return true;
}
case Query_SelectId:
{
std::string safe_name = m_params.steamId;
std::hash<std::string> h;
int id = h(safe_name) & 0x7FFFFFFF;
auto rep = m_database->Command({ "GET", "cookies.id." + safe_name }).get();
if (rep->IsString()) {
return false;
}
m_insertId = std::stoi(rep->GetString());
if (id != m_insertId) {
g_pSM->LogError(myself, "Cookies ID does not match %d vs %d", id, m_insertId);
}
return true;
}
}
return false;
}
querytype TQueryOp::PullQueryType()
{
return m_type;
}
int TQueryOp::PullQuerySerial()
{
return m_serial;
}
ParamData::~ParamData()
{
if (data) {
/* Data is only ever passed in a client disconnect query and always needs to be deleted */
delete data;
data = NULL;
}
}
ParamData::ParamData()
{
cookie = NULL;
data = NULL;
steamId[0] = '\0';
cookieId = 0;
}