Clean up fifo console

This commit is contained in:
def 2016-05-02 23:36:21 +02:00
parent 61e47422f6
commit 21ac41bfae
8 changed files with 129 additions and 132 deletions

View file

@ -42,7 +42,7 @@
#include <engine/shared/protocol.h>
#include <engine/shared/ringbuffer.h>
#include <engine/shared/snapshot.h>
#include <engine/shared/fifoconsole.h>
#include <engine/shared/fifo.h>
#include <game/version.h>
@ -2652,6 +2652,10 @@ void CClient::Run()
// process pending commands
m_pConsole->StoreCommands(false);
#if defined(CONF_FAMILY_UNIX)
m_Fifo.Init(m_pConsole, g_Config.m_ClInputFifo, CFGFLAG_CLIENT);
#endif
bool LastD = false;
bool LastQ = false;
bool LastE = false;
@ -2796,6 +2800,10 @@ void CClient::Run()
if(State() == IClient::STATE_QUITING)
break;
#if defined(CONF_FAMILY_UNIX)
m_Fifo.Update();
#endif
// beNice
if(g_Config.m_ClCpuThrottle)
net_socket_read_wait(m_NetClient[0].m_Socket, g_Config.m_ClCpuThrottle * 1000);
@ -2813,6 +2821,10 @@ void CClient::Run()
m_LocalTime = (time_get()-m_LocalStartTime)/(float)time_freq();
}
#if defined(CONF_FAMILY_UNIX)
m_Fifo.Shutdown();
#endif
GameClient()->OnShutdown();
Disconnect();
@ -3444,10 +3456,6 @@ int main(int argc, const char **argv) // ignore_convention
pClient->Engine()->InitLogfile();
#if defined(CONF_FAMILY_UNIX)
FifoConsole *fifoConsole = new FifoConsole(pConsole, g_Config.m_ClInputFifo, CFGFLAG_CLIENT);
#endif
#if defined(CONF_FAMILY_WINDOWS)
if(!g_Config.m_ClShowConsole)
FreeConsole();
@ -3460,10 +3468,6 @@ int main(int argc, const char **argv) // ignore_convention
dbg_msg("client", "starting...");
pClient->Run();
#if defined(CONF_FAMILY_UNIX)
delete fifoConsole;
#endif
// write down the config and quit
pConfig->Save();

View file

@ -194,6 +194,10 @@ class CClient : public IClient, public CDemoPlayer::IListener
char m_aDDNetSrvListToken[4];
bool m_DDNetSrvListTokenSet;
#if defined(CONF_FAMILY_UNIX)
CFifo m_Fifo;
#endif
public:
IEngine *Engine() { return m_pEngine; }
IEngineGraphics *Graphics() { return m_pGraphics; }

View file

@ -23,7 +23,7 @@
#include <engine/shared/packer.h>
#include <engine/shared/protocol.h>
#include <engine/shared/snapshot.h>
#include <engine/shared/fifoconsole.h>
#include <engine/shared/fifo.h>
#include <mastersrv/mastersrv.h>
@ -1522,6 +1522,9 @@ void CServer::PumpNetwork()
m_ServerBan.Update();
m_Econ.Update();
#if defined(CONF_FAMILY_UNIX)
m_Fifo.Update();
#endif
}
char *CServer::GetMapName()
@ -1642,6 +1645,10 @@ int CServer::Run()
m_Econ.Init(Console(), &m_ServerBan);
#if defined(CONF_FAMILY_UNIX)
m_Fifo.Init(Console(), g_Config.m_SvInputFifo, CFGFLAG_SERVER);
#endif
char aBuf[256];
str_format(aBuf, sizeof(aBuf), "server name is '%s'", g_Config.m_SvName);
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf);
@ -1802,9 +1809,13 @@ int CServer::Run()
{
if(m_aClients[i].m_State != CClient::STATE_EMPTY)
m_NetServer.Drop(i, "Server shutdown");
}
m_Econ.Shutdown();
}
#if defined(CONF_FAMILY_UNIX)
m_Fifo.Shutdown();
#endif
GameServer()->OnShutdown();
m_pMap->Unload();
@ -2237,10 +2248,6 @@ int main(int argc, const char **argv) // ignore_convention
pConsole->Register("sv_rescue", "", CFGFLAG_SERVER, CServer::ConRescue, pConsole, "Allow /rescue command so players can teleport themselves out of freeze");
pEngine->InitLogfile();
#if defined(CONF_FAMILY_UNIX)
FifoConsole *fifoConsole = new FifoConsole(pConsole, g_Config.m_SvInputFifo, CFGFLAG_SERVER);
#endif
pServer->InitRconPasswordIfEmpty();
// run the server
@ -2248,9 +2255,6 @@ int main(int argc, const char **argv) // ignore_convention
pServer->Run();
// free
#if defined(CONF_FAMILY_UNIX)
delete fifoConsole;
#endif
delete pServer;
delete pKernel;
delete pEngineMap;

