9
3

client.cpp 22 KB

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