ddnet/src/engine/shared/fifoconsole.cpp
eeeee fabcfc1cbc Update fifoconsole.cpp
actually store the pointer in the field
2014-12-21 17:21:19 -08:00

70 lines
1.3 KiB
C++

#include "fifoconsole.h"
#include <engine/shared/config.h>
#include <fstream>
#if defined(CONF_FAMILY_UNIX)
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
static LOCK gs_FifoLock = 0;
static volatile bool gs_stopFifoThread = false;
FifoConsole::FifoConsole(IConsole *pConsole, char *pFifoFile, int flag)
{
m_pFifoThread = thread_create(ListenFifoThread, this);
m_pConsole = pConsole;
m_pFifoFile = pFifoFile;
m_flag = flag;
gs_stopFifoThread = false;
if(gs_FifoLock == 0)
gs_FifoLock = lock_create();
pthread_detach((pthread_t)m_pFifoThread);
}
FifoConsole::~FifoConsole()
{
lock_wait(gs_FifoLock);
gs_stopFifoThread = true;
lock_release(gs_FifoLock);
gs_FifoLock = 0;
}
void FifoConsole::ListenFifoThread(void *pUser)
{
FifoConsole *pData = (FifoConsole *)pUser;
if(!gs_FifoLock)
return;
lock_wait(gs_FifoLock);
if(gs_stopFifoThread)
return;
mkfifo(pData->m_pFifoFile, 0600);
struct stat attribute;
stat(pData->m_pFifoFile, &attribute);
lock_release(gs_FifoLock);
if(!S_ISFIFO(attribute.st_mode))
return;
std::ifstream f;
char aBuf[8192];
while (true)
{
f.open(pData->m_pFifoFile);
while (f.getline(aBuf, sizeof(aBuf)))
pData->m_pConsole->ExecuteLineFlag(aBuf, pData->m_flag, -1);
f.close();
}
}
#endif