View file

@ -15,6 +15,7 @@
#include <base/math.h>
#include <engine/shared/mapchecker.h>
#include <engine/shared/econ.h>
#include <engine/shared/fifo.h>
#include <engine/shared/netban.h>
class CSnapIDPool
@ -154,6 +155,9 @@ public:
CSnapIDPool m_IDPool;
CNetServer m_NetServer;
CEcon m_Econ;
#if defined(CONF_FAMILY_UNIX)
CFifo m_Fifo;
#endif
CServerBan m_ServerBan;
IEngineMap *m_pMap;

View file

@ -0,0 +1,70 @@
#include "fifo.h"
#if defined(CONF_FAMILY_UNIX)
#include <engine/shared/config.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
void CFifo::Init(IConsole *pConsole, char *pFifoFile, int Flag)
{
m_File = NULL;
m_pConsole = pConsole;
if(pFifoFile[0] == '\0')
return;
m_Flag = Flag;
mkfifo(pFifoFile, 0600);
struct stat attribute;
stat(pFifoFile, &attribute);
if(!S_ISFIFO(attribute.st_mode))
{
dbg_msg("fifo", "'%s' is not a fifo, removing", pFifoFile);
fs_remove(pFifoFile);
mkfifo(pFifoFile, 0600);
stat(pFifoFile, &attribute);
if(!S_ISFIFO(attribute.st_mode))
{
dbg_msg("fifo", "can't remove file '%s', quitting", pFifoFile);
exit(2);
}
}
int fileFD = open(pFifoFile, O_RDONLY|O_NONBLOCK);
if(fileFD >= 0)
m_File = fdopen(fileFD, "r");
if(m_File == NULL)
dbg_msg("fifo", "can't open file '%s'", pFifoFile);
}
void CFifo::Shutdown()
{
fclose(m_File);
}
void CFifo::Update()
{
if(m_File == NULL)
return;
char aBuf[8192];
while(true)
{
char *pResult = fgets(aBuf, sizeof(aBuf), m_File);
if(pResult != NULL)
m_pConsole->ExecuteLineFlag(pResult, m_Flag, -1);
else
break;
}
}
#endif

24
src/engine/shared/fifo.h Normal file
View file

@ -0,0 +1,24 @@
#ifndef ENGINE_SHARED_FIFO_H
#define ENGINE_SHARED_FIFO_H
#include <engine/console.h>
#if defined(CONF_FAMILY_UNIX)
#include <stdio.h> // FILE
class CFifo
{
IConsole *m_pConsole;
int m_Flag;
FILE *m_File;
public:
void Init(IConsole *pConsole, char *pFifoFile, int Flag);
void Update();
void Shutdown();
};
#endif
#endif

View file

@ -1,91 +0,0 @@
#include "fifoconsole.h"
#if defined(CONF_FAMILY_UNIX)
#include <engine/shared/config.h>
#include <fstream>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
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_init(ListenFifoThread, this);
m_pConsole = pConsole;
m_flag = flag;
thread_detach(m_pFifoThread);
}
FifoConsole::~FifoConsole()
{
if(m_pFifoFile[0] == '\0')
return;
lock_wait(gs_FifoLock);
gs_stopFifoThread = true;
lock_unlock(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_unlock(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, -1);
f.close();
}
}
#endif

View file

@ -1,22 +0,0 @@
#ifndef ENGINE_FIFOCONSOLE_H
#define ENGINE_FIFOCONSOLE_H
#include <engine/console.h>
#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