Added ifdef USE_XTIMOSI; so that 'make depend' works.
[yaz-moved-to-github.git] / server / eventl.c
1 /*
2  * Copyright (c) 1995, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: eventl.c,v $
7  * Revision 1.21  1996-02-21 12:55:51  quinn
8  * small
9  *
10  * Revision 1.20  1996/02/21  12:52:55  quinn
11  * Test
12  *
13  * Revision 1.19  1995/12/05  11:17:30  quinn
14  * Moved some paranthesises around. Sigh.
15  *
16  * Revision 1.18  1995/11/13  09:27:41  quinn
17  * Fiddling with the variant stuff.
18  *
19  * Revision 1.17  1995/11/07  12:37:44  quinn
20  * Added support for forcing TIMEOUT event.
21  *
22  * Revision 1.16  1995/11/01  13:54:56  quinn
23  * Minor adjustments
24  *
25  * Revision 1.15  1995/09/15  14:44:15  quinn
26  * *** empty log message ***
27  *
28  * Revision 1.14  1995/08/29  14:44:50  quinn
29  * Reset timeouts.
30  *
31  * Revision 1.13  1995/08/29  11:17:56  quinn
32  * Added code to receive close
33  *
34  * Revision 1.12  1995/08/29  10:41:18  quinn
35  * Small.
36  *
37  * Revision 1.11  1995/06/19  12:39:09  quinn
38  * Fixed bug in timeout code. Added BER dumper.
39  *
40  * Revision 1.10  1995/06/16  10:31:33  quinn
41  * Added session timeout.
42  *
43  * Revision 1.9  1995/06/05  10:53:31  quinn
44  * Added a better SCAN.
45  *
46  * Revision 1.8  1995/05/16  08:51:01  quinn
47  * License, documentation, and memory fixes
48  *
49  * Revision 1.7  1995/03/27  15:02:01  quinn
50  * Added some includes for better portability
51  *
52  * Revision 1.6  1995/03/27  08:34:21  quinn
53  * Added dynamic server functionality.
54  * Released bindings to session.c (is now redundant)
55  *
56  * Revision 1.5  1995/03/15  08:37:41  quinn
57  * Now we're pretty much set for nonblocking I/O.
58  *
59  * Revision 1.4  1995/03/14  16:59:48  quinn
60  * Bug-fixes
61  *
62  * Revision 1.3  1995/03/14  11:30:14  quinn
63  * Works better now.
64  *
65  * Revision 1.2  1995/03/14  10:27:59  quinn
66  * More work on demo server.
67  *
68  * Revision 1.1  1995/03/10  18:22:44  quinn
69  * The rudiments of an asynchronous server.
70  *
71  */
72
73 #include <stdio.h>
74 #include <assert.h>
75 #include <sys/time.h>
76 #include <sys/types.h>
77 #include <unistd.h>
78 #include <stdlib.h>
79 #include <errno.h>
80 #include <string.h>
81 #ifdef _AIX
82 #include <sys/select.h>
83 #endif
84
85 #include <eventl.h>
86
87 #include <xmalloc.h>
88
89 static IOCHAN iochans = 0;
90
91 IOCHAN iochan_getchan(void)
92 {
93     return iochans;
94 }
95
96 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags)
97 {
98     IOCHAN new;
99
100     if (!(new = xmalloc(sizeof(*new))))
101         return 0;
102     new->destroyed = 0;
103     new->fd = fd;
104     new->flags = flags;
105     new->fun = cb;
106     new->next = iochans;
107     new->force_event = 0;
108     new->last_event = new->max_idle = 0;
109     iochans = new;
110     return new;
111 }
112
113 int event_loop()
114 {
115     do /* loop as long as there are active associations to process */
116     {
117         IOCHAN p, nextp;
118         fd_set in, out, except;
119         int res, max;
120         static struct timeval nullto = {0, 0}, to;
121         struct timeval *timeout;
122
123         FD_ZERO(&in);
124         FD_ZERO(&out);
125         FD_ZERO(&except);
126         timeout = &to; /* hang on select */
127         to.tv_sec = 5*60;
128         to.tv_usec = 0;
129         max = 0;
130         for (p = iochans; p; p = p->next)
131         {
132             if (p->force_event)
133                 timeout = &nullto;        /* polling select */
134             if (p->flags & EVENT_INPUT)
135                 FD_SET(p->fd, &in);
136             if (p->flags & EVENT_OUTPUT)
137                 FD_SET(p->fd, &out);
138             if (p->flags & EVENT_EXCEPT)
139                 FD_SET(p->fd, &except);
140             if (p->fd > max)
141                 max = p->fd;
142         }
143         if ((res = select(max + 1, &in, &out, &except, timeout)) < 0)
144         {
145             if (errno == EINTR)
146                 continue;
147             return 1;
148         }
149         for (p = iochans; p; p = p->next)
150         {
151             int force_event = p->force_event;
152             time_t now = time(0);
153
154             p->force_event = 0;
155             if (!p->destroyed && (FD_ISSET(p->fd, &in) || force_event ==
156                 EVENT_INPUT))
157             {
158                 p->last_event = now;
159                 (*p->fun)(p, EVENT_INPUT);
160             }
161             if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
162                 force_event == EVENT_OUTPUT))
163             {
164                 p->last_event = now;
165                 (*p->fun)(p, EVENT_OUTPUT);
166             }
167             if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
168                 force_event == EVENT_EXCEPT))
169             {
170                 p->last_event = now;
171                 (*p->fun)(p, EVENT_EXCEPT);
172             }
173             if (!p->destroyed && ((p->max_idle && now - p->last_event >
174                 p->max_idle) || force_event == EVENT_TIMEOUT))
175             {
176                 p->last_event = now;
177                 (*p->fun)(p, EVENT_TIMEOUT);
178             }
179         }
180         for (p = iochans; p; p = nextp)
181         {
182             nextp = p->next;
183
184             if (p->destroyed)
185             {
186                 IOCHAN tmp = p, pr;
187
188                 if (p == iochans)
189                     iochans = p->next;
190                 else
191                 {
192                     for (pr = iochans; pr; pr = pr->next)
193                         if (pr->next == p)
194                             break;
195                     assert(pr); /* grave error if it weren't there */
196                     pr->next = p->next;
197                 }
198                 if (nextp == p)
199                     nextp = p->next;
200                 xfree(tmp);
201             }
202         }
203     }
204     while (iochans);
205     return 0;
206 }