reopen pipe on error

This commit is contained in:
badaix 2016-02-08 12:49:09 +01:00
parent 8ac895eeb3
commit 73a9223c6f

View file

@ -25,6 +25,7 @@
#include "../encoder/encoderFactory.h" #include "../encoder/encoderFactory.h"
#include "common/log.h" #include "common/log.h"
#include "common/snapException.h" #include "common/snapException.h"
#include "common/compat.h"
using namespace std; using namespace std;
@ -32,19 +33,18 @@ using namespace std;
PipeReader::PipeReader(PcmListener* pcmListener, const ReaderUri& uri) : PcmReader(pcmListener, uri) PipeReader::PipeReader(PcmListener* pcmListener, const ReaderUri& uri) : PcmReader(pcmListener, uri), fd_(-1)
{ {
umask(0); umask(0);
mkfifo(uri_.path.c_str(), 0666); if ((mkfifo(uri_.path.c_str(), 0666) != 0) && (errno != EEXIST))
fd_ = open(uri_.path.c_str(), O_RDONLY | O_NONBLOCK); throw SnapException("failed to make fifo \"" + uri_.path + "\": " + cpt::to_string(errno));
if (fd_ == -1)
throw SnapException("failed to open fifo: \"" + uri_.path + "\"");
} }
PipeReader::~PipeReader() PipeReader::~PipeReader()
{ {
close(fd_); if (fd_ != -1)
close(fd_);
} }
@ -55,11 +55,17 @@ void PipeReader::worker()
while (active_) while (active_)
{ {
if (fd_ != -1)
close(fd_);
fd_ = open(uri_.path.c_str(), O_RDONLY | O_NONBLOCK);
gettimeofday(&tvChunk, NULL); gettimeofday(&tvChunk, NULL);
tvEncodedChunk_ = tvChunk; tvEncodedChunk_ = tvChunk;
long nextTick = chronos::getTickCount(); long nextTick = chronos::getTickCount();
try try
{ {
if (fd_ == -1)
throw SnapException("failed to open fifo: \"" + uri_.path + "\"");
while (active_) while (active_)
{ {
chunk->timestamp.sec = tvChunk.tv_sec; chunk->timestamp.sec = tvChunk.tv_sec;
@ -69,9 +75,10 @@ void PipeReader::worker()
do do
{ {
int count = read(fd_, chunk->payload + len, toRead - len); int count = read(fd_, chunk->payload + len, toRead - len);
if (count < 0)
if (count <= 0)
usleep(100*1000); usleep(100*1000);
else if (count == 0)
throw SnapException("end of file");
else else
len += count; len += count;
} }
@ -99,6 +106,7 @@ void PipeReader::worker()
catch(const std::exception& e) catch(const std::exception& e)
{ {
logE << "Exception: " << e.what() << std::endl; logE << "Exception: " << e.what() << std::endl;
usleep(100*1000);
} }
} }
} }