Change timeout parameter for yaz_poll.
[yaz-moved-to-github.git] / src / zoom-socket.c
1 /*
2  * Copyright (C) 1995-2007, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: zoom-socket.c,v 1.6 2007-11-09 22:08:14 adam Exp $
6  */
7 /**
8  * \file zoom-socket.c
9  * \brief Implements ZOOM C socket interface.
10  */
11
12 #include <assert.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <yaz/zoom.h>
16
17 #include <yaz/log.h>
18 #include <yaz/xmalloc.h>
19
20 #if HAVE_SYS_TYPES_H
21 #include <sys/types.h>
22 #endif
23 #if HAVE_SYS_TIME_H
24 #include <sys/time.h>
25 #endif
26
27 #include <yaz/poll.h>
28
29 ZOOM_API(int)
30     ZOOM_event_sys_yaz_poll(int no, ZOOM_connection *cs)
31 {
32     struct yaz_poll_fd *yp = xmalloc(sizeof(*yp) * no);
33     int i, r;
34     int nfds = 0;
35     int timeout = 30;
36
37     for (i = 0; i < no; i++)
38     {
39         ZOOM_connection c = cs[i];
40         int fd, mask;
41         
42         if (!c)
43             continue;
44         fd = ZOOM_connection_get_socket(c);
45         mask = ZOOM_connection_get_mask(c);
46         timeout = ZOOM_connection_get_timeout(c);
47
48         if (fd == -1)
49             continue;
50         if (mask)
51         {
52             enum yaz_poll_mask input_mask = 0;
53
54             if (mask & ZOOM_SELECT_READ)
55                 input_mask += yaz_poll_read;
56             if (mask & ZOOM_SELECT_WRITE)
57                 input_mask += yaz_poll_write;
58             if (mask & ZOOM_SELECT_EXCEPT)
59                 input_mask += yaz_poll_except;
60             yp[nfds].fd = fd;
61             yp[nfds].input_mask = input_mask;
62             yp[nfds].client_data = c;
63             nfds++;
64         }
65     }
66     if (nfds == 0)
67     {
68         xfree(yp);
69         return 0;
70     }
71     r = yaz_poll(yp, nfds, timeout, 0);
72     if (r >= 0)
73     {
74         for (i = 0; i < nfds; i++)
75         {
76             ZOOM_connection c = yp[i].client_data;
77             enum yaz_poll_mask output_mask = yp[i].output_mask;
78             if (output_mask & yaz_poll_timeout)
79                 ZOOM_connection_fire_event_timeout(c);
80             else
81             {
82                 int mask = 0;
83                 if (output_mask & yaz_poll_read)
84                     mask += ZOOM_SELECT_READ;
85                 if (output_mask & yaz_poll_write)
86                     mask += ZOOM_SELECT_WRITE;
87                 if (output_mask & yaz_poll_except)
88                     mask += ZOOM_SELECT_EXCEPT;
89                 ZOOM_connection_fire_event_socket(c, mask);
90             }
91         }
92     }
93     xfree(yp);
94     return r;
95 }
96
97 ZOOM_API(int)
98     ZOOM_event(int no, ZOOM_connection *cs)
99 {
100     int r;
101
102     r = ZOOM_event_nonblock(no, cs);
103     if (r)
104         return r;
105     ZOOM_event_sys_yaz_poll(no, cs);
106     return ZOOM_event_nonblock(no, cs);
107 }
108
109 /*
110  * Local variables:
111  * c-basic-offset: 4
112  * indent-tabs-mode: nil
113  * End:
114  * vim: shiftwidth=4 tabstop=8 expandtab
115  */
116