ddnet/src/engine/shared/fifoconsole.cpp

67 lines
1.3 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-12-06 13:14:46 +00:00
static volatile bool stopFifoThread = false;
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;
2014-12-06 13:14:46 +00:00
stopFifoThread = false;
2013-08-03 15:22:50 +00:00
pthread_detach((pthread_t)m_pFifoThread);
2013-07-29 19:03:59 +00:00
}
2014-12-06 13:14:46 +00:00
FifoConsole::~FifoConsole()
{
stopFifoThread = true;
//pthread_wait((pthread_t)m_pFifoThread, NULL);
}
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-12-06 13:18:59 +00:00
if(stopFifoThread)
return;
2014-11-26 20:18:11 +00:00
// This should fix the problem where sometimes the fifo thread runs at a bad
// time and can't open the fifo immediately.
while (!pData->m_pFifoFile || str_comp(pData->m_pFifoFile, "") == 0)
2014-12-06 13:14:46 +00:00
{
thread_sleep(1000);
2014-12-06 13:14:46 +00:00
}
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