Electroneum
winsock_event.h
Go to the documentation of this file.
1 /*
2  * util/winsock_event.h - unbound event handling for winsock on windows
3  *
4  * Copyright (c) 2008, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
84 #ifndef UTIL_WINSOCK_EVENT_H
85 #define UTIL_WINSOCK_EVENT_H
86 
87 #ifdef USE_WINSOCK
88 
89 #ifndef HAVE_EVENT_BASE_FREE
90 #define HAVE_EVENT_BASE_FREE
91 #endif
92 
93 /* redefine the calls to different names so that there is no name
94  * collision with other code that uses libevent names. (that uses libunbound)*/
95 #define event_init winsockevent_init
96 #define event_get_version winsockevent_get_version
97 #define event_get_method winsockevent_get_method
98 #define event_base_dispatch winsockevent_base_dispatch
99 #define event_base_loopexit winsockevent_base_loopexit
100 #define event_base_free winsockevent_base_free
101 #define event_set winsockevent_set
102 #define event_base_set winsockevent_base_set
103 #define event_add winsockevent_add
104 #define event_del winsockevent_del
105 #define signal_add winsocksignal_add
106 #define signal_del winsocksignal_del
107 
109 #define EV_TIMEOUT 0x01
110 
111 #define EV_READ 0x02
112 
113 #define EV_WRITE 0x04
114 
115 #define EV_SIGNAL 0x08
116 
117 #define EV_PERSIST 0x10
118 
119 /* needs our redblack tree */
120 #include "rbtree.h"
121 
123 #define MAX_SIG 32
124 
127 #define WSK_MAX_ITEMS 64
128 
132 struct event_base
133 {
135  rbtree_type* times;
137  struct event** items;
139  int max;
141  int cap;
143  struct event** signals;
145  int need_to_exit;
147  time_t* time_secs;
149  struct timeval* time_tv;
156  int tcp_stickies;
162  int tcp_reinvigorated;
164  WSAEVENT waitfor[WSK_MAX_ITEMS];
165 };
166 
170 struct event {
172  rbnode_type node;
174  int added;
175 
177  struct event_base *ev_base;
179  int ev_fd;
181  short ev_events;
183  struct timeval ev_timeout;
184 
186  void (*ev_callback)(int, short, void *);
188  void *ev_arg;
189 
190  /* ----- nonpublic part, for winsock_event only ----- */
192  int idx;
194  WSAEVENT hEvent;
196  int is_tcp;
198  short old_events;
201  int stick_events;
202 
206  int is_signal;
208  int just_checked;
209 };
210 
212 void *event_init(time_t* time_secs, struct timeval* time_tv);
214 const char *event_get_version(void);
216 const char *event_get_method(void);
218 int event_base_dispatch(struct event_base *);
220 int event_base_loopexit(struct event_base *, struct timeval *);
222 void event_base_free(struct event_base *);
224 void event_set(struct event *, int, short, void (*)(int, short, void *), void *);
225 
227 int event_base_set(struct event_base *, struct event *);
229 int event_add(struct event *, struct timeval *);
231 int event_del(struct event *);
232 
233 #define evtimer_add(ev, tv) event_add(ev, tv)
234 #define evtimer_del(ev) event_del(ev)
235 
236 /* uses different implementation. Cannot mix fd/timeouts and signals inside
237  * the same struct event. create several event structs for that. */
239 int signal_add(struct event *, struct timeval *);
241 #define signal_set(ev, x, cb, arg) \
242  event_set(ev, x, EV_SIGNAL|EV_PERSIST, cb, arg)
243 
244 int signal_del(struct event *);
245 
247 int mini_ev_cmp(const void* a, const void* b);
248 
255 void winsock_tcp_wouldblock(struct event* ev, int eventbit);
256 
269 int winsock_register_wsaevent(struct event_base* base, struct event* ev,
270  WSAEVENT wsaevent, void (*cb)(int, short, void*), void* arg);
271 
276 void winsock_unregister_wsaevent(struct event* ev);
277 
278 #endif /* USE_WINSOCK */
279 #endif /* UTIL_WINSOCK_EVENT_H */
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124
int mini_ev_cmp(const void *a, const void *b)