diff --git a/common.c b/common.c index 99cfe50..7616cd5 100644 --- a/common.c +++ b/common.c @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: common.c,v 1.3 2007/10/10 19:41:10 rahrenbe Exp $ + * $Id: common.c,v 1.4 2007/10/20 20:27:58 ajhseppa Exp $ */ #include @@ -39,6 +39,29 @@ const char *id_pid(const u_short Pid) return "---"; } +int selectSingleDesc(int descriptor, const int usecs, const bool selectWrite) +{ + // Wait for data + struct timeval tv; + tv.tv_sec = 0; + tv.tv_usec = usecs; + // Use select + fd_set fds; + FD_ZERO(&fds); + FD_SET(descriptor, &fds); + int retval = 0; + if (selectWrite) + retval = select(descriptor + 1, NULL, &fds, NULL, &tv); + else + retval = select(descriptor + 1, &fds, NULL, NULL, &tv); + // Check if error + if (retval < 0) { + char tmp[64]; + error("ERROR: select(): %s", strerror_r(errno, tmp, sizeof(tmp))); + } + return retval; +} + const section_filter_table_type section_filter_table[SECTION_FILTER_TABLE_SIZE] = { /* description tag pid tid mask */ diff --git a/common.h b/common.h index e3fb793..f03a893 100644 --- a/common.h +++ b/common.h @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: common.h,v 1.9 2007/10/10 19:41:10 rahrenbe Exp $ + * $Id: common.h,v 1.10 2007/10/20 20:27:59 ajhseppa Exp $ */ #ifndef __IPTV_COMMON_H @@ -32,6 +32,7 @@ uint16_t ts_pid(const uint8_t *buf); uint8_t payload(const uint8_t *tsp); const char *id_pid(const u_short Pid); +int selectSingleDesc(int descriptor, const int usecs, const bool selectWrite); struct section_filter_table_type { const char *description; diff --git a/protocolext.c b/protocolext.c index 98397a3..cc068af 100644 --- a/protocolext.c +++ b/protocolext.c @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: protocolext.c,v 1.12 2007/10/20 17:26:46 rahrenbe Exp $ + * $Id: protocolext.c,v 1.13 2007/10/20 20:27:59 ajhseppa Exp $ */ #include @@ -196,20 +196,10 @@ int cIptvProtocolExt::Read(unsigned char* *BufferAddr) // Set argument point to read buffer *BufferAddr = readBuffer; // Wait for data - struct timeval tv; - tv.tv_sec = 0; - tv.tv_usec = 500000; - // Use select - fd_set rfds; - FD_ZERO(&rfds); - FD_SET(socketDesc, &rfds); - int retval = select(socketDesc + 1, &rfds, NULL, NULL, &tv); + int retval = selectSingleDesc(socketDesc, 500000, false); // Check if error - if (retval < 0) { - char tmp[64]; - error("ERROR: select(): %s", strerror_r(errno, tmp, sizeof(tmp))); + if (retval < 0) return retval; - } // Check if data available else if (retval) { // Read data from socket diff --git a/protocolhttp.c b/protocolhttp.c index 8da27fb..3528a3d 100644 --- a/protocolhttp.c +++ b/protocolhttp.c @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: protocolhttp.c,v 1.13 2007/10/20 11:36:21 ajhseppa Exp $ + * $Id: protocolhttp.c,v 1.14 2007/10/20 20:27:59 ajhseppa Exp $ */ #include @@ -141,22 +141,11 @@ bool cIptvProtocolHttp::Connect(void) return false; } - // Select on the socket completion - struct timeval tv; - tv.tv_sec = 1; - tv.tv_usec = 0; - // Use select to check socket writability - fd_set wfds; - FD_ZERO(&wfds); - FD_SET(socketDesc, &wfds); - int retval = select(socketDesc + 1, NULL, &wfds, NULL, &tv); - // Check if error - if (retval < 0) { - char tmp[64]; - error("ERROR: select(): %s", strerror_r(errno, tmp, sizeof(tmp))); - return -1; - } - + // Select on the socket completion, check if it is writable + int retval = selectSingleDesc(socketDesc, 800000, true); + if (retval < 0) + return retval; + // Select has returned. Get socket errors if there are any int socketStatus = 0; socklen_t len = sizeof(socketStatus); @@ -229,22 +218,11 @@ bool cIptvProtocolHttp::GetHeaderLine(char* dest, unsigned int destLen, while (!newline || !linefeed) { socklen_t addrlen = sizeof(sockAddr); - // Set argument point to read buffer // Wait for data - struct timeval tv; - tv.tv_sec = 0; - tv.tv_usec = 500000; - // Use select - fd_set rfds; - FD_ZERO(&rfds); - FD_SET(socketDesc, &rfds); - int retval = select(socketDesc + 1, &rfds, NULL, NULL, &tv); + int retval = selectSingleDesc(socketDesc, 500000, false); // Check if error - if (retval < 0) { - char tmp[64]; - error("ERROR: select(): %s", strerror_r(errno, tmp, sizeof(tmp))); + if (retval < 0) return false; - } // Check if data available else if (retval) { int retval = recvfrom(socketDesc, bufptr, 1, MSG_DONTWAIT, @@ -316,20 +294,10 @@ int cIptvProtocolHttp::Read(unsigned char* *BufferAddr) // Set argument point to read buffer *BufferAddr = readBuffer; // Wait for data - struct timeval tv; - tv.tv_sec = 0; - tv.tv_usec = 500000; - // Use select - fd_set rfds; - FD_ZERO(&rfds); - FD_SET(socketDesc, &rfds); - int retval = select(socketDesc + 1, &rfds, NULL, NULL, &tv); + int retval = selectSingleDesc(socketDesc, 500000, false); // Check if error - if (retval < 0) { - char tmp[64]; - error("ERROR: select(): %s", strerror_r(errno, tmp, sizeof(tmp))); + if (retval < 0) return retval; - } // Check if data available else if (retval) { // Read data from socket diff --git a/protocoludp.c b/protocoludp.c index 016dbf9..4768a9a 100644 --- a/protocoludp.c +++ b/protocoludp.c @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: protocoludp.c,v 1.14 2007/10/20 11:36:21 ajhseppa Exp $ + * $Id: protocoludp.c,v 1.15 2007/10/20 20:27:59 ajhseppa Exp $ */ #include @@ -164,20 +164,10 @@ int cIptvProtocolUdp::Read(unsigned char* *BufferAddr) // Set argument point to read buffer *BufferAddr = readBuffer; // Wait for data - struct timeval tv; - tv.tv_sec = 0; - tv.tv_usec = 500000; - // Use select - fd_set rfds; - FD_ZERO(&rfds); - FD_SET(socketDesc, &rfds); - int retval = select(socketDesc + 1, &rfds, NULL, NULL, &tv); + int retval = selectSingleDesc(socketDesc, 500000, false); // Check if error - if (retval < 0) { - char tmp[64]; - error("ERROR: select(): %s", strerror_r(errno, tmp, sizeof(tmp))); + if (retval < 0) return retval; - } // Check if data available else if (retval) { int len = 0; diff --git a/sectionfilter.c b/sectionfilter.c index c860253..92d4972 100644 --- a/sectionfilter.c +++ b/sectionfilter.c @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: sectionfilter.c,v 1.11 2007/10/08 16:24:48 rahrenbe Exp $ + * $Id: sectionfilter.c,v 1.12 2007/10/20 20:27:59 ajhseppa Exp $ */ #include "sectionfilter.h" @@ -97,21 +97,10 @@ int cIptvSectionFilter::dmxdev_section_callback(const uint8_t *buffer1, size_t b const uint8_t *buffer2, size_t buffer2_len, enum dmx_success success) { - struct timeval tv; - tv.tv_sec = 0; - tv.tv_usec = 0; - fd_set rfds; - FD_ZERO(&rfds); - FD_SET(fifoDescriptor, &rfds); - int retval = select(fifoDescriptor + 1, &rfds, NULL, NULL, &tv); - - // Check if error - if (retval < 0) { - char tmp[64]; - error("ERROR: select(): %s", strerror_r(errno, tmp, sizeof(tmp))); - } + // See if there is data in the fifo + int retval = selectSingleDesc(fifoDescriptor, 0, false); // There is no data in the fifo, more can be written - else if (!retval) { + if (!retval) { #ifdef DEBUG_PRINTF printf("id = %d, pid %d would now write %d data to buffer\n", id, pid, buffer1_len); for (unsigned int i = 0; i < buffer1_len; ++i)