2 * Copyright (c) 1995, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1995-06-16 10:31:33 quinn
8 * Added session timeout.
10 * Revision 1.9 1995/06/05 10:53:31 quinn
11 * Added a better SCAN.
13 * Revision 1.8 1995/05/16 08:51:01 quinn
14 * License, documentation, and memory fixes
16 * Revision 1.7 1995/03/27 15:02:01 quinn
17 * Added some includes for better portability
19 * Revision 1.6 1995/03/27 08:34:21 quinn
20 * Added dynamic server functionality.
21 * Released bindings to session.c (is now redundant)
23 * Revision 1.5 1995/03/15 08:37:41 quinn
24 * Now we're pretty much set for nonblocking I/O.
26 * Revision 1.4 1995/03/14 16:59:48 quinn
29 * Revision 1.3 1995/03/14 11:30:14 quinn
32 * Revision 1.2 1995/03/14 10:27:59 quinn
33 * More work on demo server.
35 * Revision 1.1 1995/03/10 18:22:44 quinn
36 * The rudiments of an asynchronous server.
42 #include <sys/types.h>
48 #include <sys/select.h>
55 static IOCHAN iochans = 0;
57 IOCHAN iochan_getchan(void)
62 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags)
66 if (!(new = malloc(sizeof(*new))))
74 new->last_event = new->max_idle = 0;
84 fd_set in, out, except;
86 static struct timeval nullto = {0, 0}, to = {60*5, 0};
87 struct timeval *timeout;
92 timeout = &to; /* hang on select */
94 for (p = iochans; p; p = p->next)
98 if (p->flags & EVENT_INPUT)
100 if (p->flags & EVENT_OUTPUT)
102 if (p->flags & EVENT_EXCEPT)
103 FD_SET(p->fd, &except);
107 if ((res = select(max + 1, &in, &out, &except, timeout)) < 0)
113 for (p = iochans; p; p = p->next)
115 int force_event = p->force_event;
116 time_t now = time(0);
119 if (FD_ISSET(p->fd, &in) || force_event == EVENT_INPUT)
122 (*p->fun)(p, EVENT_INPUT);
124 if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
125 force_event == EVENT_OUTPUT))
128 (*p->fun)(p, EVENT_OUTPUT);
130 if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
131 force_event == EVENT_EXCEPT))
134 (*p->fun)(p, EVENT_EXCEPT);
136 if (!p->destroyed && p->max_idle && now - p->last_event >
138 (*p->fun)(p, EVENT_TIMEOUT);
140 for (p = iochans; p; p = nextp)
152 for (pr = iochans; pr; pr = pr->next)
155 assert(pr); /* grave error if it weren't there */