mirror of
https://github.com/EQEmu/Server.git
synced 2026-05-21 19:38:25 +00:00
Changed websockets lib, protocol not finalized at all
This commit is contained in:
@@ -15,7 +15,7 @@ INSTALL(TARGETS web_interface RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX})
|
||||
|
||||
ADD_DEFINITIONS(-DWEB_INTERFACE)
|
||||
|
||||
TARGET_LINK_LIBRARIES(web_interface common debug ${MySQL_LIBRARY_DEBUG} optimized ${MySQL_LIBRARY_RELEASE} ${ZLIB_LIBRARY})
|
||||
TARGET_LINK_LIBRARIES(web_interface common debug ${MySQL_LIBRARY_DEBUG} optimized ${MySQL_LIBRARY_RELEASE} ${ZLIB_LIBRARY} ${LIBWEBSOCKETS_LIBRARIES})
|
||||
|
||||
IF(MSVC)
|
||||
SET_TARGET_PROPERTIES(web_interface PROPERTIES LINK_FLAGS_RELEASE "/OPT:REF /OPT:ICF")
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
/* 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 "web_connection.h"
|
||||
|
||||
WebConnection::WebConnection(uint32 ID, WebServer *server, SOCKET in_socket, uint32 irIP, uint16 irPort)
|
||||
: TCPConnection(ID, in_socket, irIP, irPort)
|
||||
{
|
||||
w_server = server;
|
||||
}
|
||||
|
||||
WebConnection::~WebConnection() {
|
||||
}
|
||||
|
||||
bool WebConnection::ProcessReceivedData(char* errbuf) {
|
||||
//recvbuf_used
|
||||
//recvbuf
|
||||
return true;
|
||||
}
|
||||
|
||||
WebServer::WebServer(uint16 port)
|
||||
: TCPServer<EmuTCPConnection>(iPort)
|
||||
{
|
||||
}
|
||||
|
||||
void WebServer::CreateNewConnection(uint32 ID, SOCKET in_socket, uint32 irIP, uint16 irPort) {
|
||||
WebConnection *conn = new WebConnection(ID, this, in_socket, irIP, irPort, pOldFormat);
|
||||
AddConnection(conn);
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
/* 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 WI_WEB_CONNECTION_H
|
||||
#define WI_WEB_CONNECTION_H
|
||||
|
||||
#include "../common/TCPConnection.h"
|
||||
|
||||
class WebServer;
|
||||
class WebConnection : public TCPConnection
|
||||
{
|
||||
public:
|
||||
WebConnection(uint32 ID, WebServer *server, SOCKET in_socket, uint32 irIP, uint16 irPort);
|
||||
virtual ~WebConnection();
|
||||
|
||||
protected:
|
||||
virtual bool ProcessReceivedData(char* errbuf = 0);
|
||||
WebServer *w_server;
|
||||
};
|
||||
|
||||
class WebServer : protected TCPServer<WebConnection> {
|
||||
public:
|
||||
WebServer(uint16 port);
|
||||
|
||||
protected:
|
||||
virtual void CreateNewConnection(uint32 ID, SOCKET in_socket, uint32 irIP, uint16 irPort);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -7,24 +7,96 @@
|
||||
#include "../common/crash.h"
|
||||
#include "../common/EQEmuConfig.h"
|
||||
#include "worldserver.h"
|
||||
#include "lib/libwebsockets.h"
|
||||
#include <signal.h>
|
||||
#include <list>
|
||||
|
||||
volatile bool run = true;
|
||||
TimeoutManager timeout_manager;
|
||||
const EQEmuConfig *config = nullptr;
|
||||
WorldServer *worldserver = nullptr;
|
||||
libwebsocket_context *context = nullptr;
|
||||
|
||||
void CatchSignal(int sig_num) {
|
||||
run = false;
|
||||
if(worldserver)
|
||||
worldserver->Disconnect();
|
||||
|
||||
if(context)
|
||||
libwebsocket_cancel_service(context);
|
||||
}
|
||||
|
||||
int callback_http(libwebsocket_context *context, libwebsocket *wsi, libwebsocket_callback_reasons reason, void *user, void *in, size_t len) {
|
||||
switch (reason) {
|
||||
case LWS_CALLBACK_HTTP:
|
||||
libwebsockets_return_http_status(context, wsi, HTTP_STATUS_FORBIDDEN, NULL);
|
||||
break;
|
||||
case LWS_CALLBACK_HTTP_BODY_COMPLETION:
|
||||
libwebsockets_return_http_status(context, wsi, HTTP_STATUS_OK, NULL);
|
||||
return -1;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct per_session_data_eqemu {
|
||||
bool auth;
|
||||
std::list<std::string> *send_queue;
|
||||
};
|
||||
|
||||
int callback_eqemu(libwebsocket_context *context, libwebsocket *wsi, libwebsocket_callback_reasons reason, void *user, void *in, size_t len) {
|
||||
per_session_data_eqemu *session = (per_session_data_eqemu*)user;
|
||||
switch (reason) {
|
||||
case LWS_CALLBACK_ESTABLISHED:
|
||||
session->auth = false;
|
||||
session->send_queue = new std::list<std::string>();
|
||||
break;
|
||||
case LWS_CALLBACK_RECEIVE: {
|
||||
//recv and parse commands here
|
||||
if(len < 1)
|
||||
break;
|
||||
|
||||
std::string command;
|
||||
command.assign((const char*)in, len);
|
||||
|
||||
if(command.compare("get_version") == 0) {
|
||||
session->send_queue->push_back("0.8.0");
|
||||
}
|
||||
}
|
||||
break;
|
||||
case LWS_CALLBACK_SERVER_WRITEABLE:
|
||||
//send stuff here
|
||||
for (auto iter = session->send_queue->begin(); iter != session->send_queue->end(); ++iter) {
|
||||
auto n = libwebsocket_write(wsi, (unsigned char*)&(*iter)[0], (*iter).size(), LWS_WRITE_TEXT);
|
||||
if (n < (*iter).size()) {
|
||||
//couldn't write the message
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
session->send_queue->clear();
|
||||
break;
|
||||
case LWS_CALLBACK_PROTOCOL_DESTROY:
|
||||
//clean stuff up here
|
||||
delete session->send_queue;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct libwebsocket_protocols protocols[] = {
|
||||
{ "http-only", callback_http, 0, 0, },
|
||||
{ "eqemu", callback_eqemu, sizeof(per_session_data_eqemu), 0, },
|
||||
{ nullptr, nullptr, 0, 0 }
|
||||
};
|
||||
|
||||
int main() {
|
||||
RegisterExecutablePlatform(ExePlatformWebInterface);
|
||||
set_exception_handler();
|
||||
Timer InterserverTimer(INTERSERVER_TIMER); // does auto-reconnect
|
||||
_log(WEB_INTERFACE__INIT, "Starting EQEmu Socket Server.");
|
||||
_log(WEB_INTERFACE__INIT, "Starting EQEmu Web Server.");
|
||||
|
||||
if (signal(SIGINT, CatchSignal) == SIG_ERR) {
|
||||
_log(WEB_INTERFACE__ERROR, "Could not set signal handler");
|
||||
@@ -36,22 +108,48 @@ int main() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
Timer writable_socket_timer;
|
||||
config = EQEmuConfig::get();
|
||||
lws_context_creation_info info;
|
||||
memset(&info, 0, sizeof info);
|
||||
info.port = config->WebInterfacePort;
|
||||
info.protocols = protocols;
|
||||
info.extensions = libwebsocket_get_internal_extensions();
|
||||
info.gid = -1;
|
||||
info.uid = -1;
|
||||
|
||||
context = libwebsocket_create_context(&info);
|
||||
if (context == NULL) {
|
||||
_log(WEB_INTERFACE__ERROR, "Could not create websocket handler.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
worldserver = new WorldServer(config->SharedKey);
|
||||
worldserver->Connect();
|
||||
writable_socket_timer.Start(50);
|
||||
|
||||
while(run) {
|
||||
while(run) {
|
||||
Timer::SetCurrentTime();
|
||||
|
||||
if (InterserverTimer.Check()) {
|
||||
if (worldserver->TryReconnect() && (!worldserver->Connected()))
|
||||
worldserver->AsyncConnect();
|
||||
}
|
||||
worldserver->Process();
|
||||
|
||||
timeout_manager.CheckTimeouts();
|
||||
|
||||
if (writable_socket_timer.Check(true)) {
|
||||
libwebsocket_callback_on_writable_all_protocol(&protocols[1]);
|
||||
}
|
||||
|
||||
libwebsocket_service(context, 5);
|
||||
Sleep(1);
|
||||
}
|
||||
|
||||
safe_delete(worldserver);
|
||||
libwebsocket_context_destroy(context);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user