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