Uses preprocessor define WIN32 instead of WINDOWS to build code
[yaz-moved-to-github.git] / server / eventl.c
1 /*
2  * Copyright (c) 1995-1999, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: eventl.c,v $
7  * Revision 1.27  1999-02-02 13:57:34  adam
8  * Uses preprocessor define WIN32 instead of WINDOWS to build code
9  * for Microsoft WIN32.
10  *
11  * Revision 1.26  1998/02/11 11:53:35  adam
12  * Changed code so that it compiles as C++.
13  *
14  * Revision 1.25  1998/01/29 13:30:23  adam
15  * Better event handle system for NT/Unix.
16  *
17  * Revision 1.24  1997/09/04 14:19:13  adam
18  * Added credits.
19  *
20  * Revision 1.23  1997/09/01 08:52:59  adam
21  * New windows NT/95 port using MSV5.0. The test server 'ztest' was
22  * moved a separate directory. MSV5.0 project server.dsp created.
23  * As an option, the server can now operate as an NT service.
24  *
25  * Revision 1.22  1996/07/06 19:58:35  quinn
26  * System headerfiles gathered in yconfig
27  *
28  * Revision 1.21  1996/02/21  12:55:51  quinn
29  * small
30  *
31  * Revision 1.20  1996/02/21  12:52:55  quinn
32  * Test
33  *
34  * Revision 1.19  1995/12/05  11:17:30  quinn
35  * Moved some paranthesises around. Sigh.
36  *
37  * Revision 1.18  1995/11/13  09:27:41  quinn
38  * Fiddling with the variant stuff.
39  *
40  * Revision 1.17  1995/11/07  12:37:44  quinn
41  * Added support for forcing TIMEOUT event.
42  *
43  * Revision 1.16  1995/11/01  13:54:56  quinn
44  * Minor adjustments
45  *
46  * Revision 1.15  1995/09/15  14:44:15  quinn
47  * *** empty log message ***
48  *
49  * Revision 1.14  1995/08/29  14:44:50  quinn
50  * Reset timeouts.
51  *
52  * Revision 1.13  1995/08/29  11:17:56  quinn
53  * Added code to receive close
54  *
55  * Revision 1.12  1995/08/29  10:41:18  quinn
56  * Small.
57  *
58  * Revision 1.11  1995/06/19  12:39:09  quinn
59  * Fixed bug in timeout code. Added BER dumper.
60  *
61  * Revision 1.10  1995/06/16  10:31:33  quinn
62  * Added session timeout.
63  *
64  * Revision 1.9  1995/06/05  10:53:31  quinn
65  * Added a better SCAN.
66  *
67  * Revision 1.8  1995/05/16  08:51:01  quinn
68  * License, documentation, and memory fixes
69  *
70  * Revision 1.7  1995/03/27  15:02:01  quinn
71  * Added some includes for better portability
72  *
73  * Revision 1.6  1995/03/27  08:34:21  quinn
74  * Added dynamic server functionality.
75  * Released bindings to session.c (is now redundant)
76  *
77  * Revision 1.5  1995/03/15  08:37:41  quinn
78  * Now we're pretty much set for nonblocking I/O.
79  *
80  * Revision 1.4  1995/03/14  16:59:48  quinn
81  * Bug-fixes
82  *
83  * Revision 1.3  1995/03/14  11:30:14  quinn
84  * Works better now.
85  *
86  * Revision 1.2  1995/03/14  10:27:59  quinn
87  * More work on demo server.
88  *
89  * Revision 1.1  1995/03/10  18:22:44  quinn
90  * The rudiments of an asynchronous server.
91  *
92  */
93
94 #include <yconfig.h>
95 #include <stdio.h>
96 #include <assert.h>
97 #ifdef WIN32
98 #include <winsock.h>
99 #else
100 #include <unistd.h>
101 #endif
102 #include <stdlib.h>
103 #include <errno.h>
104 #include <string.h>
105
106 #include <log.h>
107 #include <comstack.h>
108 #include <xmalloc.h>
109 #include "eventl.h"
110 #include "session.h"
111 #include <statserv.h>
112
113 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags)
114 {
115     IOCHAN new_iochan;
116
117     if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
118         return 0;
119     new_iochan->destroyed = 0;
120     new_iochan->fd = fd;
121     new_iochan->flags = flags;
122     new_iochan->fun = cb;
123     new_iochan->force_event = 0;
124     new_iochan->last_event = new_iochan->max_idle = 0;
125     new_iochan->next = NULL;
126     return new_iochan;
127 }
128
129 int event_loop(IOCHAN *iochans)
130 {
131     do /* loop as long as there are active associations to process */
132     {
133         IOCHAN p, nextp;
134         fd_set in, out, except;
135         int res, max;
136         static struct timeval nullto = {0, 0}, to;
137         struct timeval *timeout;
138
139         FD_ZERO(&in);
140         FD_ZERO(&out);
141         FD_ZERO(&except);
142         timeout = &to; /* hang on select */
143         to.tv_sec = 5*60;
144         to.tv_usec = 0;
145         max = 0;
146         for (p = *iochans; p; p = p->next)
147         {
148             if (p->force_event)
149                 timeout = &nullto;        /* polling select */
150             if (p->flags & EVENT_INPUT)
151                 FD_SET(p->fd, &in);
152             if (p->flags & EVENT_OUTPUT)
153                 FD_SET(p->fd, &out);
154             if (p->flags & EVENT_EXCEPT)
155                 FD_SET(p->fd, &except);
156             if (p->fd > max)
157                 max = p->fd;
158         }
159         if ((res = select(max + 1, &in, &out, &except, timeout)) < 0)
160         {
161             if (errno == EINTR)
162                 continue;
163             else
164             {
165                 /* Destroy the first member in the chain, and try again */
166                 association *assoc = (association *)iochan_getdata(*iochans);
167                 COMSTACK conn = assoc->client_link;
168
169                 cs_close(conn);
170                 destroy_association(assoc);
171                 iochan_destroy(*iochans);
172                 logf(LOG_DEBUG, "error while selecting, destroying iochan %p",
173                         *iochans);
174             }
175         }
176         for (p = *iochans; p; p = p->next)
177         {
178             int force_event = p->force_event;
179             time_t now = time(0);
180
181             p->force_event = 0;
182             if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
183                 force_event == EVENT_INPUT))
184             {
185                 p->last_event = now;
186                 (*p->fun)(p, EVENT_INPUT);
187             }
188             if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
189                 force_event == EVENT_OUTPUT))
190             {
191                 p->last_event = now;
192                 (*p->fun)(p, EVENT_OUTPUT);
193             }
194             if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
195                 force_event == EVENT_EXCEPT))
196             {
197                 p->last_event = now;
198                 (*p->fun)(p, EVENT_EXCEPT);
199             }
200             if (!p->destroyed && ((p->max_idle && now - p->last_event >
201                 p->max_idle) || force_event == EVENT_TIMEOUT))
202             {
203                 p->last_event = now;
204                 (*p->fun)(p, EVENT_TIMEOUT);
205             }
206         }
207         for (p = *iochans; p; p = nextp)
208         {
209             nextp = p->next;
210
211             if (p->destroyed)
212             {
213                 IOCHAN tmp = p, pr;
214
215                 /* We need to inform the threadlist that this channel has been destroyed */
216                 statserv_remove(p);
217
218                 /* Now reset the pointers */
219                 if (p == *iochans)
220                     *iochans = p->next;
221                 else
222                 {
223                     for (pr = *iochans; pr; pr = pr->next)
224                         if (pr->next == p)
225                             break;
226                     assert(pr); /* grave error if it weren't there */
227                     pr->next = p->next;
228                 }
229                 if (nextp == p)
230                     nextp = p->next;
231                 xfree(tmp);
232             }
233         }
234     }
235     while (*iochans);
236     return 0;
237 }