database.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. }
  279. }
  280. try
  281. {
  282. if (!match)
  283. {
  284. FILE* pFile;
  285. pFile = fopen("sql_updates.sql", "a+");
  286. fwrite(query.c_str(), 1, query.length(), pFile);
  287. fwrite(";", sizeof(char), 1, pFile);
  288. fwrite("\n", sizeof(char), 1, pFile);
  289. fclose(pFile);
  290. }
  291. }
  292. catch (...) {}
  293. }
  294. #endif
  295. database.RunQuery(query.c_str(), query.length(), errbuf, &result, affected_rows, last_insert_id, &errnum, retry);
  296. return result;
  297. }
  298. #ifdef WORLD
  299. void Database::RunAsyncQueries(int32 queryid)
  300. {
  301. Database* asyncdb = FindFreeInstance();
  302. DBAsyncMutex.writelock();
  303. map<int32, deque<Query*>>::iterator itr = asyncQueries.find(queryid);
  304. if (itr == asyncQueries.end())
  305. {
  306. DBAsyncMutex.releasewritelock();
  307. return;
  308. }
  309. asyncQueriesMutex[queryid]->writelock();
  310. deque<Query*> queries;
  311. while (itr->second.size())
  312. {
  313. Query* cur = itr->second.front();
  314. queries.push_back(cur);
  315. itr->second.pop_front();
  316. }
  317. itr->second.clear();
  318. asyncQueries.erase(itr);
  319. DBAsyncMutex.releasewritelock();
  320. asyncQueriesMutex[queryid]->releasewritelock();
  321. int32 count = 0;
  322. while (queries.size() > 0)
  323. {
  324. Query* cur = queries.front();
  325. cur->RunQueryAsync(asyncdb);
  326. this->RemoveActiveQuery(cur);
  327. queries.pop_front();
  328. safe_delete(cur);
  329. }
  330. FreeDBInstance(asyncdb);
  331. bool isActive = IsActiveQuery(queryid);
  332. if (isActive)
  333. {
  334. continueAsync = true;
  335. DBStruct* tmp = new DBStruct;
  336. tmp->queryid = queryid;
  337. #ifdef WIN32
  338. _beginthread(DBAsyncQueries, 0, (void*)tmp);
  339. #else
  340. pthread_t t1;
  341. pthread_create(&t1, NULL, DBAsyncQueries, (void*)tmp);
  342. pthread_detach(t1);
  343. #endif
  344. }
  345. }
  346. void Database::AddAsyncQuery(Query* query)
  347. {
  348. DBAsyncMutex.writelock();
  349. map<int32, Mutex*>::iterator mutexItr = asyncQueriesMutex.find(query->GetQueryID());
  350. if (mutexItr == asyncQueriesMutex.end())
  351. {
  352. Mutex* queryMutex = new Mutex();
  353. queryMutex->SetName("AsyncQuery" + query->GetQueryID());
  354. asyncQueriesMutex.insert(make_pair(query->GetQueryID(), queryMutex));
  355. }
  356. map<int32, deque<Query*>>::iterator itr = asyncQueries.find(query->GetQueryID());
  357. asyncQueriesMutex[query->GetQueryID()]->writelock();
  358. if ( itr != asyncQueries.end())
  359. itr->second.push_back(query);
  360. else
  361. {
  362. deque<Query*> queue;
  363. queue.push_back(query);
  364. asyncQueries.insert(make_pair(query->GetQueryID(), queue));
  365. }
  366. AddActiveQuery(query);
  367. asyncQueriesMutex[query->GetQueryID()]->releasewritelock();
  368. DBAsyncMutex.releasewritelock();
  369. bool isActive = IsActiveQuery(query->GetQueryID(), query);
  370. if (!isActive)
  371. {
  372. continueAsync = true;
  373. DBStruct* tmp = new DBStruct;
  374. tmp->queryid = query->GetQueryID();
  375. #ifdef WIN32
  376. _beginthread(DBAsyncQueries, 0, (void*)tmp);
  377. #else
  378. pthread_t t1;
  379. pthread_create(&t1, NULL, DBAsyncQueries, (void*)tmp);
  380. pthread_detach(t1);
  381. #endif
  382. }
  383. }
  384. Database* Database::FindFreeInstance()
  385. {
  386. Database* db_inst = 0;
  387. map<Database*, bool>::iterator itr;
  388. DBInstanceMutex.writelock(__FUNCTION__, __LINE__);
  389. for (itr = dbInstances.begin(); itr != dbInstances.end(); itr++) {
  390. if (!itr->second)
  391. {
  392. db_inst = itr->first;
  393. itr->second = true;
  394. break;
  395. }
  396. }
  397. if (!db_inst)
  398. {
  399. WorldDatabase* tmp = new WorldDatabase();
  400. db_inst = (Database*)tmp;
  401. tmp->Init();
  402. tmp->ConnectNewDatabase();
  403. dbInstances.insert(make_pair(db_inst, true));
  404. }
  405. DBInstanceMutex.releasewritelock(__FUNCTION__, __LINE__);
  406. return db_inst;
  407. }
  408. void Database::PurgeDBInstances()
  409. {
  410. map<Database*, bool>::iterator itr;
  411. DBInstanceMutex.writelock(__FUNCTION__, __LINE__);
  412. for (itr = dbInstances.begin(); itr != dbInstances.end(); itr++) {
  413. Database* tmpInst = itr->first;
  414. safe_delete(tmpInst);
  415. }
  416. dbInstances.clear();
  417. DBInstanceMutex.releasewritelock(__FUNCTION__, __LINE__);
  418. }
  419. void Database::FreeDBInstance(Database* cur)
  420. {
  421. DBInstanceMutex.writelock(__FUNCTION__, __LINE__);
  422. dbInstances[cur] = false;
  423. DBInstanceMutex.releasewritelock(__FUNCTION__, __LINE__);
  424. }
  425. void Database::RemoveActiveQuery(Query* query)
  426. {
  427. DBQueryMutex.writelock(__FUNCTION__, __LINE__);
  428. vector<Query*>::iterator itr;
  429. for (itr = activeQuerySessions.begin(); itr != activeQuerySessions.end(); itr++)
  430. {
  431. Query* curQuery = *itr;
  432. if (query == curQuery)
  433. {
  434. activeQuerySessions.erase(itr);
  435. break;
  436. }
  437. }
  438. DBQueryMutex.releasewritelock(__FUNCTION__, __LINE__);
  439. }
  440. void Database::AddActiveQuery(Query* query)
  441. {
  442. DBQueryMutex.writelock(__FUNCTION__, __LINE__);
  443. activeQuerySessions.push_back(query);
  444. DBQueryMutex.releasewritelock(__FUNCTION__, __LINE__);
  445. }
  446. bool Database::IsActiveQuery(int32 id, Query* skip)
  447. {
  448. bool isActive = false;
  449. DBQueryMutex.readlock(__FUNCTION__, __LINE__);
  450. vector<Query*>::iterator itr;
  451. for (itr = activeQuerySessions.begin(); itr != activeQuerySessions.end(); itr++)
  452. {
  453. Query* query = *itr;
  454. if (query == skip)
  455. continue;
  456. if (query->GetQueryID() == id)
  457. {
  458. isActive = true;
  459. break;
  460. }
  461. }
  462. DBQueryMutex.releasereadlock(__FUNCTION__, __LINE__);
  463. return isActive;
  464. }
  465. #endif