ddnet/src/engine/shared/fifoconsole.cpp

92 lines
1.7 KiB
C++
Raw Normal View History

2013-07-29 19:03:59 +00:00
#include "fifoconsole.h"
#if defined(CONF_FAMILY_UNIX)
2013-07-29 19:03:59 +00:00
#include <engine/shared/config.h>
#include <fstream>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
2013-07-29 19:03:59 +00:00
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
{
2015-05-07 15:14:03 +00:00
m_pFifoFile = pFifoFile;
if(m_pFifoFile[0] == '\0')
return;
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
2015-05-31 15:36:00 +00:00
m_pFifoThread = thread_init(ListenFifoThread, this);
m_pConsole = pConsole;
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
}
2014-12-06 13:14:46 +00:00
FifoConsole::~FifoConsole()
{
2015-05-07 15:14:03 +00:00
if(m_pFifoFile[0] == '\0')
return;
2014-12-06 13:34:30 +00:00
lock_wait(gs_FifoLock);
gs_stopFifoThread = true;
lock_unlock(gs_FifoLock);
2014-12-06 13:34:30 +00:00
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)
{
dbg_msg("fifo", "FIFO not properly initialized");
exit(2);
}
2014-12-06 13:34:30 +00:00
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);
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);
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