client.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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. */
  6. #include "../common/debug.h"
  7. #ifdef WIN32
  8. #define WIN32_LEAN_AND_MEAN
  9. #include <windows.h>
  10. #include <winsock.h>
  11. #include <process.h>
  12. #else
  13. #include <sys/socket.h>
  14. #include <netinet/in.h>
  15. #include <arpa/inet.h>
  16. #include <unistd.h>
  17. #endif
  18. #include <string.h>
  19. #include <iomanip>
  20. #include <stdlib.h>
  21. #include <assert.h>
  22. #include "net.h"
  23. #include "client.h"
  24. #include "../common/EQStream.h"
  25. #include "../common/packet_dump.h"
  26. #include "../common/packet_functions.h"
  27. #include "../common/emu_opcodes.h"
  28. #include "../common/MiscFunctions.h"
  29. #include "LWorld.h"
  30. #include "LoginDatabase.h"
  31. #include "../common/ConfigReader.h"
  32. #include "../common/Log.h"
  33. extern NetConnection net;
  34. extern LWorldList world_list;
  35. extern ClientList client_list;
  36. extern LoginDatabase database;
  37. extern map<int16,OpcodeManager*>EQOpcodeManager;
  38. extern ConfigReader configReader;
  39. using namespace std;
  40. Client::Client(EQStream* ieqnc) {
  41. eqnc = ieqnc;
  42. ip = eqnc->GetrIP();
  43. port = ntohs(eqnc->GetrPort());
  44. account_id = 0;
  45. lsadmin = 0;
  46. worldadmin = 0;
  47. lsstatus = 0;
  48. version = 0;
  49. kicked = false;
  50. verified = false;
  51. memset(bannedreason, 0, sizeof(bannedreason));
  52. //worldresponse_timer = new Timer(10000);
  53. //worldresponse_timer->Disable();
  54. memset(key,0,10);
  55. LoginMode = None;
  56. num_updates = 0;
  57. updatetimer = new Timer(500);
  58. updatelisttimer = new Timer(10000);
  59. //keepalive = new Timer(5000);
  60. //logintimer = new Timer(500); // Give time for the servers to send updates
  61. //keepalive->Start();
  62. //updatetimer->Start();
  63. //logintimer->Disable();
  64. disconnectTimer = 0;
  65. memset(ClientSession,0,25);
  66. request_num = 0;
  67. login_account = 0;
  68. createRequest = 0;
  69. playWaitTimer = NULL;
  70. start = false;
  71. update_position = 0;
  72. update_packets = 0;
  73. needs_world_list = true;
  74. }
  75. Client::~Client() {
  76. //safe_delete(worldresponse_timer);
  77. //safe_delete(logintimer);
  78. safe_delete(login_account);
  79. eqnc->Close();
  80. safe_delete(playWaitTimer);
  81. safe_delete(createRequest);
  82. safe_delete(disconnectTimer);
  83. safe_delete(updatetimer);
  84. }
  85. bool Client::Process() {
  86. if(!start && !eqnc->CheckActive()){
  87. if(!playWaitTimer)
  88. playWaitTimer = new Timer(5000);
  89. else if(playWaitTimer->Check()){
  90. safe_delete(playWaitTimer);
  91. return false;
  92. }
  93. return true;
  94. }
  95. else if(!start){
  96. safe_delete(playWaitTimer);
  97. start = true;
  98. }
  99. if (disconnectTimer && disconnectTimer->Check())
  100. {
  101. safe_delete(disconnectTimer);
  102. getConnection()->SendDisconnect();
  103. }
  104. if (!kicked) {
  105. /************ Get all packets from packet manager out queue and process them ************/
  106. EQApplicationPacket *app = 0;
  107. /*if(logintimer && logintimer->Check())
  108. {
  109. database.LoadCharacters(GetLoginAccount());
  110. SendLoginAccepted();
  111. logintimer->Disable();
  112. }*/
  113. /*if(worldresponse_timer && worldresponse_timer->Check())
  114. {
  115. FatalError(WorldDownErrorMessage);
  116. worldresponse_timer->Disable();
  117. }*/
  118. if(playWaitTimer != NULL && playWaitTimer->Check ( ) )
  119. {
  120. SendPlayFailed(PLAY_ERROR_SERVER_TIMEOUT);
  121. safe_delete(playWaitTimer);
  122. }
  123. if(!needs_world_list && updatetimer && updatetimer->Check()){
  124. if(updatelisttimer && updatelisttimer->Check()){
  125. if(num_updates >= 180){ //30 minutes
  126. getConnection()->SendDisconnect();
  127. }
  128. else{
  129. vector<PacketStruct*>::iterator itr;
  130. if(update_packets){
  131. for(itr = update_packets->begin(); itr != update_packets->end(); itr++){
  132. safe_delete(*itr);
  133. }
  134. }
  135. safe_delete(update_packets);
  136. update_packets = world_list.GetServerListUpdate(version);
  137. }
  138. num_updates++;
  139. }
  140. else{
  141. if(!update_packets){
  142. update_packets = world_list.GetServerListUpdate(version);
  143. }
  144. else{
  145. if(update_position < update_packets->size()){
  146. QueuePacket(update_packets->at(update_position)->serialize());
  147. update_position++;
  148. }
  149. else
  150. update_position = 0;
  151. }
  152. }
  153. }
  154. while(app = eqnc->PopPacket())
  155. {
  156. switch(app->GetOpcode())
  157. {
  158. case OP_LoginRequestMsg:{
  159. // DumpPacket(app);
  160. PacketStruct* packet = configReader.getStruct("LS_LoginRequest", 1);
  161. if(packet->LoadPacketData(app->pBuffer,app->size)){
  162. version = packet->getType_int32_ByName("version");
  163. //[7:19 PM] Kirmmin: Well, I very quickly learned that unknown3 in LS_LoginRequest packet is the same value as cl_eqversion in the eq2_defaults.ini file.
  164. LogWrite(LOGIN__DEBUG, 0, "Login", "Client Version Provided: %i", version);
  165. if (EQOpcodeManager.count(GetOpcodeVersion(version)) == 0) {
  166. LogWrite(LOGIN__ERROR, 0, "Login", "Incompatible client version provided: %i", version);
  167. SendLoginDenied();
  168. return false;
  169. }
  170. if(EQOpcodeManager.count(GetOpcodeVersion(version)) > 0 && getConnection()){
  171. getConnection()->SetClientVersion(GetVersion());
  172. EQ2_16BitString username = packet->getType_EQ2_16BitString_ByName("username");
  173. EQ2_16BitString password = packet->getType_EQ2_16BitString_ByName("password");
  174. LoginAccount* acct = database.LoadAccount(username.data.c_str(),password.data.c_str(), net.IsAllowingAccountCreation());
  175. if(acct){
  176. Client* otherclient = client_list.FindByLSID(acct->getLoginAccountID());
  177. if(otherclient)
  178. otherclient->getConnection()->SendDisconnect(); // This person is already logged in, we don't want them logged in twice, kick the previous client as it might be a ghost
  179. }
  180. if(acct){
  181. SetAccountName(username.data.c_str());
  182. database.UpdateAccountIPAddress(acct->getLoginAccountID(), getConnection()->GetrIP());
  183. LogWrite(LOGIN__INFO, 0, "Login", "%s successfully logged in.", (char*)username.data.c_str());
  184. }
  185. else
  186. {
  187. if (username.size > 0)
  188. LogWrite(LOGIN__ERROR, 0, "Login", "%s login failed!", (char*)username.data.c_str());
  189. else
  190. LogWrite(LOGIN__ERROR, 0, "Login", "[UNKNOWN USER] login failed!");
  191. }
  192. if(!acct)
  193. SendLoginDenied();
  194. else{
  195. needs_world_list = true;
  196. SetLoginAccount(acct);
  197. SendLoginAccepted();
  198. }
  199. }
  200. else{
  201. cout << "Error bad version: " << version << endl;
  202. SendLoginDeniedBadVersion();
  203. }
  204. }
  205. else{
  206. cout << "Error loading LS_LoginRequest packet: \n";
  207. //DumpPacket(app);
  208. }
  209. safe_delete(packet);
  210. break;
  211. }
  212. case OP_KeymapLoadMsg:{
  213. // cout << "Received OP_KeymapNoneMsg\n";
  214. //dunno what this is for
  215. break;
  216. }
  217. case OP_AllWSDescRequestMsg:{
  218. SendWorldList();
  219. needs_world_list = false;
  220. database.LoadCharacters(GetLoginAccount(), GetVersion());
  221. SendCharList();
  222. break;
  223. }
  224. case OP_LsClientCrashlogReplyMsg:{
  225. // DumpPacket(app);
  226. SaveErrorsToDB(app, "Crash Log");
  227. break;
  228. }
  229. case OP_LsClientVerifylogReplyMsg:{
  230. // DumpPacket(app);
  231. SaveErrorsToDB(app, "Verify Log");
  232. break;
  233. }
  234. case OP_LsClientAlertlogReplyMsg:{
  235. // DumpPacket(app);
  236. SaveErrorsToDB(app, "Alert Log");
  237. break;
  238. }
  239. case OP_LsClientBaselogReplyMsg:{
  240. // DumpPacket(app);
  241. SaveErrorsToDB(app, "Base Log");
  242. break;
  243. }
  244. case OP_AllCharactersDescRequestMsg:{
  245. break;
  246. }
  247. case OP_CreateCharacterRequestMsg:{
  248. PacketStruct* packet = configReader.getStruct("CreateCharacter", GetVersion());
  249. playWaitTimer = new Timer ( 15000 );
  250. playWaitTimer->Start ( );
  251. cout << "Char Create Request From: " << GetAccountName() << "....";
  252. if(packet->LoadPacketData(app->pBuffer,app->size)){
  253. cout << "Loaded Successfully\n";
  254. packet->setDataByName("account_id",GetAccountID());
  255. LWorld* world_server = world_list.FindByID(packet->getType_int32_ByName("server_id"));
  256. if(!world_server)
  257. {
  258. cout << GetAccountName() << " attempted creation of character with an invalid server id of: " << packet->getType_int32_ByName("server_id") << "\n";
  259. break;
  260. }
  261. else
  262. {
  263. ServerPacket* outpack = new ServerPacket(ServerOP_CharacterCreate, app->size+sizeof(int16));
  264. int16 out_version = GetVersion();
  265. memcpy(outpack->pBuffer, &out_version, sizeof(int16));
  266. memcpy(outpack->pBuffer + sizeof(int16), app->pBuffer, app->size);
  267. uchar* tmp = outpack->pBuffer;
  268. tmp+=7;
  269. int32 account_id = GetAccountID();
  270. memcpy(tmp, &account_id, sizeof(int32));
  271. world_server->SendPacket(outpack);
  272. safe_delete(outpack);
  273. createRequest = packet;
  274. }
  275. }
  276. else{
  277. cout << "Error loading Char Create Packet!!\n";
  278. safe_delete(packet);
  279. }
  280. // world_list.SendWorldChanged(create.profile.server_id, false, this);
  281. break;
  282. }
  283. case OP_PlayCharacterRequestMsg:{
  284. int32 char_id = 0;
  285. int32 server_id = 0;
  286. PacketStruct* request = configReader.getStruct("LS_PlayRequest",GetVersion());
  287. if(request && request->LoadPacketData(app->pBuffer,app->size)){
  288. char_id = request->getType_int32_ByName("char_id");
  289. server_id = request->getType_int32_ByName("server_id");
  290. LWorld* world = world_list.FindByID(server_id);
  291. string name = database.GetCharacterName(char_id,server_id);
  292. if(world && name.length() > 0){
  293. pending_play_char_id = char_id;
  294. ServerPacket* outpack = new ServerPacket(ServerOP_UsertoWorldReq, sizeof(UsertoWorldRequest_Struct));
  295. UsertoWorldRequest_Struct* req = (UsertoWorldRequest_Struct*)outpack->pBuffer;
  296. req->char_id = char_id;
  297. req->lsaccountid = GetAccountID();
  298. req->worldid = server_id;
  299. struct in_addr in;
  300. in.s_addr = GetIP();
  301. strcpy(req->ip_address, inet_ntoa(in));
  302. world->SendPacket(outpack);
  303. delete outpack;
  304. safe_delete(playWaitTimer);
  305. playWaitTimer = new Timer ( 5000 );
  306. playWaitTimer->Start ( );
  307. }
  308. else{
  309. cout << GetAccountName() << " sent invalid Play Request: \n";
  310. SendPlayFailed(PLAY_ERROR_PROBLEM);
  311. DumpPacket(app);
  312. }
  313. }
  314. safe_delete(request);
  315. break;
  316. }
  317. case OP_DeleteCharacterRequestMsg:{
  318. PacketStruct* request = configReader.getStruct("LS_DeleteCharacterRequest", GetVersion());
  319. PacketStruct* response = configReader.getStruct("LS_DeleteCharacterResponse", GetVersion());
  320. if(request && response && request->LoadPacketData(app->pBuffer,app->size)){
  321. EQ2_16BitString name = request->getType_EQ2_16BitString_ByName("name");
  322. int32 acct_id = GetAccountID();
  323. int32 char_id = request->getType_int32_ByName("char_id");
  324. int32 server_id = request->getType_int32_ByName("server_id");
  325. if(database.VerifyDelete(acct_id, char_id, name.data.c_str())){
  326. response->setDataByName("response", 1);
  327. GetLoginAccount()->removeCharacter((char*)name.data.c_str());
  328. LWorld* world_server = world_list.FindByID(server_id);
  329. if(world_server != NULL)
  330. world_server->SendDeleteCharacter ( char_id , acct_id );
  331. }
  332. else
  333. response->setDataByName("response", 0);
  334. response->setDataByName("server_id", server_id);
  335. response->setDataByName("char_id", char_id);
  336. response->setDataByName("account_id", account_id);
  337. response->setMediumStringByName("name", (char*)name.data.c_str());
  338. response->setDataByName("max_characters", 10);
  339. EQ2Packet* outapp = response->serialize();
  340. QueuePacket(outapp);
  341. this->SendCharList();
  342. }
  343. safe_delete(request);
  344. safe_delete(response);
  345. break;
  346. }
  347. default: {
  348. const char* name = app->GetOpcodeName();
  349. if (name)
  350. LogWrite(OPCODE__DEBUG, 1, "Opcode", "%s Received %04X (%i)", name, app->GetRawOpcode(), app->GetRawOpcode());
  351. else
  352. LogWrite(OPCODE__DEBUG, 1, "Opcode", "Received %04X (%i)", app->GetRawOpcode(), app->GetRawOpcode());
  353. }
  354. }
  355. delete app;
  356. }
  357. }
  358. if (!eqnc->CheckActive()) {
  359. return false;
  360. }
  361. return true;
  362. }
  363. void Client::SaveErrorsToDB(EQApplicationPacket* app, char* type){
  364. int32 size = 0;
  365. memcpy(&size, app->pBuffer + sizeof(int32), sizeof(int32));
  366. size++;
  367. char* message = new char[size];
  368. memset(message, 0, size);
  369. z_stream zstream;
  370. int zerror = 0;
  371. zstream.next_in = app->pBuffer + 8;
  372. zstream.avail_in = app->size - 8;
  373. zstream.next_out = (BYTE*)message;
  374. zstream.avail_out = size;
  375. zstream.zalloc = Z_NULL;
  376. zstream.zfree = Z_NULL;
  377. zstream.opaque = Z_NULL;
  378. zerror = inflateInit( &zstream);
  379. if(zerror != Z_OK) {
  380. safe_delete_array(message);
  381. return;
  382. }
  383. zerror = inflate( &zstream, 0 );
  384. if(message && strlen(message) > 0)
  385. database.SaveClientLog(type, message, GetLoginAccount()->getLoginName(), GetVersion());
  386. safe_delete_array(message);
  387. }
  388. void Client::CharacterApproved(int32 server_id,int32 char_id)
  389. {
  390. if(createRequest && server_id == createRequest->getType_int32_ByName("server_id")){
  391. LWorld* world_server = world_list.FindByID(server_id);
  392. if(!world_server)
  393. return;
  394. PacketStruct* packet = configReader.getStruct("LS_CreateCharacterReply", GetVersion());
  395. if(packet){
  396. packet->setDataByName("account_id", GetAccountID());
  397. packet->setDataByName("unknown", 0xFFFFFFFF);
  398. packet->setDataByName("response", CREATESUCCESS_REPLY);
  399. packet->setMediumStringByName("name", (char*)createRequest->getType_EQ2_16BitString_ByName("name").data.c_str());
  400. EQ2Packet* outapp = packet->serialize();
  401. QueuePacket(outapp);
  402. safe_delete(packet);
  403. database.SaveCharacter(createRequest, GetLoginAccount(),char_id);
  404. // refresh characters for this account
  405. database.LoadCharacters(GetLoginAccount(), GetVersion());
  406. SendCharList();
  407. }
  408. }
  409. else{
  410. cout << GetAccountName() << " received invalid CharacterApproval from server: " << server_id << endl;
  411. }
  412. safe_delete(createRequest);
  413. }
  414. void Client::CharacterRejected(int8 reason_number)
  415. {
  416. PacketStruct* packet = configReader.getStruct("LS_CreateCharacterReply", GetVersion());
  417. if(createRequest && packet){
  418. packet->setDataByName("account_id", GetAccountID());
  419. int8 clientReasonNum = reason_number;
  420. // reason numbers change and instead of updating the world server
  421. // the login server will hold the up to date #'s
  422. /*
  423. switch(reason_number)
  424. {
  425. // these error codes seem to be removed now, they shutdown the client rather immediately
  426. // for now we are just going to play a joke on them and say they can't create a new character.
  427. case INVALIDRACE_REPLY:
  428. case INVALIDGENDER_REPLY:
  429. clientReasonNum = 8;
  430. break;
  431. case BADNAMELENGTH_REPLY:
  432. clientReasonNum = 9;
  433. break;
  434. case NAMEINVALID_REPLY:
  435. clientReasonNum = 10;
  436. break;
  437. case NAMEFILTER_REPLY:
  438. clientReasonNum = 11;
  439. break;
  440. case NAMETAKEN_REPLY:
  441. clientReasonNum = 12;
  442. break;
  443. case OVERLOADEDSERVER_REPLY:
  444. clientReasonNum = 13;
  445. break;
  446. }
  447. */
  448. packet->setDataByName("response", clientReasonNum);
  449. packet->setMediumStringByName("name", "");
  450. EQ2Packet* outapp = packet->serialize();
  451. QueuePacket(outapp);
  452. safe_delete(packet);
  453. }
  454. /*LS_CreateCharacterReply reply(GetAccountID(), reason_number, create.profile.name.data);
  455. EQ2Packet* outapp = reply.serialize();
  456. QueuePacket(outapp);
  457. create.Clear();*/
  458. }
  459. void Client::SendCharList(){
  460. /*PacketStruct* packet = configReader.getStruct("LS_CreateCharacterReply");
  461. packet->setDataByName("account_id", GetAccountID());
  462. packet->setDataByName("response", reason_number);
  463. packet->setDataByName("name", &create.profile.name);
  464. EQ2Packet* outapp = packet->serialize();
  465. QueuePacket(outapp);
  466. safe_delete(packet);*/
  467. LogWrite(LOGIN__INFO, 0, "Login", "[%s] sending character list.", GetAccountName());
  468. LS_CharSelectList list;
  469. list.loadData(GetAccountID(), GetLoginAccount()->charlist, GetVersion());
  470. EQ2Packet* outapp = list.serialize(GetVersion());
  471. // DumpPacket(outapp);
  472. QueuePacket(outapp);
  473. }
  474. void Client::SendLoginDeniedBadVersion(){
  475. EQ2Packet* app = new EQ2Packet(OP_LoginReplyMsg, 0, sizeof(LS_LoginResponse));
  476. LS_LoginResponse* ls_response = (LS_LoginResponse*)app->pBuffer;
  477. ls_response->reply_code = 6;
  478. ls_response->unknown03 = 0xFFFFFFFF;
  479. ls_response->unknown04 = 0xFFFFFFFF;
  480. QueuePacket(app);
  481. StartDisconnectTimer();
  482. }
  483. void Client::SendLoginDenied(){
  484. EQ2Packet* app = new EQ2Packet(OP_LoginReplyMsg, 0, sizeof(LS_LoginResponse));
  485. LS_LoginResponse* ls_response = (LS_LoginResponse*)app->pBuffer;
  486. ls_response->reply_code = 1;
  487. ls_response->unknown03 = 0xFFFFFFFF;
  488. ls_response->unknown04 = 0xFFFFFFFF;
  489. QueuePacket(app);
  490. StartDisconnectTimer();
  491. }
  492. void Client::SendLoginAccepted() {
  493. PacketStruct* packet = configReader.getStruct("LS_LoginReplyMsg", GetVersion());
  494. int i = 0;
  495. if (packet)
  496. {
  497. packet->setDataByName("account_id", 1);
  498. //packet->setDataByName("login_response", 0);
  499. //packet->setDataByName("reset_appearance", 0);
  500. packet->setDataByName("do_not_force_soga", 1);
  501. //packet->setDataByName("race_unknown", 0x3F);
  502. //packet->setDataByName("unknown11", 2); // can be 7
  503. packet->setDataByName("sub_level", 2);
  504. packet->setDataByName("race_flag", 0x1FFFFF);
  505. packet->setDataByName("class_flag", 0x7FFFFFE);
  506. packet->setMediumStringByName("username", GetAccountName());
  507. packet->setMediumStringByName("password", GetAccountName());
  508. packet->setDataByName("unknown5", 0x7C);
  509. packet->setDataByName("unknown7", 0xFF6FFFBF);
  510. // Image 2020 Notes
  511. // Login Server only supports AoM at current time, but we will need to keep in mind the structure for 60100 or later calls for additional custom fields
  512. // >=60100
  513. packet->setDataByName("unknown11", 0x0E);
  514. packet->setDataByName("unknown7a", 0xFFFF);
  515. packet->setDataByName("unknown8", 0xFF, 1);
  516. EQ2Packet* outapp = packet->serialize();
  517. QueuePacket(outapp);
  518. safe_delete(packet);
  519. }
  520. }
  521. void Client::SendWorldList(){
  522. EQ2Packet* pack = world_list.MakeServerListPacket(lsadmin, version);
  523. EQ2Packet* dupe = pack->Copy();
  524. //DumpPacket(dupe->pBuffer,dupe->size);
  525. QueuePacket(dupe);
  526. return;
  527. }
  528. void Client::QueuePacket(EQ2Packet* app){
  529. eqnc->EQ2QueuePacket(app);
  530. }
  531. void Client::WorldResponse(int32 worldid, int8 response, char* ip_address, int32 port, int32 access_key)
  532. {
  533. LWorld* world = world_list.FindByID(worldid);
  534. if(world == 0) {
  535. FatalError(0);
  536. return;
  537. }
  538. if(response != 1){
  539. if(response == PLAY_ERROR_CHAR_NOT_LOADED){
  540. string pending_play_char_name = database.GetCharacterName(pending_play_char_id, worldid);
  541. if(database.VerifyDelete(GetAccountID(), pending_play_char_id, pending_play_char_name.c_str())){
  542. GetLoginAccount()->removeCharacter((char*)pending_play_char_name.c_str());
  543. }
  544. }
  545. FatalError(response);
  546. return;
  547. }
  548. PacketStruct* response_packet = configReader.getStruct("LS_PlayResponse", GetVersion());
  549. if(response_packet){
  550. safe_delete(playWaitTimer);
  551. response_packet->setDataByName("response", 1);
  552. response_packet->setSmallStringByName("server", ip_address);
  553. response_packet->setDataByName("port", port);
  554. response_packet->setDataByName("account_id", GetAccountID());
  555. response_packet->setDataByName("access_code", access_key);
  556. EQ2Packet* outapp = response_packet->serialize();
  557. QueuePacket(outapp);
  558. safe_delete(response_packet);
  559. }
  560. return;
  561. }
  562. void Client::FatalError(int8 response) {
  563. safe_delete(playWaitTimer);
  564. SendPlayFailed(response);
  565. }
  566. void Client::SendPlayFailed(int8 response){
  567. PacketStruct* response_packet = configReader.getStruct("LS_PlayResponse", GetVersion());
  568. if(response_packet){
  569. response_packet->setDataByName("response", response);
  570. response_packet->setSmallStringByName("server", "");
  571. response_packet->setDataByName("port", 0);
  572. response_packet->setDataByName("account_id", GetAccountID());
  573. response_packet->setDataByName("access_code", 0);
  574. EQ2Packet* outapp = response_packet->serialize();
  575. QueuePacket(outapp);
  576. safe_delete(response_packet);
  577. }
  578. }
  579. void ClientList::Add(Client* client) {
  580. MClientList.writelock();
  581. client_list[client] = true;
  582. MClientList.releasewritelock();
  583. }
  584. Client* ClientList::Get(int32 ip, int16 port) {
  585. Client* ret = 0;
  586. map<Client*, bool>::iterator itr;
  587. MClientList.readlock();
  588. for(itr = client_list.begin(); itr != client_list.end(); itr++){
  589. if(itr->first->GetIP() == ip && itr->first->GetPort() == port){
  590. ret = itr->first;
  591. break;
  592. }
  593. }
  594. MClientList.releasereadlock();
  595. return ret;
  596. }
  597. void ClientList::FindByCreateRequest(){
  598. Client* client = 0;
  599. map<Client*, bool>::iterator itr;
  600. MClientList.readlock();
  601. for(itr = client_list.begin(); itr != client_list.end(); itr++){
  602. if(itr->first->AwaitingCharCreationRequest()){
  603. if(!client)
  604. client = itr->first;
  605. else{
  606. client = 0;//more than 1 character waiting, dont want to send rejection to wrong one
  607. break;
  608. }
  609. }
  610. }
  611. MClientList.releasereadlock();
  612. if(client)
  613. client->CharacterRejected(UNKNOWNERROR_REPLY);
  614. }
  615. Client* ClientList::FindByLSID(int32 lsaccountid) {
  616. Client* client = 0;
  617. map<Client*, bool>::iterator itr;
  618. MClientList.readlock();
  619. for(itr = client_list.begin(); itr != client_list.end(); itr++){
  620. if(itr->first->GetAccountID() == lsaccountid){
  621. client = itr->first;
  622. break;
  623. }
  624. }
  625. MClientList.releasereadlock();
  626. return client;
  627. }
  628. void ClientList::SendPacketToAllClients(EQ2Packet* app){
  629. Client* client = 0;
  630. map<Client*, bool>::iterator itr;
  631. MClientList.readlock();
  632. if(client_list.size() > 0){
  633. for(itr = client_list.begin(); itr != client_list.end(); itr++){
  634. itr->first->QueuePacket(app->Copy());
  635. }
  636. }
  637. safe_delete(app);
  638. MClientList.releasereadlock();
  639. }
  640. void ClientList::Process() {
  641. Client* client = 0;
  642. vector<Client*> erase_list;
  643. map<Client*, bool>::iterator itr;
  644. MClientList.readlock();
  645. for(itr = client_list.begin(); itr != client_list.end(); itr++){
  646. client = itr->first;
  647. if(!client->Process())
  648. erase_list.push_back(client);
  649. }
  650. MClientList.releasereadlock();
  651. if(erase_list.size() > 0){
  652. vector<Client*>::iterator erase_itr;
  653. MClientList.writelock();
  654. for(erase_itr = erase_list.begin(); erase_itr != erase_list.end(); erase_itr++){
  655. client = *erase_itr;
  656. struct in_addr in;
  657. in.s_addr = client->getConnection()->GetRemoteIP();
  658. net.numclients--;
  659. LogWrite(LOGIN__INFO, 0, "Login", "Removing client from ip: %s on port %i, Account Name: %s", inet_ntoa(in), ntohs(client->getConnection()->GetRemotePort()), client->GetAccountName());
  660. client->getConnection()->SetState(CLOSED);
  661. net.UpdateWindowTitle();
  662. client_list.erase(client);
  663. }
  664. MClientList.releasewritelock();
  665. }
  666. }
  667. void Client::StartDisconnectTimer() {
  668. if (!disconnectTimer)
  669. {
  670. disconnectTimer = new Timer(1000);
  671. disconnectTimer->Start();
  672. }
  673. }