2004-12-30 23:43:55 +01:00
|
|
|
#include "tools/select.h"
|
|
|
|
|
|
|
|
#include <vdr/tools.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
cTBSelect::cTBSelect(void) {
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
cTBSelect::~cTBSelect() {
|
|
|
|
}
|
|
|
|
|
|
|
|
int cTBSelect::Select(uint TimeoutMs) {
|
|
|
|
struct timeval tv;
|
|
|
|
ssize_t res;
|
|
|
|
int ms;
|
|
|
|
|
|
|
|
tv.tv_usec = (TimeoutMs % 1000) * 1000;
|
|
|
|
tv.tv_sec = TimeoutMs / 1000;
|
2007-04-02 12:32:34 +02:00
|
|
|
memcpy(m_FdsResult, m_FdsQuery, sizeof(m_FdsResult));
|
2004-12-30 23:43:55 +01:00
|
|
|
|
|
|
|
if (TimeoutMs == 0)
|
2007-04-02 12:32:34 +02:00
|
|
|
return ::select(m_MaxFiled + 1, &m_FdsResult[0], &m_FdsResult[1], NULL, &tv);
|
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;
|
2007-04-02 12:32:34 +02:00
|
|
|
while (ms > 0 && (res = ::select(m_MaxFiled + 1, &m_FdsResult[0], &m_FdsResult[1], NULL,
|
2004-12-30 23:43:55 +01:00
|
|
|
&tv)) == -1 && errno == EINTR) {
|
2005-02-08 14:59:16 +01:00
|
|
|
ms = TimeoutMs - starttime.Elapsed();
|
2004-12-30 23:43:55 +01:00
|
|
|
tv.tv_usec = (ms % 1000) * 1000;
|
|
|
|
tv.tv_sec = ms / 1000;
|
2007-04-02 12:32:34 +02:00
|
|
|
memcpy(m_FdsResult, m_FdsQuery, sizeof(m_FdsResult));
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
2007-04-02 12:32:34 +02:00
|
|
|
if (ms <= 0 || res == 0) {
|
2004-12-30 23:43:55 +01:00
|
|
|
errno = ETIMEDOUT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cTBSelect::Select(void) {
|
|
|
|
ssize_t res;
|
2007-04-02 12:32:34 +02:00
|
|
|
do {
|
|
|
|
memcpy(m_FdsResult, m_FdsQuery, sizeof(m_FdsResult));
|
|
|
|
} while ((res = ::select(m_MaxFiled + 1, &m_FdsResult[0], &m_FdsResult[1], NULL, NULL)) == -1 && errno == EINTR);
|
2004-12-30 23:43:55 +01:00
|
|
|
return res;
|
|
|
|
}
|