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