ddnet/src/engine/shared/fifoconsole.cpp

49 lines
935 B
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 17:42:34 +00:00
FifoConsole::FifoConsole(IConsole *pConsole, char *pFifoFile, int flag)
2013-07-29 19:03:59 +00:00
{
2014-08-29 17:42:34 +00:00
void *m_pFifoThread = thread_create(ListenFifoThread, this);
m_pConsole = pConsole;
m_pFifoFile = pFifoFile;
m_flag = flag;
2013-08-03 15:22:50 +00:00
pthread_detach((pthread_t)m_pFifoThread);
2013-07-29 19:03:59 +00:00
}
void FifoConsole::ListenFifoThread(void *pUser)
{
2014-08-29 17:42:34 +00:00
FifoConsole *pData = (FifoConsole *)pUser;
2014-08-29 14:52:08 +00:00
2014-08-29 17:42:34 +00:00
if (str_comp(pData->m_pFifoFile, "") == 0)
2013-07-29 19:03:59 +00:00
return;
2014-08-29 17:42:34 +00:00
mkfifo(pData->m_pFifoFile, 0600);
2013-07-29 19:03:59 +00:00
struct stat attribute;
2014-08-29 17:42:34 +00:00
stat(pData->m_pFifoFile, &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 17:42:34 +00:00
f.open(pData->m_pFifoFile);
2014-04-18 22:40:21 +00:00
while (f.getline(aBuf, sizeof(aBuf)))
2014-08-29 17:42:34 +00:00
pData->m_pConsole->ExecuteLineFlag(aBuf, pData->m_flag, -1);
2013-07-29 19:03:59 +00:00
f.close();
}
}
2013-08-06 04:44:53 +00:00
#endif