1
0
mirror of https://gitlab.xiph.org/xiph/icecast-server.git synced 2024-12-04 14:46:30 -05:00

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
This commit is contained in:
Michael Smith 2002-07-24 14:52:28 +00:00
parent b45b6853da
commit 59e523a023
4 changed files with 46 additions and 30 deletions

View File

@ -109,18 +109,10 @@ static unsigned long _next_connection_id(void)
static connection_t *_accept_connection(void)
{
int sock;
fd_set rfds;
connection_t *con;
struct timeval tv;
char *ip;
FD_ZERO(&rfds);
FD_SET(global.serversock, &rfds);
tv.tv_sec = 0;
tv.tv_usec = 30000;
if (select(global.serversock + 1, &rfds, NULL, NULL, &tv) <= 0) {
if (util_timed_wait_for_fd(global.serversock, 30) <= 0) {
return NULL;
}

View File

@ -27,6 +27,7 @@
#include "log.h"
#include "logging.h"
#include "config.h"
#include "util.h"
#include "source.h"
@ -117,9 +118,6 @@ void *source_main(void *arg)
refbuf_t *refbuf, *abuf;
int data_done;
fd_set rfds;
struct timeval tv;
int listeners = 0;
timeout = config_get_config()->source_timeout;
@ -147,7 +145,7 @@ void *source_main(void *arg)
stats_event(source->mount, "description", s);
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) {
WARN0("Bad data from source");
break;
@ -155,20 +153,16 @@ void *source_main(void *arg)
while (refbuf == NULL) {
bytes = 0;
while (bytes <= 0) {
FD_ZERO(&rfds);
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 */
ret = util_timed_wait_for_fd(source->con->sock, timeout*1000);
if (ret <= 0) { /* timeout expired */
bytes = 0;
break;
}
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;
ret = source->format->get_buffer(source->format, buffer, bytes, &refbuf);

View File

@ -4,6 +4,9 @@
#include <sys/time.h>
#include <sys/socket.h>
#include <unistd.h>
#ifdef HAVE_POLL
#include <sys/poll.h>
#endif
#else
#include <winsock2.h>
#include <windows.h>
@ -14,13 +17,44 @@
#include "config.h"
#include "util.h"
/* 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;
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)
{
fd_set rfds;
int read_bytes, ret;
unsigned long pos;
char c;
struct timeval tv;
ice_config_t *config;
config = config_get_config();
@ -32,13 +66,8 @@ int util_read_header(int sock, char *buff, unsigned long len)
while ((read_bytes == 1) && (pos < (len - 1))) {
read_bytes = 0;
FD_ZERO(&rfds);
FD_SET(sock, &rfds);
if (util_timed_wait_for_fd(sock, config->header_timeout*1000) > 0) {
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 (c != '\r') buff[pos++] = c;
if ((pos > 1) && (buff[pos - 1] == '\n' && buff[pos - 2] == '\n')) {

View File

@ -1,6 +1,7 @@
#ifndef __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);
#endif /* __UTIL_H__ */