CCL: fix use of "term" field in sub queries
[yaz-moved-to-github.git] / src / zoom-socket.c
index a3adebf..cb5579a 100644 (file)
@@ -1,13 +1,14 @@
-/*
- * Copyright (C) 1995-2007, Index Data ApS
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2012 Index Data
  * See the file LICENSE for details.
- *
- * $Id: zoom-socket.c,v 1.1 2007-01-09 13:56:48 adam Exp $
  */
 /**
  * \file zoom-socket.c
  * \brief Implements ZOOM C socket interface.
  */
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
 
 #include <assert.h>
 #include <string.h>
@@ -15,6 +16,7 @@
 #include <yaz/zoom.h>
 
 #include <yaz/log.h>
+#include <yaz/xmalloc.h>
 
 #if HAVE_SYS_TYPES_H
 #include <sys/types.h>
 #if HAVE_SYS_TIME_H
 #include <sys/time.h>
 #endif
-#if HAVE_SYS_POLL_H
-#include <sys/poll.h>
-#endif
-#if HAVE_SYS_SELECT_H
-#include <sys/select.h>
-#endif
-#ifdef WIN32
-#if FD_SETSIZE < 512
-#define FD_SETSIZE 512
-#endif
-#include <winsock.h>
-#endif
-
-
-ZOOM_API(int)
-    ZOOM_event_sys_select(int no, ZOOM_connection *cs)
-{
-    struct timeval tv;
-    fd_set input, output, except;
-    int i, r;
-    int max_fd = 0;
-    int timeout = 30;
-    int nfds = 0;
-
-    FD_ZERO(&input);
-    FD_ZERO(&output);
-    FD_ZERO(&except);
-
-    for (i = 0; i<no; i++)
-    {
-        ZOOM_connection c = cs[i];
-        int fd, mask;
-        
-        if (!c)
-            continue;
-        fd = ZOOM_connection_get_socket(c);
-        mask = ZOOM_connection_get_mask(c);
-        timeout = ZOOM_connection_get_timeout(c);
-
-        if (fd == -1)
-            continue;
-        if (max_fd < fd)
-            max_fd = fd;
-        
-        if (mask & ZOOM_SELECT_READ)
-            FD_SET(fd, &input);
-        if (mask & ZOOM_SELECT_WRITE)
-            FD_SET(fd, &output);
-        if (mask & ZOOM_SELECT_EXCEPT)
-            FD_SET(fd, &except);
-        if (mask)
-            nfds++;
-    }
-    if (nfds == 0)
-        return 0;
-
-    tv.tv_sec = timeout;
-    tv.tv_usec = 0;
-
-    while ((r = select(max_fd+1, &input, &output, &except,
-                       (timeout == -1 ? 0 : &tv))) < 0 && errno == EINTR)
-    {
-        ;
-    }
-    if (r < 0)
-    {
-        yaz_log(YLOG_WARN|YLOG_ERRNO, "ZOOM_event_sys_select");
-        return r;
-    }
-
-    for (i = 0; i<no; i++)
-    {
-        ZOOM_connection c = cs[i];
-        int fd, mask;
 
-        if (!c)
-            continue;
-        fd = ZOOM_connection_get_socket(c);
-        mask = 0;
-        if (r)
-        {
-            /* no timeout and real socket */
-            if (FD_ISSET(fd, &input))
-                mask += ZOOM_SELECT_READ;
-            if (FD_ISSET(fd, &output))
-                mask += ZOOM_SELECT_WRITE;
-            if (FD_ISSET(fd, &except))
-                mask += ZOOM_SELECT_EXCEPT;
-            if (mask)
-                ZOOM_connection_fire_event_socket(c, mask);
-        }
-        else
-            ZOOM_connection_fire_event_timeout(c);
-    }
-    return r;
-}
+#include <yaz/poll.h>
 
-#if HAVE_SYS_POLL_H
 ZOOM_API(int)
