new function: set_stream_fds()
authorAndre <maan@p133.(none)>
Sat, 8 Apr 2006 16:53:46 +0000 (18:53 +0200)
committerAndre <maan@p133.(none)>
Sat, 8 Apr 2006 16:53:46 +0000 (18:53 +0200)
There are many places like the following:

FD_SET(fd, fdset);
max = MAX(max, fd);

set_stream_fds() simply combines these two commands.

audiod.c
fd.c
fd.h

index 9708f4fa363f4ffa6d3540fc7cb27c79917f76ca..72da56d14583444f55e2d55175e4249cccc623a1 100644 (file)
--- a/audiod.c
+++ b/audiod.c
@@ -961,9 +961,9 @@ static void close_decoder_if_idle(int slot_num)
        clear_slot(slot_num);
 }
 
-static int set_stream_fds(fd_set *wfds)
+static void set_stream_fds(fd_set *wfds, int *max_fileno)
 {
-       int i, max_fileno = -1;
+       int i;
 
        check_timeouts();
        FOR_EACH_SLOT(i) {
@@ -985,12 +985,9 @@ static int set_stream_fds(fd_set *wfds)
                        continue;
                if (!get_loaded_bytes(i))
                        continue;
-               FD_SET(s->write_fd, wfds);
+               para_fd_set(s->write_fd, wfds, max_fileno);
                s->wcheck = 1;
-               max_fileno = MAX(s->write_fd, max_fileno);
        }
-//     PARA_INFO_LOG("return %d\n", max_fileno);
-       return max_fileno;
 }
 
 static int write_audio_data(int slot_num)
@@ -1521,9 +1518,10 @@ static int open_stat_pipe(void)
        return ret;
 }
 
-static int audiod_pre_select(fd_set *rfds, fd_set *wfds, struct timeval *tv)
+static void audiod_pre_select(fd_set *rfds, fd_set *wfds, struct timeval *tv,
+               int *max_fileno)
 {
-       int i, ret, max = -1;
+       int i, ret;
 
        FOR_EACH_SLOT(i) {
                struct slot_info *s = &slot[i];
@@ -1534,10 +1532,8 @@ static int audiod_pre_select(fd_set *rfds, fd_set *wfds, struct timeval *tv)
                a = &afi[s->format];
                ret = a->receiver->pre_select(rn, rfds, wfds, tv);
 //             PARA_NOTICE_LOG("%s preselect: %d\n", a->receiver->name, ret);
-               max = MAX(max, ret);
+               *max_fileno = MAX(*max_fileno, ret);
        }
-       return max;
-
 }
 static void audiod_post_select(int select_ret, fd_set *rfds, fd_set *wfds)
 {
@@ -1577,15 +1573,14 @@ repeat:
        /* always check signal pipe and the local socket */
        FD_SET(signal_pipe, &rfds);
        max_fileno = signal_pipe;
-       FD_SET(audiod_socket, &rfds);
-       max_fileno = MAX(max_fileno, audiod_socket);
+       para_fd_set(audiod_socket, &rfds, &max_fileno);
 
        if (audiod_status != AUDIOD_ON)
                kill_all_decoders();
        else if (playing)
                start_current_receiver();
 
-       max_fileno = MAX(max_fileno, set_stream_fds(&wfds));
+       set_stream_fds(&wfds, &max_fileno);
        /* status pipe */
        if (stat_pipe >= 0 && audiod_status == AUDIOD_OFF)
                close_stat_pipe();
@@ -1594,16 +1589,12 @@ repeat:
                sbo = 0;
                status_buf[0] = '\0';
        }
-       if (stat_pipe >= 0 && audiod_status != AUDIOD_OFF) {
-               FD_SET(stat_pipe, &rfds);
-               max_fileno = MAX(max_fileno, stat_pipe);
-       }
+       if (stat_pipe >= 0 && audiod_status != AUDIOD_OFF)
+               para_fd_set(stat_pipe, &rfds, &max_fileno);
        /* local socket */
        tv.tv_sec = 0;
        tv.tv_usec = 200 * 1000;
-       ret = audiod_pre_select(&rfds, &wfds, &tv);
-       max_fileno = MAX(max_fileno, ret);
-
+       audiod_pre_select(&rfds, &wfds, &tv, &max_fileno);
        ret = para_select(max_fileno + 1, &rfds, &wfds, &tv);
        if (ret < 0)
                goto repeat;
diff --git a/fd.c b/fd.c
index f5d326c044d4e9b08aec613b25845a9e074aa3ea..4928850e38d7d01688702d8acb8e898da53e284d 100644 (file)
--- a/fd.c
+++ b/fd.c
@@ -84,3 +84,18 @@ int mark_fd_nonblock(int fd)
        return 1;
 }
 
+/**
+ * set a file descriptor in a fd_set
+ *
+ * \param fd the file descriptor to be set
+ * \param fds the file descriptor set
+ * \param max_fileno highest-numbered file descriptor
+ *
+ * This wrapper for FD_SET() passes its first two arguments to \p FD_SET. Upon
+ * return, \a max_fileno contains the maximum of the old_value and \a fd.
+*/
+void para_fd_set(int fd, fd_set *fds, int *max_fileno)
+{
+       FD_SET(fd, fds);
+       *max_fileno = MAX(*max_fileno, fd);
+}
diff --git a/fd.h b/fd.h
index 42df3a3ec1d39e42d8d2b1f0b522d96d24068035..66d3799fae2235ac1bfc41709aeb66a45fba8f96 100644 (file)
--- a/fd.h
+++ b/fd.h
@@ -22,3 +22,4 @@ int file_exists(const char *);
 int para_select(int n, fd_set *readfds, fd_set *writefds,
                struct timeval *timeout);
 int mark_fd_nonblock(int fd);
+void para_fd_set(int fd, fd_set *fds, int *max_fileno);