Rewrite Soap Server (#130)

Co-authored-by: Antz <billy1arm@users.noreply.github.com>
This commit is contained in:
Meltie2013 2021-02-04 16:02:02 -06:00 committed by GitHub
parent 248e2db97d
commit 16d620b84d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 132 additions and 219 deletions

View File

@ -22,205 +22,105 @@
* and lore are copyrighted by Blizzard Entertainment, Inc. * and lore are copyrighted by Blizzard Entertainment, Inc.
*/ */
#include <ace/OS.h>
#include <ace/Message_Block.h>
#include "SoapThread.h" #include "SoapThread.h"
#include "soapH.h"
#include "AccountMgr.h"
#include "Log.h"
#include "World.h"
#include "soapStub.h" #include "soapStub.h"
#include "World.h" void SoapThread(const std::string& host, uint16 port)
#include "Log.h"
#include "AccountMgr.h"
/// WARNING! This code needs serious reviewing
struct SOAPCommand
{ {
public: struct soap soap;
void appendToPrintBuffer(const char* msg) soap_init(&soap);
{ soap_set_imode(&soap, SOAP_C_UTFSTRING);
m_printBuffer += msg; soap_set_omode(&soap, SOAP_C_UTFSTRING);
}
void setCommandSuccess(bool val)
{
m_success = val;
}
bool hasCommandSucceeded()
{
return m_success;
}
static void print(void* callbackArg, const char* msg)
{
((SOAPCommand*)callbackArg)->appendToPrintBuffer(msg);
}
static void commandFinished(void* callbackArg, bool success);
bool m_success;
std::string m_printBuffer;
};
class SoapPool : public ACE_Task<ACE_MT_SYNCH>
{
public:
virtual int svc(void) override
{
while (1)
{
ACE_Message_Block* mb = 0;
if (this->getq(mb) == -1)
{
break;
}
// Process the message.
process_message(mb);
}
return 0;
}
private:
void process_message(ACE_Message_Block* mb)
{
struct soap* soap;
ACE_OS::memcpy(&soap, mb->rd_ptr(), sizeof(struct soap*));
mb->release();
soap_serve(soap);
soap_destroy(soap); // dealloc C++ data
soap_end(soap); // dealloc data and clean up
soap_done(soap); // detach soap struct
free(soap);
}
};
SoapThread::~SoapThread()
{
if(pool_)
{
delete pool_;
}
}
int SoapThread::open(void* unused)
{
// create pool
pool_ = new SoapPool;
pool_->activate(THR_NEW_LWP | THR_JOINABLE, SOAP_THREADS);
int m;
soap_init(&soap_);
soap_set_imode(&soap_, SOAP_C_UTFSTRING);
soap_set_omode(&soap_, SOAP_C_UTFSTRING);
m = soap_bind(&soap_, host_, port_, 100);
if (m < 0)
{
sLog.outError("SoapThread: couldn't bind to %s:%d", host_, port_);
return -1;
}
// check every 3 seconds if world ended // check every 3 seconds if world ended
soap_.accept_timeout = 3; soap.accept_timeout = 3;
soap_.recv_timeout = 5; soap.recv_timeout = 5;
soap_.send_timeout = 5; soap.send_timeout = 5;
activate(); if (!soap_valid_socket(soap_bind(&soap, host.c_str(), port, 100)))
return 0; {
} sLog.outError("SoapThread: couldn't bind to %s:%d", host.c_str(), port);
exit(-1);
}
sLog.outString("SoapThread: Bound to http://%s:%d", host.c_str(), port);
int SoapThread::svc()
{
int s;
sLog.outString("SOAP Thread started (listening on %s:%d)", host_, port_);
while (!World::IsStopped()) while (!World::IsStopped())
{ {
s = soap_accept(&soap_); if (!soap_valid_socket(soap_accept(&soap)))
if (s < 0)
{ {
// ran into an accept timeout continue; // ran into an accept timeout
continue;
} }
struct soap* thread_soap = soap_copy(&soap_);// make a safe copy sLog.outString("Accepted connection from IP %d.%d.%d.%d", (int)(soap.ip >> 24) & 0xFF, (int)(soap.ip >> 16) & 0xFF, (int)(soap.ip >> 8) & 0xFF, (int)soap.ip & 0xFF);
struct soap* thread_soap = soap_copy(&soap); // make a safe copy
ACE_Message_Block* mb = new ACE_Message_Block(sizeof(struct soap*)); process_message(thread_soap);
ACE_OS::memcpy(mb->wr_ptr(), &thread_soap, sizeof(struct soap*));
pool_->putq(mb);
} }
pool_->msg_queue()->deactivate(); soap_done(&soap);
pool_->wait();
soap_done(&soap_);
sLog.outString("SOAP Thread stopped");
return 0;
} }
void process_message(struct soap* soap_message)
/* {
Code used for generating stubs: soap_serve(soap_message);
soap_destroy(soap_message); // dealloc C++ data
int ns1__executeCommand(char* command, char** result); soap_end(soap_message); // dealloc data and clean up
*/ soap_done(soap_message); // detach soap struct
free(soap_message);
}
int ns1__executeCommand(soap* soap, char* command, char** result) int ns1__executeCommand(soap* soap, char* command, char** result)
{ {
// security check // security check
if (!soap->userid || !soap->passwd) if (!soap->userid || !soap->passwd)
{ {
DEBUG_LOG("MaNGOSsoap: Client didn't provide login information"); sLog.outString("SoapThread: Client didn't provide login information");
return 401; return 401;
} }
uint32 accountId = sAccountMgr.GetId(soap->userid); uint32 accountId = sAccountMgr.GetId(soap->userid);
if (!accountId) if (!accountId)
{ {
DEBUG_LOG("MaNGOSsoap: Client used invalid username '%s'", soap->userid); sLog.outString("SoapThread: Client used invalid username %s", soap->userid);
return 401; return 401;
} }
if (!sAccountMgr.CheckPassword(accountId, soap->passwd)) if (!sAccountMgr.CheckPassword(accountId, soap->passwd))
{ {
DEBUG_LOG("MaNGOSsoap: invalid password for account '%s'", soap->userid); sLog.outString("SoapThread: Client sent an invalid password for account %s", soap->passwd);
return 401; return 401;
} }
/* ToDo: Add realm check */
if (sAccountMgr.GetSecurity(accountId) < SEC_ADMINISTRATOR) if (sAccountMgr.GetSecurity(accountId) < SEC_ADMINISTRATOR)
{ {
DEBUG_LOG("MaNGOSsoap: %s's gmlevel is too low", soap->userid); sLog.outString("SoapThread: %s's account security level is to low", soap->userid);
return 403; return 403;
} }
if (!command || !*command) if (!command || !*command)
{ {
return soap_sender_fault(soap, "Command mustn't be empty", "The supplied command was an empty string"); return soap_sender_fault(soap, "Command can not be empty", "The supplied command was an empty string");
} }
DEBUG_LOG("MaNGOSsoap: got command '%s'", command); sLog.outString("SoapThread: Recieved command %s", command);
SOAPCommand connection; SOAPCommand connection;
// commands are executed in the world thread. We have to wait for them to be completed // commands are executed in the world thread and have to wait till they are completed.
{ {
// CliCommandHolder will be deleted from world, accessing after queueing is NOT save
CliCommandHolder* cmd = new CliCommandHolder(accountId, SEC_CONSOLE, &connection, command, &SOAPCommand::print, &SOAPCommand::commandFinished); CliCommandHolder* cmd = new CliCommandHolder(accountId, SEC_CONSOLE, &connection, command, &SOAPCommand::print, &SOAPCommand::commandFinished);
sWorld.QueueCliCommand(cmd); sWorld.QueueCliCommand(cmd);
} }
ACE_OS::sleep(1); // wait until the command has finished executing
connection.finishedPromise.get_future().wait();
// the command has finished executing already
char* printBuffer = soap_strdup(soap, connection.m_printBuffer.c_str()); char* printBuffer = soap_strdup(soap, connection.m_printBuffer.c_str());
if (connection.hasCommandSucceeded()) if (connection.hasCommandSucceeded())
{ {

View File

@ -25,29 +25,47 @@
#ifndef MANGOS_H_SOAPTHREAD #ifndef MANGOS_H_SOAPTHREAD
#define MANGOS_H_SOAPTHREAD #define MANGOS_H_SOAPTHREAD
#include <ace/Task.h>
#include "Common.h" #include "Common.h"
#include "soapH.h"
class SoapPool; #include <mutex>
#include <future>
#include <string>
class SoapThread: public ACE_Task_Base void process_message(struct soap* soap_message);
void SoapThread(const std::string& host, uint16 port);
class SOAPCommand
{ {
enum { SOAP_THREADS = 1 }; //TODO: configure in mangosd.conf public:
SOAPCommand() : m_success(false) { }
~SOAPCommand() { }
public: void appendToPrintBuffer(std::string msg)
SoapThread(uint16 port, const char* host) : host_(host), port_(port), pool_(NULL) {} {
virtual ~SoapThread(); m_printBuffer += msg;
}
virtual int svc() override; void setCommandSuccess(bool val)
virtual int open(void*) override; {
m_success = val;
finishedPromise.set_value();
}
private: bool hasCommandSucceeded() const
const char *host_; {
uint16 port_; return m_success;
SoapPool *pool_; }
struct soap soap_;
static void print(void* callbackArg, const char* msg)
{
((SOAPCommand*)callbackArg)->appendToPrintBuffer(msg);
}
static void commandFinished(void* callbackArg, bool success);
bool m_success;
std::string m_printBuffer;
std::promise<void> finishedPromise;
}; };
#endif #endif

View File

@ -478,13 +478,14 @@ int main(int argc, char** argv)
// 3. Start the SOAP listener thread, if enabled // 3. Start the SOAP listener thread, if enabled
//************************************************************************************************************************ //************************************************************************************************************************
#ifdef ENABLE_SOAP #ifdef ENABLE_SOAP
SoapThread* soapThread = NULL; std::shared_ptr<std::thread> soapThread;
if (sConfig.GetBoolDefault("SOAP.Enabled", false)) if (sConfig.GetBoolDefault("SOAP.Enabled", false))
{ {
host = sConfig.GetStringDefault("SOAP.IP", "127.0.0.1"); soapThread.reset(new std::thread(SoapThread, sConfig.GetStringDefault("SOAP.IP", "127.0.0.1"), uint16(sConfig.GetIntDefault("SOAP.Port", 7878))), [](std::thread* thread)
port = sConfig.GetIntDefault("SOAP.Port", 7878); {
soapThread = new SoapThread(port, host.c_str()); thread->join();
soapThread->open(0); delete thread;
});
} }
#else /* ENABLE_SOAP */ #else /* ENABLE_SOAP */
if (sConfig.GetBoolDefault("SOAP.Enabled", false)) if (sConfig.GetBoolDefault("SOAP.Enabled", false))
@ -533,12 +534,6 @@ int main(int argc, char** argv)
delete freezeThread; delete freezeThread;
} }
#ifdef ENABLE_SOAP
if (soapThread)
{
delete soapThread;
}
#endif
if (raThread) if (raThread)
{ {
delete raThread; delete raThread;