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