Commit 59e523a0 authored by Michael Smith's avatar Michael Smith

Allow either poll or select to be used (based on whether HAVE_POLL is defined.)

Still need to make autoconf define HAVE_POLL where relevent.

svn path=/trunk/icecast/; revision=3717
parent b45b6853
...@@ -109,18 +109,10 @@ static unsigned long _next_connection_id(void) ...@@ -109,18 +109,10 @@ static unsigned long _next_connection_id(void)
static connection_t *_accept_connection(void) static connection_t *_accept_connection(void)
{ {
int sock; int sock;
fd_set rfds;
connection_t *con; connection_t *con;
struct timeval tv;
char *ip; char *ip;
FD_ZERO(&rfds); if (util_timed_wait_for_fd(global.serversock, 30) <= 0) {
FD_SET(global.serversock, &rfds);
tv.tv_sec = 0;
tv.tv_usec = 30000;
if (select(global.serversock + 1, &rfds, NULL, NULL, &tv) <= 0) {
return NULL; return NULL;
} }
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include "log.h" #include "log.h"
#include "logging.h" #include "logging.h"
#include "config.h" #include "config.h"
#include "util.h"
#include "source.h" #include "source.h"
...@@ -117,9 +118,6 @@ void *source_main(void *arg) ...@@ -117,9 +118,6 @@ void *source_main(void *arg)
refbuf_t *refbuf, *abuf; refbuf_t *refbuf, *abuf;
int data_done; int data_done;
fd_set rfds;
struct timeval tv;
int listeners = 0; int listeners = 0;
timeout = config_get_config()->source_timeout; timeout = config_get_config()->source_timeout;
...@@ -147,7 +145,7 @@ void *source_main(void *arg) ...@@ -147,7 +145,7 @@ void *source_main(void *arg)
stats_event(source->mount, "description", s); stats_event(source->mount, "description", s);
while (global.running == ICE_RUNNING) { while (global.running == ICE_RUNNING) {
int ret = source->format->get_buffer(source->format, NULL, 0, &refbuf); ret = source->format->get_buffer(source->format, NULL, 0, &refbuf);
if(ret < 0) { if(ret < 0) {
WARN0("Bad data from source"); WARN0("Bad data from source");
break; break;
...@@ -155,20 +153,16 @@ void *source_main(void *arg) ...@@ -155,20 +153,16 @@ void *source_main(void *arg)
while (refbuf == NULL) { while (refbuf == NULL) {
bytes = 0; bytes = 0;
while (bytes <= 0) { while (bytes <= 0) {
FD_ZERO(&rfds); ret = util_timed_wait_for_fd(source->con->sock, timeout*1000);
FD_SET(source->con->sock, &rfds);
tv.tv_sec = timeout;
tv.tv_usec = 0;
ret = select(source->con->sock + 1, &rfds, NULL, NULL, &tv); if (ret <= 0) { /* timeout expired */
if (ret == 0) { /* timeout expired */
bytes = 0; bytes = 0;
break; break;
} }
bytes = sock_read_bytes(source->con->sock, buffer, 4096); bytes = sock_read_bytes(source->con->sock, buffer, 4096);
if (bytes == 0 || (bytes < 0 && !sock_recoverable(sock_error()))) break; if (bytes == 0 || (bytes < 0 && !sock_recoverable(sock_error())))
break;
} }
if (bytes <= 0) break; if (bytes <= 0) break;
ret = source->format->get_buffer(source->format, buffer, bytes, &refbuf); ret = source->format->get_buffer(source->format, buffer, bytes, &refbuf);
......
...@@ -4,6 +4,9 @@ ...@@ -4,6 +4,9 @@
#include <sys/time.h> #include <sys/time.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <unistd.h> #include <unistd.h>
#ifdef HAVE_POLL
#include <sys/poll.h>
#endif
#else #else
#include <winsock2.h> #include <winsock2.h>
#include <windows.h> #include <windows.h>
...@@ -14,13 +17,44 @@ ...@@ -14,13 +17,44 @@
#include "config.h" #include "config.h"
#include "util.h" #include "util.h"
int util_read_header(int sock, char *buff, unsigned long len) /* Abstract out an interface to use either poll or select depending on which
* is available (poll is preferred) to watch a single fd.
*
* timeout is in milliseconds.
*
* returns > 0 if activity on the fd occurs before the timeout.
* 0 if no activity occurs
* < 0 for error.
*/
int util_timed_wait_for_fd(int fd, int timeout)
{ {
#ifdef HAVE_POLL
struct pollfd ufds;
ufds.fd = fd;
ufds.events = POLLIN;
ufds.revents = 0;
return poll(&ufds, 1, timeout);
#else
fd_set rfds; fd_set rfds;
struct timeval tv;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
tv.tv_sec = timeout/1000;
tv.tv_usec = (timeout - tv.tv_sec)*1000;
return select(fd+1, &rfds, NULL, NULL, &tv);
#endif
}
int util_read_header(int sock, char *buff, unsigned long len)
{
int read_bytes, ret; int read_bytes, ret;
unsigned long pos; unsigned long pos;
char c; char c;
struct timeval tv;
ice_config_t *config; ice_config_t *config;
config = config_get_config(); config = config_get_config();
...@@ -32,13 +66,8 @@ int util_read_header(int sock, char *buff, unsigned long len) ...@@ -32,13 +66,8 @@ int util_read_header(int sock, char *buff, unsigned long len)
while ((read_bytes == 1) && (pos < (len - 1))) { while ((read_bytes == 1) && (pos < (len - 1))) {
read_bytes = 0; read_bytes = 0;
FD_ZERO(&rfds); if (util_timed_wait_for_fd(sock, config->header_timeout*1000) > 0) {
FD_SET(sock, &rfds);
tv.tv_sec = config->header_timeout;
tv.tv_usec = 0;
if (select(sock + 1, &rfds, NULL, NULL, &tv) > 0) {
if ((read_bytes = recv(sock, &c, 1, 0))) { if ((read_bytes = recv(sock, &c, 1, 0))) {
if (c != '\r') buff[pos++] = c; if (c != '\r') buff[pos++] = c;
if ((pos > 1) && (buff[pos - 1] == '\n' && buff[pos - 2] == '\n')) { if ((pos > 1) && (buff[pos - 1] == '\n' && buff[pos - 2] == '\n')) {
......
#ifndef __UTIL_H__ #ifndef __UTIL_H__
#define __UTIL_H__ #define __UTIL_H__
int util_timed_wait_for_fd(int fd, int timeout);
int util_read_header(int sock, char *buff, unsigned long len); int util_read_header(int sock, char *buff, unsigned long len);
#endif /* __UTIL_H__ */ #endif /* __UTIL_H__ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment