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