vdr-plugin-streamdev/tools/source.c

172 lines
3.2 KiB
C
Raw Permalink Normal View History

2004-12-30 23:43:55 +01:00
#include "tools/source.h"
#include "tools/select.h"
#include "common.h"
#include <vdr/tools.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
cTBSource::cTBSource(void) {
m_BytesRead = 0;
m_BytesWritten = 0;
m_Filed = -1;
}
bool cTBSource::Open(int Filed, bool IsUnixFd) {
if (IsOpen())
Close();
m_Filed = Filed;
if (IsUnixFd && ::fcntl(m_Filed, F_SETFL, O_NONBLOCK) == -1)
return false;
return true;
}
cTBSource::~cTBSource() {
}
bool cTBSource::Close(void) {
if (!IsOpen()) {
errno = EBADF;
return false;
}
m_Filed = -1;
return true;
}
ssize_t cTBSource::Read(void *Buffer, size_t Length) {
ssize_t res;
while ((res = SysRead(Buffer, Length)) < 0 && errno == EINTR)
errno = 0;
if (res > 0) m_BytesRead += res;
return res;
}
ssize_t cTBSource::Write(const void *Buffer, size_t Length) {
ssize_t res;
while ((res = SysWrite(Buffer, Length)) < 0 && errno == EINTR)
errno = 0;
if (res > 0) m_BytesWritten += res;
return res;
}
bool cTBSource::TimedWrite(const void *Buffer, size_t Length, uint TimeoutMs) {
cTBSelect sel;
int ms, offs;
2005-02-08 14:59:16 +01:00
cTimeMs starttime;
2004-12-30 23:43:55 +01:00
offs = 0;
sel.Clear();
sel.Add(m_Filed, true);
2004-12-30 23:43:55 +01:00
while (Length > 0) {
int b;
ms = TimeoutMs - starttime.Elapsed();
if (ms <= 0) {
errno = ETIMEDOUT;
return false;
}
2004-12-30 23:43:55 +01:00
if (sel.Select(ms) == -1)
return false;
if (sel.CanWrite(m_Filed)) {
if ((b = Write((char*)Buffer + offs, Length)) == -1)
return false;
offs += b;
Length -= b;
}
}
return true;
}
2005-02-08 20:54:52 +01:00
bool cTBSource::SafeWrite(const void *Buffer, size_t Length) {
cTBSelect sel;
int offs;
offs = 0;
sel.Clear();
sel.Add(m_Filed, true);
2005-02-08 20:54:52 +01:00
while (Length > 0) {
int b;
if (sel.Select() == -1)
return false;
if (sel.CanWrite(m_Filed)) {
if ((b = Write((char*)Buffer + offs, Length)) == -1)
return false;
offs += b;
Length -= b;
}
}
return true;
}
2004-12-30 23:43:55 +01:00
ssize_t cTBSource::ReadUntil(void *Buffer, size_t Length, const char *Seq,
uint TimeoutMs) {
int ms;
2005-02-08 18:22:35 +01:00
size_t len;
2004-12-30 23:43:55 +01:00
cTBSelect sel;
2005-02-08 18:22:35 +01:00
if ((len = m_LineBuffer.find(Seq)) != (size_t)-1) {
if (len > Length) {
2004-12-30 23:43:55 +01:00
errno = ENOBUFS;
return -1;
}
2005-02-08 18:22:35 +01:00
memcpy(Buffer, m_LineBuffer.data(), len);
m_LineBuffer.erase(0, len + strlen(Seq));
Dprintf("ReadUntil: Served from Linebuffer: %d, |%.*s|\n", len, len - 1,
2004-12-30 23:43:55 +01:00
(char*)Buffer);
2005-02-08 18:22:35 +01:00
return len;
2004-12-30 23:43:55 +01:00
}
2005-02-08 14:59:16 +01:00
cTimeMs starttime;
2004-12-30 23:43:55 +01:00
ms = TimeoutMs;
sel.Clear();
sel.Add(m_Filed, false);
2005-02-08 18:22:35 +01:00
while (m_LineBuffer.size() < BUFSIZ) {
2004-12-30 23:43:55 +01:00
if (sel.Select(ms) == -1)
return -1;
if (sel.CanRead(m_Filed)) {
2005-02-08 18:22:35 +01:00
int b;
len = m_LineBuffer.size();
m_LineBuffer.resize(BUFSIZ);
if ((b = Read((char*)m_LineBuffer.data() + len, BUFSIZ - len)) == -1) {
m_LineBuffer.resize(len);
2004-12-30 23:43:55 +01:00
return -1;
}
2005-02-08 18:22:35 +01:00
m_LineBuffer.resize(len + b);
2004-12-30 23:43:55 +01:00
2005-02-08 18:22:35 +01:00
if ((len = m_LineBuffer.find(Seq)) != (size_t)-1) {
if (len > Length) {
2004-12-30 23:43:55 +01:00
errno = ENOBUFS;
return -1;
}
2005-02-08 18:22:35 +01:00
memcpy(Buffer, m_LineBuffer.data(), len);
m_LineBuffer.erase(0, len + strlen(Seq));
Dprintf("ReadUntil: Served from Linebuffer: %d, |%.*s|\n", len, len - 1,
2004-12-30 23:43:55 +01:00
(char*)Buffer);
2005-02-08 18:22:35 +01:00
return len;
2004-12-30 23:43:55 +01:00
}
}
2005-02-08 14:59:16 +01:00
ms = TimeoutMs - starttime.Elapsed();
2004-12-30 23:43:55 +01:00
if (ms <= 0) {
errno = ETIMEDOUT;
return -1;
}
}
errno = ENOBUFS;
return -1;
}