diff --git a/ucs/ucs.cpp b/ucs/ucs.cpp index 3edc3b45e..1e7c0e898 100644 --- a/ucs/ucs.cpp +++ b/ucs/ucs.cpp @@ -54,9 +54,6 @@ volatile bool RunLoops = true; void CatchSignal(int sig_num) { RunLoops = false; - - if(worldserver) - worldserver->Disconnect(); } std::string GetMailPrefix() { @@ -143,8 +140,6 @@ int main() { worldserver = new WorldServer; - worldserver->Connect(); - while(RunLoops) { Timer::SetCurrentTime(); @@ -154,12 +149,6 @@ int main() { if(ChannelListProcessTimer.Check()) ChannelList->Process(); - if (InterserverTimer.Check()) { - if (worldserver->TryReconnect() && (!worldserver->Connected())) - worldserver->AsyncConnect(); - } - worldserver->Process(); - EQ::EventLoop::Get().Process(); Sleep(1); diff --git a/ucs/worldserver.cpp b/ucs/worldserver.cpp index 4e11c39b7..fb9176c22 100644 --- a/ucs/worldserver.cpp +++ b/ucs/worldserver.cpp @@ -17,6 +17,15 @@ */ #include "../common/global_define.h" #include "../common/eqemu_logsys.h" +#include "../common/servertalk.h" +#include "../common/misc_functions.h" +#include "../common/packet_functions.h" +#include "../common/md5.h" +#include "worldserver.h" +#include "clientlist.h" +#include "ucsconfig.h" +#include "database.h" + #include #include #include @@ -25,14 +34,6 @@ #include #include -#include "../common/servertalk.h" -#include "worldserver.h" -#include "clientlist.h" -#include "ucsconfig.h" -#include "database.h" -#include "../common/packet_functions.h" -#include "../common/md5.h" - extern WorldServer worldserver; extern Clientlist *g_Clientlist; extern const ucsconfig *Config; @@ -41,94 +42,78 @@ extern Database database; void ProcessMailTo(Client *c, std::string from, std::string subject, std::string message); WorldServer::WorldServer() -: WorldConnection(EmuTCPConnection::packetModeUCS, Config->SharedKey.c_str()) { - pTryReconnect = true; + m_connection.reset(new EQ::Net::ServertalkClient(Config->WorldIP, Config->WorldTCPPort, false, "UCS", Config->SharedKey)); + m_connection->OnMessage(std::bind(&WorldServer::ProcessMessage, this, std::placeholders::_1, std::placeholders::_2)); } WorldServer::~WorldServer() { } -void WorldServer::OnConnected() +void WorldServer::ProcessMessage(uint16 opcode, EQ::Net::Packet &p) { - Log.Out(Logs::Detail, Logs::UCS_Server, "Connected to World."); - WorldConnection::OnConnected(); -} + ServerPacket tpack(opcode, p); + ServerPacket *pack = &tpack; -void WorldServer::Process() -{ - WorldConnection::Process(); + Log.Out(Logs::Detail, Logs::UCS_Server, "Received Opcode: %4X", opcode); - if (!Connected()) - return; - - ServerPacket *pack = nullptr; - - while((pack = tcpc.PopPacket())) + switch(opcode) { - Log.Out(Logs::Detail, Logs::UCS_Server, "Received Opcode: %4X", pack->opcode); - - switch(pack->opcode) + case 0: { + break; + } + case ServerOP_KeepAlive: { - case 0: { + break; + } + case ServerOP_UCSMessage: + { + char *Buffer = (char *)pack->pBuffer; + + auto From = new char[strlen(Buffer) + 1]; + + VARSTRUCT_DECODE_STRING(From, Buffer); + + std::string Message = Buffer; + + Log.Out(Logs::Detail, Logs::UCS_Server, "Player: %s, Sent Message: %s", From, Message.c_str()); + + Client *c = g_Clientlist->FindCharacter(From); + + safe_delete_array(From); + + if(Message.length() < 2) break; - } - case ServerOP_KeepAlive: + + if(!c) { + Log.Out(Logs::Detail, Logs::UCS_Server, "Client not found."); break; } - case ServerOP_UCSMessage: + + if(Message[0] == ';') { - char *Buffer = (char *)pack->pBuffer; - - auto From = new char[strlen(Buffer) + 1]; - - VARSTRUCT_DECODE_STRING(From, Buffer); - - std::string Message = Buffer; - - Log.Out(Logs::Detail, Logs::UCS_Server, "Player: %s, Sent Message: %s", From, Message.c_str()); - - Client *c = g_Clientlist->FindCharacter(From); - - safe_delete_array(From); - - if(Message.length() < 2) - break; - - if(!c) - { - Log.Out(Logs::Detail, Logs::UCS_Server, "Client not found."); - break; - } - - if(Message[0] == ';') - { - c->SendChannelMessageByNumber(Message.substr(1, std::string::npos)); - } - else if(Message[0] == '[') - { - g_Clientlist->ProcessOPMailCommand(c, Message.substr(1, std::string::npos)); - } - - break; + c->SendChannelMessageByNumber(Message.substr(1, std::string::npos)); } - - case ServerOP_UCSMailMessage: + else if(Message[0] == '[') { - ServerMailMessageHeader_Struct *mail = (ServerMailMessageHeader_Struct*)pack->pBuffer; - database.SendMail(std::string("SOE.EQ.") + Config->ShortName + std::string(".") + std::string(mail->to), - std::string(mail->from), - mail->subject, - mail->message, - std::string()); - break; + g_Clientlist->ProcessOPMailCommand(c, Message.substr(1, std::string::npos)); } + + break; + } + + case ServerOP_UCSMailMessage: + { + ServerMailMessageHeader_Struct *mail = (ServerMailMessageHeader_Struct*)pack->pBuffer; + database.SendMail(std::string("SOE.EQ.") + Config->ShortName + std::string(".") + std::string(mail->to), + std::string(mail->from), + mail->subject, + mail->message, + std::string()); + break; } } - - safe_delete(pack); - return; } diff --git a/ucs/worldserver.h b/ucs/worldserver.h index 0b9bebacd..aad68e085 100644 --- a/ucs/worldserver.h +++ b/ucs/worldserver.h @@ -18,18 +18,20 @@ #ifndef WORLDSERVER_H #define WORLDSERVER_H -#include "../common/worldconn.h" +#include "../net/servertalk_client_connection.h" #include "../common/eq_packet_structs.h" +#include -class WorldServer : public WorldConnection +class WorldServer { public: WorldServer(); - virtual ~WorldServer(); - virtual void Process(); + ~WorldServer(); + void ProcessMessage(uint16 opcode, EQ::Net::Packet &); private: - virtual void OnConnected(); + + std::unique_ptr m_connection; }; #endif diff --git a/world/net.cpp b/world/net.cpp index 2332f46ec..23231fd8b 100644 --- a/world/net.cpp +++ b/world/net.cpp @@ -437,6 +437,20 @@ int main(int argc, char** argv) { QSLink.RemoveConnection(connection); }); + server_connection->OnConnectionIdentified("UCS", [](std::shared_ptr connection) { + Log.OutF(Logs::General, Logs::World_Server, "New UCS Server connection from {2} at {0}:{1}", + connection->Handle()->RemoteIP(), connection->Handle()->RemotePort(), connection->GetUUID()); + + UCSLink.SetConnection(connection); + }); + + server_connection->OnConnectionRemoved("UCS", [](std::shared_ptr connection) { + Log.OutF(Logs::General, Logs::World_Server, "UCS Query Server connection from {0}", + connection->GetUUID()); + + UCSLink.SetConnection(nullptr); + }); + EQ::Net::EQStreamManagerOptions opts(9000, false, false); EQ::Net::EQStreamManager eqsm(opts); @@ -451,7 +465,6 @@ int main(int argc, char** argv) { InterserverTimer.Trigger(); uint8 ReconnectCounter = 100; std::shared_ptr eqs; - EmuTCPConnection* tcpc; EQStreamInterface *eqsi; eqsm.OnNewConnection([&stream_identifier](std::shared_ptr stream) { @@ -510,7 +523,6 @@ int main(int argc, char** argv) { zoneserver_list.Process(); launcher_list.Process(); - UCSLink.Process(); LFPGroupList.Process(); adventure_manager.Process(); diff --git a/world/ucs.cpp b/world/ucs.cpp index e15007959..49632276a 100644 --- a/world/ucs.cpp +++ b/world/ucs.cpp @@ -11,109 +11,58 @@ UCSConnection::UCSConnection() { Stream = 0; - authenticated = false; } -void UCSConnection::SetConnection(EmuTCPConnection *inStream) +void UCSConnection::SetConnection(std::shared_ptr inStream) { - if(Stream) + if(Stream && Stream->Handle()) { Log.Out(Logs::Detail, Logs::UCS_Server, "Incoming UCS Connection while we were already connected to a UCS."); - Stream->Disconnect(); + Stream->Handle()->Disconnect(); } Stream = inStream; - - authenticated = false; + Stream->OnMessage(std::bind(&UCSConnection::ProcessPacket, this, std::placeholders::_1, std::placeholders::_2)); } -bool UCSConnection::Process() +void UCSConnection::ProcessPacket(uint16 opcode, EQ::Net::Packet &p) { - if (!Stream || !Stream->Connected()) - return false; + if (!Stream) + return; - ServerPacket *pack = 0; + ServerPacket tpack(opcode, p); + ServerPacket *pack = &tpack; - while((pack = Stream->PopPacket())) + switch(opcode) { - if (!authenticated) + case 0: + break; + + case ServerOP_KeepAlive: { - if (WorldConfig::get()->SharedKey.length() > 0) - { - if (pack->opcode == ServerOP_ZAAuth && pack->size == 16) - { - uint8 tmppass[16]; - - MD5::Generate((const uchar*) WorldConfig::get()->SharedKey.c_str(), WorldConfig::get()->SharedKey.length(), tmppass); - - if (memcmp(pack->pBuffer, tmppass, 16) == 0) - authenticated = true; - else - { - struct in_addr in; - in.s_addr = GetIP(); - Log.Out(Logs::Detail, Logs::UCS_Server, "UCS authorization failed."); - auto pack = new ServerPacket(ServerOP_ZAAuthFailed); - SendPacket(pack); - delete pack; - Disconnect(); - return false; - } - } - else - { - struct in_addr in; - in.s_addr = GetIP(); - Log.Out(Logs::Detail, Logs::UCS_Server, "UCS authorization failed."); - auto pack = new ServerPacket(ServerOP_ZAAuthFailed); - SendPacket(pack); - delete pack; - Disconnect(); - return false; - } - } - else - { - Log.Out(Logs::Detail, Logs::UCS_Server,"**WARNING** You have not configured a world shared key in your config file. You should add a STRING element to your element to prevent unauthroized zone access."); - authenticated = true; - } - delete pack; - continue; + // ignore this + break; } - switch(pack->opcode) + case ServerOP_ZAAuth: { - case 0: - break; - - case ServerOP_KeepAlive: - { - // ignore this - break; - } - case ServerOP_ZAAuth: - { - Log.Out(Logs::Detail, Logs::UCS_Server, "Got authentication from UCS when they are already authenticated."); - break; - } - default: - { - Log.Out(Logs::Detail, Logs::UCS_Server, "Unknown ServerOPcode from UCS 0x%04x, size %d", pack->opcode, pack->size); - DumpPacket(pack->pBuffer, pack->size); - break; - } + Log.Out(Logs::Detail, Logs::UCS_Server, "Got authentication from UCS when they are already authenticated."); + break; + } + default: + { + Log.Out(Logs::Detail, Logs::UCS_Server, "Unknown ServerOPcode from UCS 0x%04x, size %d", opcode, pack->size); + DumpPacket(pack->pBuffer, pack->size); + break; } - - delete pack; } - return(true); } -bool UCSConnection::SendPacket(ServerPacket* pack) +void UCSConnection::SendPacket(ServerPacket* pack) { if(!Stream) - return false; + return; - return Stream->SendPacket(pack); + Stream->SendPacket(pack); } void UCSConnection::SendMessage(const char *From, const char *Message) diff --git a/world/ucs.h b/world/ucs.h index a6e8ae032..d2051c0be 100644 --- a/world/ucs.h +++ b/world/ucs.h @@ -2,22 +2,22 @@ #define UCS_H #include "../common/types.h" -#include "../common/emu_tcp_connection.h" +#include "../common/net/servertalk_server_connection.h" #include "../common/servertalk.h" +#include class UCSConnection { public: UCSConnection(); - void SetConnection(EmuTCPConnection *inStream); - bool Process(); - bool SendPacket(ServerPacket* pack); - void Disconnect() { if(Stream) Stream->Disconnect(); } + void SetConnection(std::shared_ptr connection); + void ProcessPacket(uint16 opcode, EQ::Net::Packet &p); + void SendPacket(ServerPacket* pack); + void Disconnect() { if(Stream && Stream->Handle()) Stream->Handle()->Disconnect(); } void SendMessage(const char *From, const char *Message); private: - inline uint32 GetIP() const { return Stream ? Stream->GetrIP() : 0; } - EmuTCPConnection *Stream; - bool authenticated; + inline std::string GetIP() const { return (Stream && Stream->Handle()) ? Stream->Handle()->RemoteIP() : 0; } + std::shared_ptr Stream; }; #endif /*UCS_H_*/