YAZ  5.23.1
zoom-socket.c
Go to the documentation of this file.
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12 
13 #include <assert.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <yaz/zoom.h>
17 
18 #include <yaz/log.h>
19 #include <yaz/xmalloc.h>
20 
21 #if HAVE_SYS_TYPES_H
22 #include <sys/types.h>
23 #endif
24 #if HAVE_SYS_TIME_H
25 #include <sys/time.h>
26 #endif
27 
28 #include <yaz/poll.h>
29 
30 ZOOM_API(int)
32 {
33  struct yaz_poll_fd *yp = (struct yaz_poll_fd *) xmalloc(sizeof(*yp) * no);
34  int i, r;
35  int nfds = 0;
36  int timeout = 30;
37 
38  for (i = 0; i < no; i++)
39  {
40  ZOOM_connection c = cs[i];
41  int fd, mask;
42 
43  if (!c)
44  continue;
46  mask = ZOOM_connection_get_mask(c);
47  timeout = ZOOM_connection_get_timeout(c);
48 
49  if (fd == -1)
50  continue;
51  if (mask)
52  {
54 
55  if (mask & ZOOM_SELECT_READ)
56  yaz_poll_add(input_mask, yaz_poll_read);
57  if (mask & ZOOM_SELECT_WRITE)
58  yaz_poll_add(input_mask, yaz_poll_write);
59  if (mask & ZOOM_SELECT_EXCEPT)
60  yaz_poll_add(input_mask, yaz_poll_except);
61  yp[nfds].fd = fd;
62  yp[nfds].input_mask = input_mask;
63  yp[nfds].client_data = c;
64  nfds++;
65  }
66  }
67  if (nfds == 0)
68  {
69  xfree(yp);
70  return 0;
71  }
72  r = yaz_poll(yp, nfds, timeout, 0);
73  if (r >= 0)
74  {
75  for (i = 0; i < nfds; i++)
76  {
79  if (output_mask & yaz_poll_timeout)
81  else
82  {
83  int mask = 0;
84  if (output_mask & yaz_poll_read)
85  mask += ZOOM_SELECT_READ;
86  if (output_mask & yaz_poll_write)
87  mask += ZOOM_SELECT_WRITE;
88  if (output_mask & yaz_poll_except)
89  mask += ZOOM_SELECT_EXCEPT;
91  }
92  }
93  }
94  xfree(yp);
95  return r;
96 }
97 
98 ZOOM_API(int)
100 {
101  int r;
102 
103  r = ZOOM_event_nonblock(no, cs);
104  if (r)
105  return r;
106  while (ZOOM_event_sys_yaz_poll(no, cs) < 0 && errno == EINTR)
107  ;
108  return ZOOM_event_nonblock(no, cs);
109 }
110 
111 /*
112  * Local variables:
113  * c-basic-offset: 4
114  * c-file-style: "Stroustrup"
115  * indent-tabs-mode: nil
116  * End:
117  * vim: shiftwidth=4 tabstop=8 expandtab
118  */
119 
enum yaz_poll_mask output_mask
Definition: poll.h:54
struct ZOOM_connection_p * ZOOM_connection
Definition: zoom.h:52
Header for ZOOM.
Header for errno utilities.
select/poll fd info
Definition: poll.h:50
ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask)
fire socket event activity (read,write,except)
#define yaz_poll_add(var, value)
Definition: poll.h:76
Poll, select wrappers.
#define xfree(x)
utility macro which calls xfree_f
Definition: xmalloc.h:53
#define ZOOM_SELECT_READ
select/poll socket mask: read
Definition: zoom.h:415
ZOOM_connection_fire_event_timeout(ZOOM_connection c)
fire socket event timeout
#define ZOOM_API(x)
Definition: zoom.h:42
#define ZOOM_SELECT_WRITE
select/poll socket mask: write
Definition: zoom.h:417
ZOOM_connection_get_mask(ZOOM_connection c)
get socket mask for connection
ZOOM_event_sys_yaz_poll(int no, ZOOM_connection *cs)
Definition: zoom-socket.c:31
int fd
Definition: poll.h:56
#define xmalloc(x)
utility macro which calls malloc_f
Definition: xmalloc.h:49
ZOOM_connection_get_socket(ZOOM_connection c)
get socket fd for ZOOM connection
Header for memory handling functions.
enum yaz_poll_mask input_mask
Definition: poll.h:52
void * client_data
Definition: poll.h:58
ZOOM_connection_get_timeout(ZOOM_connection c)
get timeout in seconds for ZOOM connection
ZOOM_event_nonblock(int no, ZOOM_connection *cs)
process one event for one of connections given
Definition: zoom-c.c:2080
ZOOM_event(int no, ZOOM_connection *cs)
wait for events on connection(s) (BLOCKING)
Definition: zoom-socket.c:99
Logging utility.
#define ZOOM_SELECT_EXCEPT
select/poll socket mask: except
Definition: zoom.h:419
int yaz_poll(struct yaz_poll_fd *fds, int num_fds, int sec, int nsec)
poll wrapper for poll or select
Definition: poll.c:161
int mask
Definition: log.c:81
yaz_poll_mask
select/poll masks .. timeout is "output" only
Definition: poll.h:41