vss: Check the afs socket either for reading or for writing.
[paraslash.git] / vss.c
diff --git a/vss.c b/vss.c
index a754267..3c59bf4 100644 (file)
--- a/vss.c
+++ b/vss.c
@@ -262,6 +262,19 @@ enum afs_socket_status {
 
 static enum afs_socket_status afsss;
 
+static int need_to_request_new_audio_file(void)
+{
+       if (map) /* have audio file */
+               return 0;
+       if (!vss_playing()) /* don't need one */
+               return 0;
+       if (mmd->new_vss_status_flags & VSS_NOMORE)
+               return 0;
+       if (afsss == AFS_SOCKET_AFD_PENDING) /* already requested one */
+               return 0;
+       return 1;
+}
+
 /**
  * Compute the timeout for para_server's main select-loop.
  *
@@ -288,7 +301,6 @@ struct timeval *vss_preselect(fd_set *rfds, fd_set *wfds, int *max_fileno)
        int i;
        struct timeval *tv;
 
-       para_fd_set(afs_socket, rfds, max_fileno);
        if (!map)
                for (i = 0; senders[i].name; i++)
                        senders[i].shutdown_clients();
@@ -317,17 +329,13 @@ struct timeval *vss_preselect(fd_set *rfds, fd_set *wfds, int *max_fileno)
                mmd->current_chunk = mmd->repos_request;
        }
        tv = vss_compute_timeout();
-       if (tv)
-               goto out;
-       if (!map && vss_playing() &&
-                       !(mmd->new_vss_status_flags & VSS_NOMORE)) {
-               if (afsss != AFS_SOCKET_AFD_PENDING) {
-                       PARA_DEBUG_LOG("ready and playing, but no audio file\n");
-                       para_fd_set(afs_socket, wfds, max_fileno);
-                       afsss = AFS_SOCKET_CHECK_FOR_WRITE;
-               }
-       }
 out:
+       if (need_to_request_new_audio_file()) {
+               PARA_DEBUG_LOG("ready and playing, but no audio file\n");
+               para_fd_set(afs_socket, wfds, max_fileno);
+               afsss = AFS_SOCKET_CHECK_FOR_WRITE;
+       } else
+               para_fd_set(afs_socket, rfds, max_fileno);
        return tv;
 }
 
@@ -421,14 +429,18 @@ void vss_post_select(fd_set *rfds, fd_set *wfds)
 {
        int ret;
 
-       if (FD_ISSET(afs_socket, rfds))
-               recv_afs_result();
-       if (afsss != AFS_SOCKET_CHECK_FOR_WRITE || !FD_ISSET(afs_socket, wfds))
+       if (afsss != AFS_SOCKET_CHECK_FOR_WRITE) {
+               if (FD_ISSET(afs_socket, rfds))
+                       recv_afs_result();
+               return;
+       }
+       if (!FD_ISSET(afs_socket, wfds))
                return;
        PARA_NOTICE_LOG("requesting new fd from afs\n");
        ret = send_buffer(afs_socket, "new");
        afsss = AFS_SOCKET_AFD_PENDING;
 }
+
 /**
  * Get the data of the given chunk.
  *