PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/DataInterface.cpp

http://github.com/InfiniteRasa/Game-Server
C++ | 248 lines | 207 code | 31 blank | 10 comment | 18 complexity | 576f3b7ed020497a0848447619d6d566 MD5 | raw file
  1. #include<winsock2.h>
  2. #include<stdio.h>
  3. #include"DataInterface.h"
  4. DataInterfaceWorkerThread_t workerThread[WORKER_THREADS];
  5. struct
  6. {
  7. sint8* dbHost;
  8. sint8* dbUser;
  9. sint8* dbName;
  10. sint8* dbPass;
  11. sint32 dbPort;
  12. } dbConInfo;
  13. struct GSInfo
  14. {
  15. sint8* ServerIP;
  16. sint32 ServerIPHex;
  17. sint32 ServerPort;
  18. sint32 ID;
  19. };
  20. struct
  21. {
  22. MYSQL* mysql_as;
  23. sint8* dbHost;
  24. sint8* dbUser;
  25. sint8* dbName;
  26. sint8* dbPass;
  27. sint32 dbPort;
  28. struct GSInfo gsInfo;
  29. } AuthInfo;
  30. MYSQL* _DataInterface_gs_connect()
  31. {
  32. MYSQL* dbHandle;
  33. dbHandle = mysql_init(0);
  34. MYSQL* dbHandleErr = dbHandle;
  35. dbHandle = mysql_real_connect(dbHandle, dbConInfo.dbHost,
  36. dbConInfo.dbUser, dbConInfo.dbPass,
  37. dbConInfo.dbName, dbConInfo.dbPort, 0, 0);
  38. if( dbHandle == 0 )
  39. {
  40. printf("GS Mysql: ");
  41. puts(mysql_error(dbHandleErr));
  42. Sleep(1000*60);
  43. return 0;
  44. }
  45. // set charset utf8
  46. mysql_set_character_set(dbHandle, "utf8");
  47. return dbHandle;
  48. }
  49. MYSQL* _DataInterface_as_connect()
  50. {
  51. MYSQL* dbHandle;
  52. dbHandle = mysql_init(0);
  53. MYSQL* dbHandleErr = dbHandle;
  54. dbHandle = mysql_real_connect(dbHandle, AuthInfo.dbHost,
  55. AuthInfo.dbUser, AuthInfo.dbPass,
  56. AuthInfo.dbName, AuthInfo.dbPort, 0, 0);
  57. if( dbHandle == 0 )
  58. {
  59. printf("AS Mysql: ");
  60. puts(mysql_error(dbHandleErr));
  61. Sleep(1000*60);
  62. return 0;
  63. }
  64. // set charset utf8
  65. mysql_set_character_set(dbHandle, "utf8");
  66. return dbHandle;
  67. }
  68. void DataInterface_queueJob(void *jobData, void *workCallback, void *doneCallback, void *param)
  69. {
  70. DataInterfaceWorkerThread_t *wt = workerThread+0;
  71. // diJob_getCharacterPreviewInfo_t *job, void *cb, void *param
  72. sint32 nwi = (wt->queueWriteIndex+1)%WORKER_QUEUELENGTH;
  73. if( wt->queueReadIndex == nwi )
  74. while( wt->queueReadIndex != nwi ) Sleep(10);
  75. // append job
  76. wt->jobQueue[wt->queueWriteIndex].jobData = jobData;
  77. wt->jobQueue[wt->queueWriteIndex].param = param;
  78. wt->jobQueue[wt->queueWriteIndex].workCallback = (void (__cdecl *)(MYSQL *,void *,void *,void *))workCallback;
  79. wt->jobQueue[wt->queueWriteIndex].doneCallBack = doneCallback;
  80. wt->queueWriteIndex = nwi;
  81. }
  82. sint32 _DataInterface_work(DataInterfaceWorkerThread_t *wt)
  83. {
  84. while( 1 )
  85. {
  86. while( wt->queueReadIndex == wt->queueWriteIndex )
  87. Sleep(1);
  88. // process job
  89. wt->jobQueue[wt->queueReadIndex].workCallback(wt->dbCon, wt->jobQueue[wt->queueReadIndex].jobData, wt->jobQueue[wt->queueReadIndex].doneCallBack, wt->jobQueue[wt->queueReadIndex].param);
  90. wt->queueReadIndex = (wt->queueReadIndex+1)%WORKER_QUEUELENGTH;
  91. }
  92. return 0;
  93. }
  94. void _DataInterface_initWorkerThread(sint32 index)
  95. {
  96. DataInterfaceWorkerThread_t *wt = workerThread+index;
  97. wt->dbCon = _DataInterface_gs_connect();
  98. wt->queueReadIndex = 0;
  99. wt->queueWriteIndex = 0;
  100. //CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)_DataInterface_work, wt, 0, NULL);
  101. Thread::New(NULL, (THREAD_ROUTINE)_DataInterface_work, wt);
  102. }
  103. void DataInterface_init()
  104. {
  105. printf("Reading configuration file...\n\n");
  106. INIParser* Parser = new INIParser("config.ini");
  107. // Game Server Database
  108. dbConInfo.dbHost = _strdup(Parser->GetString("Game Server Database", "dbHost", "localhost").c_str());
  109. dbConInfo.dbPort = Parser->GetInt("Game Server Database", "dbPort", 3306);
  110. dbConInfo.dbName = _strdup(Parser->GetString("Game Server Database", "dbName", "ir_gameserver").c_str());
  111. dbConInfo.dbUser = _strdup(Parser->GetString("Game Server Database", "dbUser", "root").c_str());
  112. dbConInfo.dbPass = _strdup(Parser->GetString("Game Server Database", "dbPass", "").c_str());
  113. // Auth Server Database
  114. AuthInfo.dbHost = _strdup(Parser->GetString("Auth Server Database", "dbHost", "localhost").c_str());
  115. AuthInfo.dbPort = Parser->GetInt("Auth Server Database", "dbPort", 3306);
  116. AuthInfo.dbName = _strdup(Parser->GetString("Auth Server Database", "dbName", "ir_authentication").c_str());
  117. AuthInfo.dbUser = _strdup(Parser->GetString("Auth Server Database", "dbUser", "root").c_str());
  118. AuthInfo.dbPass = _strdup(Parser->GetString("Auth Server Database", "dbPass", "").c_str());
  119. // Game Server options
  120. AuthInfo.gsInfo.ServerIP = _strdup(Parser->GetString("Game Server", "IPAddress", "127.0.0.1").c_str());
  121. AuthInfo.gsInfo.ServerIPHex = DataInterface_IPtoHex();
  122. AuthInfo.gsInfo.ServerPort = Parser->GetInt("Game Server", "Port", 8001);
  123. AuthInfo.gsInfo.ID = Parser->GetInt("Game Server", "ID", 234);
  124. delete Parser;
  125. printf(" GSDB Host: %s\n", dbConInfo.dbHost);
  126. printf(" GSDB Port: %d\n", dbConInfo.dbPort);
  127. printf(" GSDB Name: %s\n", dbConInfo.dbName);
  128. printf(" GSDB User: %s\n", dbConInfo.dbUser);
  129. printf(" GSDB Pass: %s\n\n", dbConInfo.dbPass);
  130. printf(" ASDB Host: %s\n", AuthInfo.dbHost);
  131. printf(" ASDB Port: %d\n", AuthInfo.dbPort);
  132. printf(" ASDB Name: %s\n", AuthInfo.dbName);
  133. printf(" ASDB User: %s\n", AuthInfo.dbUser);
  134. printf(" ASDB Pass: %s\n\n", AuthInfo.dbPass);
  135. printf(" Server ID: %d\n", AuthInfo.gsInfo.ID);
  136. printf(" Server IP: %s\n", AuthInfo.gsInfo.ServerIP);
  137. printf(" Server Port: %d\n\n", AuthInfo.gsInfo.ServerPort);
  138. printf("Connecting to Game Server Database...\n");
  139. MYSQL *mysql_gs = _DataInterface_gs_connect();
  140. if( !mysql_gs ) { ExitProcess(-1); }
  141. mysql_close(mysql_gs);
  142. printf("Connecting to Auth Server Database...\n");
  143. AuthInfo.mysql_as = _DataInterface_as_connect();
  144. if( !AuthInfo.mysql_as ) { ExitProcess(-1); }
  145. for(sint32 i=0; i<WORKER_THREADS; i++)
  146. _DataInterface_initWorkerThread(i);
  147. }
  148. void* DataInterface_allocJob(sint32 size)
  149. {
  150. return malloc(size);
  151. }
  152. void DataInterface_freeJob(void *job)
  153. {
  154. free(job);
  155. }
  156. void DataInterface_registerServerForAuth()
  157. {
  158. sint8 queryText1[1024];
  159. wsprintf(queryText1, "SELECT server_id FROM game_servers WHERE host='%s' AND port='%i' AND server_id='%i' LIMIT 1", AuthInfo.gsInfo.ServerIP, AuthInfo.gsInfo.ServerPort, AuthInfo.gsInfo.ID);
  160. if( mysql_query(AuthInfo.mysql_as, queryText1) )
  161. {
  162. printf("MySQL: Error checking server entry\n");
  163. Sleep(1000*60);
  164. return;
  165. }
  166. MYSQL_RES *dbResult = mysql_store_result(AuthInfo.mysql_as);
  167. MYSQL_ROW dbRow;
  168. dbRow = mysql_fetch_row(dbResult);
  169. if (dbRow != NULL)
  170. {
  171. printf("MySQL: The server is already registered in the database\n");
  172. return;
  173. }
  174. sint8 queryText2[1024];
  175. wsprintf(queryText2, "INSERT INTO game_servers ("
  176. "`server_id`,`host`,`port`,`age_limit`,`pk_flag`,`current_users`,`max_users`,`status`,`static`)"
  177. " VALUES(%i,'%s',%i,18,0,0,10,1,1);", AuthInfo.gsInfo.ID, AuthInfo.gsInfo.ServerIP, AuthInfo.gsInfo.ServerPort);
  178. if( mysql_query(AuthInfo.mysql_as, queryText2) )
  179. {
  180. printf("MySQL: Error registering the server in the database\n");
  181. }
  182. else
  183. printf("MySQL: The server has been registered in the database\n");
  184. return;
  185. }
  186. // 127.0.0.1 -> 1.0.0.127 -> 0x0100007F
  187. uint32 DataInterface_IPtoHex()
  188. {
  189. uint32 a, b, c, d, ret;
  190. sscanf(AuthInfo.gsInfo.ServerIP, "%d.%d.%d.%d", &a, &b, &c, &d);
  191. ret = a | (b<<8) | (c<<16) | (d<<24);
  192. return ret;
  193. }
  194. uint32 DataInterface_getMyIP()
  195. {
  196. return (uint32)AuthInfo.gsInfo.ServerIPHex;
  197. }
  198. sint32 DataInterface_QuerySession(uint32 ID1, uint32 ID2, authSessionInfo_t *asiOut)
  199. {
  200. sint8 queryText[1024];
  201. wsprintf(queryText, "SELECT account,uid FROM sessions WHERE session_id1='%u' AND session_id2='%u' LIMIT 1", ID1, ID2);
  202. if( mysql_query(AuthInfo.mysql_as, queryText) )
  203. {
  204. printf("Error in query DataInterface_QuerySession\n");
  205. return 0;
  206. }
  207. MYSQL_RES *dbResult = mysql_store_result(AuthInfo.mysql_as);
  208. MYSQL_ROW dbRow;
  209. dbRow = mysql_fetch_row(dbResult);
  210. if (dbRow == NULL) { return 0; }
  211. strcpy(asiOut->Accountname, (sint8*)dbRow[0]);
  212. asiOut->ID = atoi(dbRow[1]);
  213. printf("User ID: %u\n", asiOut->ID);
  214. asiOut->UID1 = ID1;
  215. asiOut->UID2 = ID2;
  216. return 1;
  217. }