database.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "../common/debug.h"
  17. #include <iostream>
  18. using namespace std;
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <errmsg.h>
  23. //#include <mysqld_error.h>
  24. #include <limits.h>
  25. #include <ctype.h>
  26. #include <assert.h>
  27. #include <map>
  28. // Disgrace: for windows compile
  29. #ifdef WIN32
  30. #include <WinSock2.h>
  31. #include <windows.h>
  32. #define snprintf _snprintf
  33. #define strncasecmp _strnicmp
  34. #define strcasecmp _stricmp
  35. #else
  36. #include "unix.h"
  37. #include <netinet/in.h>
  38. #endif
  39. #include "database.h"
  40. #include "EQStream.h"
  41. #include "packet_functions.h"
  42. #include "emu_opcodes.h"
  43. #ifdef WORLD
  44. #include "../WorldServer/WorldDatabase.h"
  45. extern WorldDatabase database;
  46. #endif
  47. #ifdef LOGIN
  48. #include "../LoginServer/LoginDatabase.h"
  49. extern LoginDatabase database;
  50. #endif
  51. #ifdef PARSER
  52. #include "../PacketParser/ParserDatabase.h"
  53. extern ParserDatabase database;
  54. #endif
  55. #ifdef PATCHER
  56. #include "../PatchServer/PatcherDatabase.h"
  57. extern PatcherDatabase database;
  58. #endif
  59. #include "../common/EQEMuError.h"
  60. #include "../common/packet_dump.h"
  61. #include "../common/Log.h"
  62. #ifdef WORLD
  63. ThreadReturnType DBAsyncQueries(void* str)
  64. {
  65. // allow some buffer for multiple queries to collect
  66. Sleep(10);
  67. DBStruct* data = (DBStruct*)str;
  68. database.RunAsyncQueries(data->queryid);
  69. delete data;
  70. THREAD_RETURN(NULL);
  71. }
  72. #endif
  73. Database::Database()
  74. {
  75. InitVars();
  76. }
  77. bool Database::Init(bool silentLoad) {
  78. char host[200], user[200], passwd[200], database[200];
  79. int32 port=0;
  80. bool compression = false;
  81. bool items[6] = {false, false, false, false, false, false};
  82. const char* exampleIni[] = { "[Database]", "host = localhost", "user = root", "password = pass", "database = dbname", "### --- Assure each parameter is on a new line!" };
  83. if(!ReadDBINI(host, user, passwd, database, port, compression, items)) {
  84. //exit(1);
  85. return false;
  86. }
  87. if (!items[0] || !items[1] || !items[2] || !items[3])
  88. {
  89. LogWrite(DATABASE__ERROR, 0, "DB", "Database file %s is incomplete.", DB_INI_FILE);
  90. int i;
  91. for (i = 0; i < 4; i++)
  92. {
  93. if ( !items[i] )
  94. LogWrite(DATABASE__ERROR, 0, "DB", "Could not find parameter %s", exampleIni[i]);
  95. }
  96. LogWrite(DATABASE__ERROR, 0, "DB", "Example File:");
  97. int length = sizeof exampleIni / sizeof exampleIni[0];
  98. for(i=0;i<length;i++)
  99. LogWrite(DATABASE__ERROR, 0, "DB", "%s", exampleIni[i]);
  100. //exit (1);
  101. return false;
  102. }
  103. int32 errnum = 0;
  104. char errbuf[MYSQL_ERRMSG_SIZE];
  105. if (!Open(host, user, passwd, database,port, &errnum, errbuf))
  106. {
  107. LogWrite(DATABASE__ERROR, 0, "DB", "Failed to connect to database: Error: %s", errbuf);
  108. HandleMysqlError(errnum);
  109. //exit(1);
  110. return false;
  111. }
  112. else
  113. {
  114. if (!silentLoad)
  115. LogWrite(DATABASE__INFO, 0, "DB", "Using database '%s' at %s", database, host);
  116. }
  117. return true;
  118. }
  119. map<int16, int16> Database::GetVersions(){
  120. map<int16, int16> opcodes;
  121. Query query;
  122. MYSQL_ROW row;
  123. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "select distinct version_range1, version_range2 from opcodes");
  124. while(result && (row = mysql_fetch_row(result))){
  125. if(row[0] && row[1])
  126. opcodes[atoi(row[0])] = atoi(row[1]);
  127. }
  128. return opcodes;
  129. }
  130. map<string, uint16> Database::GetOpcodes(int16 version){
  131. map<string, uint16> opcodes;
  132. Query query;
  133. MYSQL_ROW row;
  134. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "select name, opcode from opcodes where %i between version_range1 and version_range2 order by version_range1, id", version);
  135. while(result && (row = mysql_fetch_row(result))){
  136. opcodes[row[0]] = atoi(row[1]);
  137. }
  138. return opcodes;
  139. }
  140. void Database::HandleMysqlError(int32 errnum) {
  141. switch(errnum) {
  142. case 0:
  143. break;
  144. case 1045: // Access Denied
  145. case 2001: {
  146. AddEQEMuError(EQEMuError_Mysql_1405, true);
  147. break;
  148. }
  149. case 2003: { // Unable to connect
  150. AddEQEMuError(EQEMuError_Mysql_2003, true);
  151. break;
  152. }
  153. case 2005: { // Unable to connect
  154. AddEQEMuError(EQEMuError_Mysql_2005, true);
  155. break;
  156. }
  157. case 2007: { // Unable to connect
  158. AddEQEMuError(EQEMuError_Mysql_2007, true);
  159. break;
  160. }
  161. }
  162. }
  163. void Database::InitVars() {
  164. }
  165. Database::~Database()
  166. {
  167. #ifdef WORLD
  168. DBQueryMutex.writelock(__FUNCTION__, __LINE__);
  169. activeQuerySessions.clear();
  170. DBQueryMutex.releasewritelock(__FUNCTION__, __LINE__);
  171. DBAsyncMutex.writelock();
  172. continueAsync = false;
  173. map<int32, deque<Query*>>::iterator itr;
  174. for (itr = asyncQueries.begin(); itr != asyncQueries.end(); itr++)
  175. {
  176. asyncQueriesMutex[itr->first]->writelock();
  177. deque<Query*> queries = itr->second;
  178. while (queries.size() > 0)
  179. {
  180. Query* cur = queries.front();
  181. queries.pop_front();
  182. safe_delete(cur);
  183. }
  184. asyncQueriesMutex[itr->first]->releasewritelock();
  185. Mutex* mutex = asyncQueriesMutex[itr->first];
  186. asyncQueriesMutex.erase(itr->first);
  187. safe_delete(mutex);
  188. }
  189. asyncQueries.clear();
  190. asyncQueriesMutex.clear();
  191. DBAsyncMutex.releasewritelock();
  192. PurgeDBInstances();
  193. #endif
  194. }
  195. #ifdef WORLD
  196. void Query::AddQueryAsync(int32 queryID, Database* db, QUERY_TYPE type, const char* format, ...) {
  197. in_type = type;
  198. va_list args;
  199. va_start(args, format);
  200. #ifdef WIN32
  201. char* buffer;
  202. int buf_len = _vscprintf(format, args) + 1;
  203. buffer = new char[buf_len];
  204. vsprintf(buffer, format, args);
  205. #else
  206. char* buffer;
  207. int buf_len;
  208. va_list argcopy;
  209. va_copy(argcopy, args);
  210. buf_len = vsnprintf(NULL, 0, format, argcopy) + 1;
  211. va_end(argcopy);
  212. buffer = new char[buf_len];
  213. vsnprintf(buffer, buf_len, format, args);
  214. #endif
  215. va_end(args);
  216. query = string(buffer);
  217. Query* asyncQuery = new Query(this, queryID);
  218. safe_delete_array(buffer);
  219. db->AddAsyncQuery(asyncQuery);
  220. }
  221. void Query::RunQueryAsync(Database* db) {
  222. db->RunQuery(query.c_str(), query.length(), errbuf, &result, affected_rows, last_insert_id, &errnum, retry);
  223. }
  224. #endif
  225. MYSQL_RES* Query::RunQuery2(QUERY_TYPE type, const char* format, ...){
  226. va_list args;
  227. va_start( args, format );
  228. #ifdef WIN32
  229. char * buffer;
  230. int buf_len = _vscprintf( format, args ) + 1;
  231. buffer = new char[buf_len];
  232. vsprintf( buffer, format, args );
  233. #else
  234. char* buffer;
  235. int buf_len;
  236. va_list argcopy;
  237. va_copy(argcopy, args);
  238. buf_len = vsnprintf(NULL, 0, format, argcopy) + 1;
  239. va_end(argcopy);
  240. buffer = new char[buf_len];
  241. vsnprintf(buffer, buf_len, format, args);
  242. #endif
  243. va_end(args);
  244. query = string(buffer);
  245. safe_delete_array( buffer );
  246. return RunQuery2(query.c_str(), type);
  247. }
  248. MYSQL_RES* Query::RunQuery2(string in_query, QUERY_TYPE type){
  249. switch(type){
  250. case Q_SELECT:
  251. break;
  252. case Q_DBMS:
  253. case Q_REPLACE:
  254. case Q_DELETE:
  255. case Q_UPDATE:
  256. safe_delete(affected_rows);
  257. affected_rows = new int32;
  258. break;
  259. case Q_INSERT:
  260. safe_delete(last_insert_id);
  261. last_insert_id = new int32;
  262. }
  263. if(result){
  264. if(!multiple_results)
  265. multiple_results = new vector<MYSQL_RES*>();
  266. multiple_results->push_back(result);
  267. }
  268. query = in_query;
  269. #if defined WORLD && defined _DEBUG
  270. if (type == Q_UPDATE || type == Q_INSERT || type == Q_DELETE || type == Q_REPLACE)
  271. {
  272. char* filteredTables[] = { " characters", " character_", " `character_", " statistics", " variables", " char_colors", " `guild", " bugs" };
  273. bool match = false;
  274. for (int i = 0; i < sizeof(filteredTables) / sizeof(filteredTables[0]); i++)
  275. {
  276. if (query.find(filteredTables[i]) != std::string::npos) {
  277. match = true;
  278. break;
  279. }
  280. }
  281. try
  282. {
  283. if (!match)
  284. {
  285. FILE* pFile;
  286. pFile = fopen("sql_updates.sql", "a+");
  287. fwrite(query.c_str(), 1, query.length(), pFile);
  288. fwrite(";", sizeof(char), 1, pFile);
  289. fwrite("\n", sizeof(char), 1, pFile);
  290. fclose(pFile);
  291. }
  292. }
  293. catch (...) {}
  294. }
  295. #endif
  296. database.RunQuery(query.c_str(), query.length(), errbuf, &result, affected_rows, last_insert_id, &errnum, retry);
  297. return result;
  298. }
  299. #ifdef WORLD
  300. void Database::RunAsyncQueries(int32 queryid)
  301. {
  302. Database* asyncdb = FindFreeInstance();
  303. DBAsyncMutex.writelock();
  304. map<int32, deque<Query*>>::iterator itr = asyncQueries.find(queryid);
  305. if (itr == asyncQueries.end())
  306. {
  307. DBAsyncMutex.releasewritelock();
  308. return;
  309. }
  310. asyncQueriesMutex[queryid]->writelock();
  311. deque<Query*> queries;
  312. while (itr->second.size())
  313. {
  314. Query* cur = itr->second.front();
  315. queries.push_back(cur);
  316. itr->second.pop_front();
  317. }
  318. itr->second.clear();
  319. asyncQueries.erase(itr);
  320. DBAsyncMutex.releasewritelock();
  321. asyncQueriesMutex[queryid]->releasewritelock();
  322. int32 count = 0;
  323. while (queries.size() > 0)
  324. {
  325. Query* cur = queries.front();
  326. cur->RunQueryAsync(asyncdb);
  327. this->RemoveActiveQuery(cur);
  328. queries.pop_front();
  329. safe_delete(cur);
  330. }
  331. FreeDBInstance(asyncdb);
  332. bool isActive = IsActiveQuery(queryid);
  333. if (isActive)
  334. {
  335. continueAsync = true;
  336. DBStruct* tmp = new DBStruct;
  337. tmp->queryid = queryid;
  338. #ifdef WIN32
  339. _beginthread(DBAsyncQueries, 0, (void*)tmp);
  340. #else
  341. pthread_t t1;
  342. pthread_create(&t1, NULL, DBAsyncQueries, (void*)tmp);
  343. pthread_detach(t1);
  344. #endif
  345. }
  346. }
  347. void Database::AddAsyncQuery(Query* query)
  348. {
  349. DBAsyncMutex.writelock();
  350. map<int32, Mutex*>::iterator mutexItr = asyncQueriesMutex.find(query->GetQueryID());
  351. if (mutexItr == asyncQueriesMutex.end())
  352. {
  353. Mutex* queryMutex = new Mutex();
  354. queryMutex->SetName("AsyncQuery" + query->GetQueryID());
  355. asyncQueriesMutex.insert(make_pair(query->GetQueryID(), queryMutex));
  356. }
  357. map<int32, deque<Query*>>::iterator itr = asyncQueries.find(query->GetQueryID());
  358. asyncQueriesMutex[query->GetQueryID()]->writelock();
  359. if ( itr != asyncQueries.end())
  360. itr->second.push_back(query);
  361. else
  362. {
  363. deque<Query*> queue;
  364. queue.push_back(query);
  365. asyncQueries.insert(make_pair(query->GetQueryID(), queue));
  366. }
  367. AddActiveQuery(query);
  368. asyncQueriesMutex[query->GetQueryID()]->releasewritelock();
  369. DBAsyncMutex.releasewritelock();
  370. bool isActive = IsActiveQuery(query->GetQueryID(), query);
  371. if (!isActive)
  372. {
  373. continueAsync = true;
  374. DBStruct* tmp = new DBStruct;
  375. tmp->queryid = query->GetQueryID();
  376. #ifdef WIN32
  377. _beginthread(DBAsyncQueries, 0, (void*)tmp);
  378. #else
  379. pthread_t t1;
  380. pthread_create(&t1, NULL, DBAsyncQueries, (void*)tmp);
  381. pthread_detach(t1);
  382. #endif
  383. }
  384. }
  385. Database* Database::FindFreeInstance()
  386. {
  387. Database* db_inst = 0;
  388. map<Database*, bool>::iterator itr;
  389. DBInstanceMutex.writelock(__FUNCTION__, __LINE__);
  390. for (itr = dbInstances.begin(); itr != dbInstances.end(); itr++) {
  391. if (!itr->second)
  392. {
  393. db_inst = itr->first;
  394. itr->second = true;
  395. break;
  396. }
  397. }
  398. if (!db_inst)
  399. {
  400. WorldDatabase* tmp = new WorldDatabase();
  401. db_inst = (Database*)tmp;
  402. tmp->Init();
  403. tmp->ConnectNewDatabase();
  404. dbInstances.insert(make_pair(db_inst, true));
  405. }
  406. DBInstanceMutex.releasewritelock(__FUNCTION__, __LINE__);
  407. return db_inst;
  408. }
  409. void Database::PurgeDBInstances()
  410. {
  411. map<Database*, bool>::iterator itr;
  412. DBInstanceMutex.writelock(__FUNCTION__, __LINE__);
  413. for (itr = dbInstances.begin(); itr != dbInstances.end(); itr++) {
  414. Database* tmpInst = itr->first;
  415. safe_delete(tmpInst);
  416. }
  417. dbInstances.clear();
  418. DBInstanceMutex.releasewritelock(__FUNCTION__, __LINE__);
  419. }
  420. void Database::PingAsyncDatabase()
  421. {
  422. map<Database*, bool>::iterator itr;
  423. DBInstanceMutex.readlock(__FUNCTION__, __LINE__);
  424. for (itr = dbInstances.begin(); itr != dbInstances.end(); itr++) {
  425. Database* tmpInst = itr->first;
  426. tmpInst->ping();
  427. }
  428. DBInstanceMutex.releasereadlock(__FUNCTION__, __LINE__);
  429. }
  430. void Database::FreeDBInstance(Database* cur)
  431. {
  432. DBInstanceMutex.writelock(__FUNCTION__, __LINE__);
  433. dbInstances[cur] = false;
  434. DBInstanceMutex.releasewritelock(__FUNCTION__, __LINE__);
  435. }
  436. void Database::RemoveActiveQuery(Query* query)
  437. {
  438. DBQueryMutex.writelock(__FUNCTION__, __LINE__);
  439. vector<Query*>::iterator itr;
  440. for (itr = activeQuerySessions.begin(); itr != activeQuerySessions.end(); itr++)
  441. {
  442. Query* curQuery = *itr;
  443. if (query == curQuery)
  444. {
  445. activeQuerySessions.erase(itr);
  446. break;
  447. }
  448. }
  449. DBQueryMutex.releasewritelock(__FUNCTION__, __LINE__);
  450. }
  451. void Database::AddActiveQuery(Query* query)
  452. {
  453. DBQueryMutex.writelock(__FUNCTION__, __LINE__);
  454. activeQuerySessions.push_back(query);
  455. DBQueryMutex.releasewritelock(__FUNCTION__, __LINE__);
  456. }
  457. bool Database::IsActiveQuery(int32 id, Query* skip)
  458. {
  459. bool isActive = false;
  460. DBQueryMutex.readlock(__FUNCTION__, __LINE__);
  461. vector<Query*>::iterator itr;
  462. for (itr = activeQuerySessions.begin(); itr != activeQuerySessions.end(); itr++)
  463. {
  464. Query* query = *itr;
  465. if (query == skip)
  466. continue;
  467. if (query->GetQueryID() == id)
  468. {
  469. isActive = true;
  470. break;
  471. }
  472. }
  473. DBQueryMutex.releasereadlock(__FUNCTION__, __LINE__);
  474. return isActive;
  475. }
  476. #endif