ddnet/src/engine/shared/fifoconsole.cpp

70 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:34:30 +00:00
static LOCK gs_FifoLock = 0;
static volatile bool gs_stopFifoThread = false;
2014-12-06 13:14:46 +00:00
2014-08-29 17:42:34 +00:00
FifoConsole::FifoConsole(IConsole *pConsole, char *pFifoFile, int flag)
2013-07-29 19:03:59 +00:00
{
m_pFifoThread = thread_create(ListenFifoThread, this);
2014-08-29 17:42:34 +00:00
m_pConsole = pConsole;
m_pFifoFile = pFifoFile;
m_flag = flag;
2014-12-06 13:14:46 +00:00
2014-12-06 13:34:30 +00:00
gs_stopFifoThread = false;
if(gs_FifoLock == 0)
gs_FifoLock = lock_create();
2014-12-06 13:14:46 +00:00
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()
{
2014-12-06 13:34:30 +00:00
lock_wait(gs_FifoLock);
gs_stopFifoThread = true;
lock_release(gs_FifoLock);
gs_FifoLock = 0;
2014-12-06 13:14:46 +00:00
}
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:34:30 +00:00
if(!gs_FifoLock)
return;
lock_wait(gs_FifoLock);
if(gs_stopFifoThread)
2014-12-06 13:18: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);
2014-12-06 13:34:30 +00:00
lock_release(gs_FifoLock);
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