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