From 381edc0a3a6164a91bf86894200d6303dfee90ed Mon Sep 17 00:00:00 2001 From: "False.Genesis" Date: Fri, 15 Jun 2007 17:36:17 +0000 Subject: [PATCH] * added missing files from #136. thx bLuma! --- src/Client/ControlSocket.cpp | 59 +++++++++++++++++++++++++++++++++ src/Client/ControlSocket.h | 23 +++++++++++++ src/Client/RemoteController.cpp | 38 +++++++++++++++++++++ src/Client/RemoteController.h | 37 +++++++++++++++++++++ 4 files changed, 157 insertions(+) create mode 100644 src/Client/ControlSocket.cpp create mode 100644 src/Client/ControlSocket.h create mode 100644 src/Client/RemoteController.cpp create mode 100644 src/Client/RemoteController.h diff --git a/src/Client/ControlSocket.cpp b/src/Client/ControlSocket.cpp new file mode 100644 index 0000000..c72d2b5 --- /dev/null +++ b/src/Client/ControlSocket.cpp @@ -0,0 +1,59 @@ +#include "common.h" +#include "PseuWoW.h" +#include "RemoteController.h" +#include "ControlSocket.h" + +ControlSocket::ControlSocket(SocketHandler& h) : TcpSocket(h) +{ + _ok = false; + DEBUG(logdebug("ControlSocket created")); +} + +void ControlSocket::OnAccept(void) +{ + logdetail("ControlSocket: Incoming connection from %s:%u [host:%s]",GetRemoteAddress().c_str(),GetRemotePort(),GetRemoteHostname().c_str()); + + // must perform some crappy ptr conversion here, doesnt want to typecast SocketHandler -> ControlSocketHandler directly + SocketHandler& hnd = Handler(); + ControlSocketHandler *chnd = static_cast(&hnd); + _instance = chnd->GetInstance(); + DEBUG(logdebug("ControlSocket: setting instance = %X",_instance)); + + // accept only connections from one host for now, if set + if(_instance->GetConf()->rmcontrolhost.length() + && !(GetRemoteAddress() == _instance->GetConf()->rmcontrolhost || GetRemoteHostname() == _instance->GetConf()->rmcontrolhost)) + { + logdetail("ControlSocket: connection rejected. closing."); + SetCloseAndDelete(true); + return; + } + + _ok = true; +} + +void ControlSocket::OnRead(void) +{ + if(!_ok) + return; + TcpSocket::OnRead(); + uint32 len = ibuf.GetLength(); + if(!len) + { + logdetail("ControlSocket: connection to %s:%u closed.",GetRemoteAddress().c_str(),GetRemotePort()); + return; + } + + char *buf = new char[len]; + ibuf.Read(buf,len); + if(buf[0]==0) + { + // reserved for future applications + } + else + { + if(_instance && _instance->GetScripts()) + _instance->GetScripts()->RunSingleLine(&buf[0]); + } + + delete [] buf; +} diff --git a/src/Client/ControlSocket.h b/src/Client/ControlSocket.h new file mode 100644 index 0000000..213cba1 --- /dev/null +++ b/src/Client/ControlSocket.h @@ -0,0 +1,23 @@ +#ifndef CONTROLSOCKET_H +#define CONTROLSOCKET_H + +#include "Network/TcpSocket.h" +#include "RemoteController.h" + +class ControlSocket : public TcpSocket +{ +public: + ControlSocket(SocketHandler& h); + + void SetInstance(PseuInstance *in) { _instance = in; } + + void OnAccept(); + void OnRead(); + +private: + PseuInstance *_instance; + bool _ok; +}; + + +#endif diff --git a/src/Client/RemoteController.cpp b/src/Client/RemoteController.cpp new file mode 100644 index 0000000..421c20c --- /dev/null +++ b/src/Client/RemoteController.cpp @@ -0,0 +1,38 @@ +#include "common.h" +#include "log.h" +#include "ControlSocket.h" +#include "Network/ListenSocket.h" +#include "RemoteController.h" + +RemoteController::RemoteController(PseuInstance *in,uint32 port) +{ + DEBUG(logdebug("RemoteController: setting instance = %X",in)); + h.SetInstance(in); + _mustdie = false; + _instance = in; + ListenSocket *ls = new ListenSocket(h); + if(ls->Bind(port)) + { + logerror("RemoteController: Can't bind to port %u",port); + _mustdie = true; + return; + } + h.Add(ls); + log("RemoteController: listening on port %u",port); +} + +RemoteController::~RemoteController() +{ + DEBUG(logdebug("~RemoteController()")); +} + +void RemoteController::Update(void) +{ + if(_mustdie) + return; + h.Select(0,0); +} + + + + diff --git a/src/Client/RemoteController.h b/src/Client/RemoteController.h new file mode 100644 index 0000000..abbd931 --- /dev/null +++ b/src/Client/RemoteController.h @@ -0,0 +1,37 @@ +#ifndef REMOTECONTROLLER_H +#define REMOTECONTROLLER_H + +#include "Network/SocketHandler.h" + +class PseuInstance; +class ControlSocket; + +class ControlSocketHandler : public SocketHandler +{ +public: + void SetInstance(PseuInstance *in) { _instance = in; } + PseuInstance *GetInstance(void) { return _instance; } +private: + PseuInstance *_instance; + +}; + + + +class RemoteController +{ +public: + RemoteController(PseuInstance*,uint32 port); + ~RemoteController(); + void SetPermission(uint8 p) { _perm = p; } + void Update(void); + bool MustDie(void) { return _mustdie; } + +private: + ControlSocketHandler h; + bool _mustdie; + PseuInstance *_instance; + uint8 _perm; +}; + +#endif \ No newline at end of file