client.cpp 22 KB

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