-    ZOOM_event_sys_poll(int no, ZOOM_connection *cs)
+    ZOOM_event_sys_yaz_poll(int no, ZOOM_connection *cs)
 {
-    struct pollfd pollfds[1024];
-    ZOOM_connection poll_cs[1024];
+    struct yaz_poll_fd *yp = (struct yaz_poll_fd *) xmalloc(sizeof(*yp) * no);
     int i, r;
     int nfds = 0;
     int timeout = 30;
 
-    for (i = 0; i<no; i++)
+    for (i = 0; i < no; i++)
     {
         ZOOM_connection c = cs[i];
         int fd, mask;
@@ -144,74 +50,68 @@ ZOOM_API(int)
             continue;
         if (mask)
         {
-            short poll_events = 0;
+            enum yaz_poll_mask input_mask = yaz_poll_none;
 
             if (mask & ZOOM_SELECT_READ)
-                poll_events += POLLIN;
+                yaz_poll_add(input_mask, yaz_poll_read);
             if (mask & ZOOM_SELECT_WRITE)
-                poll_events += POLLOUT;
+                yaz_poll_add(input_mask, yaz_poll_write);
             if (mask & ZOOM_SELECT_EXCEPT)
-                poll_events += POLLERR;
-            pollfds[nfds].fd = fd;
-            pollfds[nfds].events = poll_events;
-            pollfds[nfds].revents = 0;
-            poll_cs[nfds] = c;
+                yaz_poll_add(input_mask, yaz_poll_except);
+            yp[nfds].fd = fd;
+            yp[nfds].input_mask = input_mask;
+            yp[nfds].client_data = c;
             nfds++;
         }
     }
     if (nfds == 0)
-        return 0;
-    while ((r = poll(pollfds, nfds,
-         (timeout == -1 ? -1 : timeout * 1000))) < 0
-          && errno == EINTR)
-    {
-        ;
-    }
-    if (r < 0)
     {
-        yaz_log(YLOG_WARN|YLOG_ERRNO, "ZOOM_event_sys_poll");
-        return r;
+        xfree(yp);
+        return 0;
     }
-    for (i = 0; i<nfds; i++)
+    r = yaz_poll(yp, nfds, timeout, 0);
+    if (r >= 0)
     {
-        ZOOM_connection c = poll_cs[i];
-        if (r)
+        for (i = 0; i < nfds; i++)
         {
-            int mask = 0;
-            if (pollfds[i].revents & POLLIN)
-                mask += ZOOM_SELECT_READ;
-            if (pollfds[i].revents & POLLOUT)
-                mask += ZOOM_SELECT_WRITE;
-            if (pollfds[i].revents & POLLERR)
-                mask += ZOOM_SELECT_EXCEPT;
-            ZOOM_connection_fire_event_socket(c, mask);
+            ZOOM_connection c = (ZOOM_connection) yp[i].client_data;
+            enum yaz_poll_mask output_mask = yp[i].output_mask;
+            if (output_mask & yaz_poll_timeout)
+                ZOOM_connection_fire_event_timeout(c);
+            else
+            {
+                int mask = 0;
+                if (output_mask & yaz_poll_read)
+                    mask += ZOOM_SELECT_READ;
+                if (output_mask & yaz_poll_write)
+                    mask += ZOOM_SELECT_WRITE;
+                if (output_mask & yaz_poll_except)
+                    mask += ZOOM_SELECT_EXCEPT;
+                ZOOM_connection_fire_event_socket(c, mask);
+            }
         }
-        else
-            ZOOM_connection_fire_event_timeout(c);
     }
+    xfree(yp);
     return r;
 }
-#endif
 
 ZOOM_API(int)
     ZOOM_event(int no, ZOOM_connection *cs)
 {
     int r;
 
-    r = ZOOM_process_event(no, cs);
+    r = ZOOM_event_nonblock(no, cs);
     if (r)
         return r;
-#if HAVE_SYS_POLL_H
-    ZOOM_event_sys_poll(no, cs);
-#else
-    ZOOM_event_sys_select(no, cs);
-#endif
-    return ZOOM_process_event(no, cs);
+    while (ZOOM_event_sys_yaz_poll(no, cs) < 0 && errno == EINTR)
+        ;
+    return ZOOM_event_nonblock(no, cs);
 }
 
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab