client.cpp 22 KB

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