From 47e5064885a6dc700196e70aa28372b1d2bb6781 Mon Sep 17 00:00:00 2001 From: def Date: Fri, 14 Aug 2015 23:15:42 +0200 Subject: Add fifo console support for server (from DDNet) --- objs/createdir.txt | 0 src/engine/server/server.cpp | 8 ++++ src/engine/shared/config_variables.h | 2 +- src/engine/shared/fifoconsole.cpp | 91 ++++++++++++++++++++++++++++++++++++ src/engine/shared/fifoconsole.h | 22 +++++++++ 5 files changed, 122 insertions(+), 1 deletion(-) delete mode 100644 objs/createdir.txt create mode 100644 src/engine/shared/fifoconsole.cpp create mode 100644 src/engine/shared/fifoconsole.h diff --git a/objs/createdir.txt b/objs/createdir.txt deleted file mode 100644 index e69de29b..00000000 diff --git a/src/engine/server/server.cpp b/src/engine/server/server.cpp index f62ceb4d..b830dfe8 100644 --- a/src/engine/server/server.cpp +++ b/src/engine/server/server.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include @@ -2310,11 +2311,18 @@ int main(int argc, const char **argv) // ignore_convention pEngine->InitLogfile(); +#if defined(CONF_FAMILY_UNIX) + FifoConsole *fifoConsole = new FifoConsole(pConsole, g_Config.m_SvInputFifo, CFGFLAG_SERVER); +#endif + // run the server dbg_msg("server", "starting..."); pServer->Run(); // free +#if defined(CONF_FAMILY_UNIX) + delete fifoConsole; +#endif delete pServer; delete pKernel; delete pEngineMap; diff --git a/src/engine/shared/config_variables.h b/src/engine/shared/config_variables.h index ee517c91..31713888 100644 --- a/src/engine/shared/config_variables.h +++ b/src/engine/shared/config_variables.h @@ -105,7 +105,7 @@ MACRO_CONFIG_INT(EcOutputLevel, ec_output_level, 1, 0, 2, CFGFLAG_ECON, "Adjusts MACRO_CONFIG_INT(SvGlobalBantime, sv_global_bantime, 60, 0, 1440, CFGFLAG_SERVER, "The time a client gets banned if the ban server reports it. 0 to disable") MACRO_CONFIG_INT(SvRespawnProtection, sv_respawn_protection, 1, 0, 1, CFGFLAG_SERVER, "Whether a player gets respawn protection") - +MACRO_CONFIG_STR(SvInputFifo, sv_input_fifo, 128, "", CFGFLAG_SERVER, "Fifo file to use as input for server console") #if defined(CONF_SQL) //zCatch mysql. diff --git a/src/engine/shared/fifoconsole.cpp b/src/engine/shared/fifoconsole.cpp new file mode 100644 index 00000000..e031b142 --- /dev/null +++ b/src/engine/shared/fifoconsole.cpp @@ -0,0 +1,91 @@ +#include "fifoconsole.h" + +#if defined(CONF_FAMILY_UNIX) + +#include + +#include + +#include +#include +#include +#include + +static LOCK gs_FifoLock = 0; +static volatile bool gs_stopFifoThread = false; + +FifoConsole::FifoConsole(IConsole *pConsole, char *pFifoFile, int flag) +{ + m_pFifoFile = pFifoFile; + if(m_pFifoFile[0] == '\0') + return; + + gs_stopFifoThread = false; + if(gs_FifoLock == 0) + gs_FifoLock = lock_create(); + + m_pFifoThread = thread_create(ListenFifoThread, this); + m_pConsole = pConsole; + m_flag = flag; + + pthread_detach((pthread_t)m_pFifoThread); +} + +FifoConsole::~FifoConsole() +{ + if(m_pFifoFile[0] == '\0') + return; + + lock_wait(gs_FifoLock); + gs_stopFifoThread = true; + lock_release(gs_FifoLock); + gs_FifoLock = 0; +} + +void FifoConsole::ListenFifoThread(void *pUser) +{ + FifoConsole *pData = (FifoConsole *)pUser; + + if(!gs_FifoLock) + { + dbg_msg("fifo", "FIFO not properly initialized"); + exit(2); + } + + lock_wait(gs_FifoLock); + if(gs_stopFifoThread) + return; + + mkfifo(pData->m_pFifoFile, 0600); + + struct stat attribute; + stat(pData->m_pFifoFile, &attribute); + + if(!S_ISFIFO(attribute.st_mode)) + { + dbg_msg("fifo", "'%s' is not a FIFO, removing", pData->m_pFifoFile); + fs_remove(pData->m_pFifoFile); + mkfifo(pData->m_pFifoFile, 0600); + stat(pData->m_pFifoFile, &attribute); + + if(!S_ISFIFO(attribute.st_mode)) + { + dbg_msg("fifo", "Can't remove file, quitting"); + exit(2); + } + } + + lock_release(gs_FifoLock); + + std::ifstream f; + char aBuf[8192]; + + while (true) + { + f.open(pData->m_pFifoFile); + while (f.getline(aBuf, sizeof(aBuf))) + pData->m_pConsole->ExecuteLineFlag(aBuf, pData->m_flag); + f.close(); + } +} +#endif diff --git a/src/engine/shared/fifoconsole.h b/src/engine/shared/fifoconsole.h new file mode 100644 index 00000000..83d14bc0 --- /dev/null +++ b/src/engine/shared/fifoconsole.h @@ -0,0 +1,22 @@ +#ifndef ENGINE_FIFOCONSOLE_H +#define ENGINE_FIFOCONSOLE_H + +#include + +#if defined(CONF_FAMILY_UNIX) + +class FifoConsole +{ + static void ListenFifoThread(void *pUser); + IConsole *m_pConsole; + void *m_pFifoThread; + char *m_pFifoFile; + int m_flag; + +public: + FifoConsole(IConsole *pConsole, char *pFifoFile, int flag); + ~FifoConsole(); +}; +#endif + +#endif // FILE_ENGINE_FIFOCONSOLE_H -- cgit 1.4.1