2 * Copyright (c) 1995-2001, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.30 2001-10-05 13:55:17 adam
8 * Added defines YAZ_GNU_THREADS, YAZ_POSIX_THREADS in code and yaz-config
10 * Revision 1.29 1999/11/30 13:47:12 adam
11 * Improved installation. Moved header files to include/yaz.
13 * Revision 1.28 1999/08/27 09:40:32 adam
14 * Renamed logf function to yaz_log. Removed VC++ project files.
16 * Revision 1.27 1999/02/02 13:57:34 adam
17 * Uses preprocessor define WIN32 instead of WINDOWS to build code
18 * for Microsoft WIN32.
20 * Revision 1.26 1998/02/11 11:53:35 adam
21 * Changed code so that it compiles as C++.
23 * Revision 1.25 1998/01/29 13:30:23 adam
24 * Better event handle system for NT/Unix.
26 * Revision 1.24 1997/09/04 14:19:13 adam
29 * Revision 1.23 1997/09/01 08:52:59 adam
30 * New windows NT/95 port using MSV5.0. The test server 'ztest' was
31 * moved a separate directory. MSV5.0 project server.dsp created.
32 * As an option, the server can now operate as an NT service.
34 * Revision 1.22 1996/07/06 19:58:35 quinn
35 * System headerfiles gathered in yconfig
37 * Revision 1.21 1996/02/21 12:55:51 quinn
40 * Revision 1.20 1996/02/21 12:52:55 quinn
43 * Revision 1.19 1995/12/05 11:17:30 quinn
44 * Moved some paranthesises around. Sigh.
46 * Revision 1.18 1995/11/13 09:27:41 quinn
47 * Fiddling with the variant stuff.
49 * Revision 1.17 1995/11/07 12:37:44 quinn
50 * Added support for forcing TIMEOUT event.
52 * Revision 1.16 1995/11/01 13:54:56 quinn
55 * Revision 1.15 1995/09/15 14:44:15 quinn
56 * *** empty log message ***
58 * Revision 1.14 1995/08/29 14:44:50 quinn
61 * Revision 1.13 1995/08/29 11:17:56 quinn
62 * Added code to receive close
64 * Revision 1.12 1995/08/29 10:41:18 quinn
67 * Revision 1.11 1995/06/19 12:39:09 quinn
68 * Fixed bug in timeout code. Added BER dumper.
70 * Revision 1.10 1995/06/16 10:31:33 quinn
71 * Added session timeout.
73 * Revision 1.9 1995/06/05 10:53:31 quinn
74 * Added a better SCAN.
76 * Revision 1.8 1995/05/16 08:51:01 quinn
77 * License, documentation, and memory fixes
79 * Revision 1.7 1995/03/27 15:02:01 quinn
80 * Added some includes for better portability
82 * Revision 1.6 1995/03/27 08:34:21 quinn
83 * Added dynamic server functionality.
84 * Released bindings to session.c (is now redundant)
86 * Revision 1.5 1995/03/15 08:37:41 quinn
87 * Now we're pretty much set for nonblocking I/O.
89 * Revision 1.4 1995/03/14 16:59:48 quinn
92 * Revision 1.3 1995/03/14 11:30:14 quinn
95 * Revision 1.2 1995/03/14 10:27:59 quinn
96 * More work on demo server.
98 * Revision 1.1 1995/03/10 18:22:44 quinn
99 * The rudiments of an asynchronous server.
114 #include <yaz/yconfig.h>
116 #include <yaz/comstack.h>
117 #include <yaz/xmalloc.h>
120 #include <yaz/statserv.h>
124 #define YAZ_EV_SELECT pth_select
127 #ifndef YAZ_EV_SELECT
128 #define YAZ_EV_SELECT select
131 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags)
135 if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
137 new_iochan->destroyed = 0;
139 new_iochan->flags = flags;
140 new_iochan->fun = cb;
141 new_iochan->force_event = 0;
142 new_iochan->last_event = new_iochan->max_idle = 0;
143 new_iochan->next = NULL;
147 int event_loop(IOCHAN *iochans)
149 do /* loop as long as there are active associations to process */
152 fd_set in, out, except;
154 static struct timeval nullto = {0, 0}, to;
155 struct timeval *timeout;
160 timeout = &to; /* hang on select */
164 for (p = *iochans; p; p = p->next)
167 timeout = &nullto; /* polling select */
168 if (p->flags & EVENT_INPUT)
170 if (p->flags & EVENT_OUTPUT)
172 if (p->flags & EVENT_EXCEPT)
173 FD_SET(p->fd, &except);
177 res = YAZ_EV_SELECT(max + 1, &in, &out, &except, timeout);
184 /* Destroy the first member in the chain, and try again */
185 association *assoc = (association *)iochan_getdata(*iochans);
186 COMSTACK conn = assoc->client_link;
189 destroy_association(assoc);
190 iochan_destroy(*iochans);
191 yaz_log(LOG_DEBUG, "error select, destroying iochan %p",
195 for (p = *iochans; p; p = p->next)
197 int force_event = p->force_event;
198 time_t now = time(0);
201 if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
202 force_event == EVENT_INPUT))
205 (*p->fun)(p, EVENT_INPUT);
207 if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
208 force_event == EVENT_OUTPUT))
211 (*p->fun)(p, EVENT_OUTPUT);
213 if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
214 force_event == EVENT_EXCEPT))
217 (*p->fun)(p, EVENT_EXCEPT);
219 if (!p->destroyed && ((p->max_idle && now - p->last_event >
220 p->max_idle) || force_event == EVENT_TIMEOUT))
223 (*p->fun)(p, EVENT_TIMEOUT);
226 for (p = *iochans; p; p = nextp)
234 /* We need to inform the threadlist that this channel has been destroyed */
237 /* Now reset the pointers */
242 for (pr = *iochans; pr; pr = pr->next)
245 assert(pr); /* grave error if it weren't there */