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