ddnet/src/engine/shared/fifoconsole.cpp

59 lines
1 KiB
C++
Raw Normal View History

2013-07-29 19:03:59 +00:00
#include "fifoconsole.h"
#include <engine/shared/config.h>
#include <fstream>
2013-08-06 04:45:13 +00:00
#if defined(CONF_FAMILY_UNIX)
2014-08-29 14:52:08 +00:00
2013-07-29 19:03:59 +00:00
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
2014-08-29 14:52:08 +00:00
extern bool IsClient;
2013-07-29 19:03:59 +00:00
FifoConsole::FifoConsole(IConsole *pConsole)
{
2013-08-03 15:22:50 +00:00
void *m_pFifoThread = thread_create(ListenFifoThread, pConsole);
pthread_detach((pthread_t)m_pFifoThread);
2013-07-29 19:03:59 +00:00
}
void FifoConsole::ListenFifoThread(void *pUser)
{
IConsole *pConsole = (IConsole *)pUser;
2014-08-29 14:52:08 +00:00
char *fifofile;
if (IsClient)
fifofile = g_Config.m_ClInputFifo;
else
fifofile = g_Config.m_SvInputFifo;
if (str_comp(fifofile, "") == 0)
2013-07-29 19:03:59 +00:00
return;
2014-08-29 14:52:08 +00:00
mkfifo(fifofile, 0600);
2013-07-29 19:03:59 +00:00
struct stat attribute;
2014-08-29 14:52:08 +00:00
stat(fifofile, &attribute);
if(!S_ISFIFO(attribute.st_mode))
return;
2013-07-29 19:03:59 +00:00
std::ifstream f;
2014-04-18 22:40:21 +00:00
char aBuf[8192];
2013-07-29 19:03:59 +00:00
while (true)
{
2014-08-29 14:52:08 +00:00
f.open(fifofile);
2014-04-18 22:40:21 +00:00
while (f.getline(aBuf, sizeof(aBuf)))
2013-07-29 19:03:59 +00:00
{
2014-08-29 14:52:08 +00:00
if (IsClient)
pConsole->ExecuteLineFlag(aBuf, CFGFLAG_CLIENT, -1);
else
pConsole->ExecuteLineFlag(aBuf, CFGFLAG_SERVER, -1);
2013-07-29 19:03:59 +00:00
}
f.close();
}
}
2013-08-06 04:44:53 +00:00
#endif