diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 29673471d..f4186f8da 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -64,6 +64,7 @@ SET(common_sources timeoutmgr.cpp timer.cpp unix.cpp + uuid.cpp worldconn.cpp XMLParser.cpp platform.cpp @@ -185,6 +186,7 @@ SET(common_headers timer.h types.h unix.h + uuid.h useperl.h version.h worldconn.h diff --git a/common/uuid.cpp b/common/uuid.cpp new file mode 100644 index 000000000..caa18438b --- /dev/null +++ b/common/uuid.cpp @@ -0,0 +1,44 @@ +/* + EQEMu: Everquest Server Emulator + Copyright (C) 2001-2014 EQEMu Development Team (http://eqemulator.net) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY except by those people which sell it, which + are required to give you total support for your newly bought product; + without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include "uuid.h" + +#ifdef WIN32 +#include +#else +#include +#endif + +std::string CreateUUID() { +#ifdef WIN32 + UUID uuid; + UuidCreate(&uuid); + unsigned char *str = nullptr; + UuidToStringA(&uuid, &str); + std::string s((char*)str); + RpcStringFreeA(&str); + return s; +#else + char str[64] = { 0 }; + uuid_t uuid; + uuid_generate_random(uuid); + uuid_unparse(uuid, str); + return str; +#endif +} diff --git a/common/uuid.h b/common/uuid.h new file mode 100644 index 000000000..907e51a65 --- /dev/null +++ b/common/uuid.h @@ -0,0 +1,27 @@ +/* + EQEMu: Everquest Server Emulator + Copyright (C) 2001-2014 EQEMu Development Team (http://eqemulator.net) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY except by those people which sell it, which + are required to give you total support for your newly bought product; + without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ +#ifndef COMMON_UUID_H +#define COMMON_UUID_H + +#include + +std::string CreateUUID(); + +#endif + diff --git a/common/web_interface_utils.h b/common/web_interface_utils.h index e018634fe..6f2eccb72 100644 --- a/common/web_interface_utils.h +++ b/common/web_interface_utils.h @@ -15,8 +15,8 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#ifndef WI_WEBINTUTIL_H -#define WI_WEBINTUTIL_H +#ifndef COMMON_WEBINTUTIL_H +#define COMMON_WEBINTUTIL_H #include "../common/debug.h" #include "rapidjson/writer.h" diff --git a/web_interface/CMakeLists.txt b/web_interface/CMakeLists.txt index 22132c0b8..de9333b9a 100644 --- a/web_interface/CMakeLists.txt +++ b/web_interface/CMakeLists.txt @@ -20,6 +20,7 @@ TARGET_LINK_LIBRARIES(web_interface common debug ${MySQL_LIBRARY_DEBUG} optimize IF(MSVC) SET_TARGET_PROPERTIES(web_interface PROPERTIES LINK_FLAGS_RELEASE "/OPT:REF /OPT:ICF") TARGET_LINK_LIBRARIES(web_interface "Ws2_32.lib") + TARGET_LINK_LIBRARIES(web_interface rpcrt4) ENDIF(MSVC) IF(MINGW) diff --git a/web_interface/web_interface.cpp b/web_interface/web_interface.cpp index 3c3b2d05b..55e72a4a2 100644 --- a/web_interface/web_interface.cpp +++ b/web_interface/web_interface.cpp @@ -7,25 +7,29 @@ #include "../common/crash.h" #include "../common/EQEmuConfig.h" #include "../common/web_interface_utils.h" +#include "../common/uuid.h" #include "worldserver.h" #include "lib/libwebsockets.h" -#include -#include #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" +#include +#include +#include + +#define MAX_MESSAGE_LENGTH 2048 + +struct per_session_data_eqemu { + bool auth; + std::string uuid; + std::list *send_queue; +}; volatile bool run = true; TimeoutManager timeout_manager; const EQEmuConfig *config = nullptr; WorldServer *worldserver = nullptr; libwebsocket_context *context = nullptr; - -struct per_session_data_eqemu { - bool auth; - std::list *send_queue; -}; - -per_session_data_eqemu *globalsession = NULL; +std::map sessions; void CatchSignal(int sig_num) { run = false; @@ -55,7 +59,10 @@ int callback_eqemu(libwebsocket_context *context, libwebsocket *wsi, libwebsocke switch (reason) { case LWS_CALLBACK_ESTABLISHED: session->auth = false; + session->uuid = CreateUUID(); session->send_queue = new std::list(); + sessions[session->uuid] = session; + printf("Create session %s\n", session->uuid.c_str()); break; case LWS_CALLBACK_RECEIVE: { @@ -66,31 +73,21 @@ int callback_eqemu(libwebsocket_context *context, libwebsocket *wsi, libwebsocke std::string command; command.assign((const char*)in, len); - globalsession = session; if(command.compare("get_version") == 0) { session->send_queue->push_back("0.8.0"); } - if (command.compare("test_json") == 0) { - session->send_queue->push_back(MakeJSON("niggers:tits")); - } - if (command.compare("stream_test") == 0) { - ServerPacket* pack = new ServerPacket(ServerOP_WIServGeneric, len + 1); - pack->WriteString(command.c_str()); - worldserver->SendPacket(pack); - safe_delete(pack); - } - - } break; - case LWS_CALLBACK_SERVER_WRITEABLE: - //send stuff here + } + case LWS_CALLBACK_SERVER_WRITEABLE: { + //send messages here + char out_message[MAX_MESSAGE_LENGTH + LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING + 1]; for (auto iter = session->send_queue->begin(); iter != session->send_queue->end(); ++iter) { + + //out_message size_t sz = LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING + (*iter).size(); - unsigned char *buf = new unsigned char[sz]; - memset(buf, 0, sz); - memcpy(&buf[LWS_SEND_BUFFER_PRE_PADDING], &(*iter)[0], (*iter).size()); - auto n = libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], (*iter).size(), LWS_WRITE_TEXT); - delete[] buf; + memset(out_message, 0, sz); + memcpy(&out_message[LWS_SEND_BUFFER_PRE_PADDING], &(*iter)[0], (*iter).size()); + auto n = libwebsocket_write(wsi, (unsigned char*)&out_message[LWS_SEND_BUFFER_PRE_PADDING], (*iter).size(), LWS_WRITE_TEXT); if (n < (*iter).size()) { //couldn't write the message return -1; @@ -98,9 +95,19 @@ int callback_eqemu(libwebsocket_context *context, libwebsocket *wsi, libwebsocke } session->send_queue->clear(); break; + } case LWS_CALLBACK_PROTOCOL_DESTROY: - //clean stuff up here - delete session->send_queue; + //clean up sessions here + safe_delete(session->send_queue); + break; + + case LWS_CALLBACK_CLOSED: + printf("Closed session %s\n", session->uuid.c_str()); + //Session closed but perhaps not yet destroyed, we still don't want to track it though. + sessions.erase(session->uuid); + safe_delete(session->send_queue); + session->uuid.clear(); + session->auth = false; break; default: break; diff --git a/web_interface/worldserver.cpp b/web_interface/worldserver.cpp index e566374db..4fc02a76c 100644 --- a/web_interface/worldserver.cpp +++ b/web_interface/worldserver.cpp @@ -34,7 +34,6 @@ struct per_session_data_eqemu { std::list *send_queue; }; -extern per_session_data_eqemu *globalsession; WorldServer::WorldServer(std::string shared_key) : WorldConnection(EmuTCPConnection::packetModeWebInterface, shared_key.c_str()){ pTryReconnect = true; @@ -60,9 +59,6 @@ void WorldServer::Process(){ case 0: { break; } case ServerOP_KeepAlive: { break; } case ServerOP_WIWorldResponse: { - char pos_update[255]; - pack->ReadString(pos_update); - globalsession->send_queue->push_back(pos_update); break; } }