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