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