2 Unix SMB/CIFS implementation.
3 main select loop and event handling
4 wrapper for http://liboop.org/
6 Copyright (C) Stefan Metzmacher 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "lib/events/events.h"
25 #include "lib/events/events_internal.h"
30 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
32 NOTE: this code compiles fine, but is completly *UNTESTED*
33 and is only commited as example
35 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
38 static int oop_event_context_destructor(struct event_context *ev)
40 oop_source_sys *oop_sys = ev->additional_data;
42 oop_sys_delete(oop_sys);
48 create a oop_event_context structure.
50 static int oop_event_context_init(struct event_context *ev, void *private_data)
52 oop_source_sys *oop_sys = private_data;
55 oop_sys = oop_sys_new();
60 talloc_set_destructor(ev, oop_event_context_destructor);
63 ev->additional_data = oop_sys;
68 static void *oop_event_fd_handler(oop_source *oop, int fd, oop_event oop_type, void *ptr)
70 struct fd_event *fde = ptr;
72 if (fd != fde->fd) return OOP_ERROR;
76 fde->handler(fde->event_ctx, fde, EVENT_FD_READ, fde->private_data);
79 fde->handler(fde->event_ctx, fde, EVENT_FD_WRITE, fde->private_data);
93 static int oop_event_fd_destructor(struct fd_event *fde)
95 struct event_context *ev = fde->event_ctx;
96 oop_source_sys *oop_sys = ev->additional_data;
97 oop_source *oop = oop_sys_source(oop_sys);
99 if (fde->flags & EVENT_FD_READ)
100 oop->cancel_fd(oop, fde->fd, OOP_READ);
101 if (fde->flags & EVENT_FD_WRITE)
102 oop->cancel_fd(oop, fde->fd, OOP_WRITE);
104 if (fde->flags & EVENT_FD_AUTOCLOSE) {
114 return NULL on failure (memory allocation error)
116 static struct fd_event *oop_event_add_fd(struct event_context *ev, TALLOC_CTX *mem_ctx,
117 int fd, uint16_t flags,
118 event_fd_handler_t handler,
121 struct fd_event *fde;
122 oop_source_sys *oop_sys = ev->additional_data;
123 oop_source *oop = oop_sys_source(oop_sys);
125 fde = talloc(mem_ctx?mem_ctx:ev, struct fd_event);
126 if (!fde) return NULL;
131 fde->handler = handler;
132 fde->private_data = private_data;
133 fde->additional_flags = 0;
134 fde->additional_data = NULL;
136 if (fde->flags & EVENT_FD_READ)
137 oop->on_fd(oop, fde->fd, OOP_READ, oop_event_fd_handler, fde);
138 if (fde->flags & EVENT_FD_WRITE)
139 oop->on_fd(oop, fde->fd, OOP_WRITE, oop_event_fd_handler, fde);
141 talloc_set_destructor(fde, oop_event_fd_destructor);
147 return the fd event flags
149 static uint16_t oop_event_get_fd_flags(struct fd_event *fde)
155 set the fd event flags
157 static void oop_event_set_fd_flags(struct fd_event *fde, uint16_t flags)
159 oop_source_sys *oop_sys;
162 oop_sys = fde->event_ctx->additional_data;
163 oop = oop_sys_source(oop_sys);
165 if ((fde->flags & EVENT_FD_READ)&&(!(flags & EVENT_FD_READ)))
166 oop->cancel_fd(oop, fde->fd, OOP_READ);
168 if ((!(fde->flags & EVENT_FD_READ))&&(flags & EVENT_FD_READ))
169 oop->on_fd(oop, fde->fd, OOP_READ, oop_event_fd_handler, fde);
171 if ((fde->flags & EVENT_FD_WRITE)&&(!(flags & EVENT_FD_WRITE)))
172 oop->cancel_fd(oop, fde->fd, OOP_WRITE);
174 if ((!(fde->flags & EVENT_FD_WRITE))&&(flags & EVENT_FD_WRITE))
175 oop->on_fd(oop, fde->fd, OOP_WRITE, oop_event_fd_handler, fde);
180 static int oop_event_timed_destructor(struct timed_event *te);
182 static int oop_event_timed_deny_destructor(struct timed_event *te)
187 static void *oop_event_timed_handler(oop_source *oop, struct timeval t, void *ptr)
189 struct timed_event *te = ptr;
191 /* deny the handler to free the event */
192 talloc_set_destructor(te, oop_event_timed_deny_destructor);
193 te->handler(te->event_ctx, te, t, te->private_data);
195 talloc_set_destructor(te, oop_event_timed_destructor);
202 destroy a timed event
204 static int oop_event_timed_destructor(struct timed_event *te)
206 struct event_context *ev = te->event_ctx;
207 oop_source_sys *oop_sys = ev->additional_data;
208 oop_source *oop = oop_sys_source(oop_sys);
210 oop->cancel_time(oop, te->next_event, oop_event_timed_handler, te);
217 return NULL on failure (memory allocation error)
219 static struct timed_event *oop_event_add_timed(struct event_context *ev, TALLOC_CTX *mem_ctx,
220 struct timeval next_event,
221 event_timed_handler_t handler,
224 oop_source_sys *oop_sys = ev->additional_data;
225 oop_source *oop = oop_sys_source(oop_sys);
226 struct timed_event *te;
228 te = talloc(mem_ctx?mem_ctx:ev, struct timed_event);
229 if (te == NULL) return NULL;
232 te->next_event = next_event;
233 te->handler = handler;
234 te->private_data = private_data;
235 te->additional_data = NULL;
237 oop->on_time(oop, te->next_event, oop_event_timed_handler, te);
239 talloc_set_destructor(te, oop_event_timed_destructor);
245 do a single event loop using the events defined in ev
247 static int oop_event_loop_once(struct event_context *ev)
250 oop_source_sys *oop_sys = ev->additional_data;
252 oop_ret = oop_sys_run_once(oop_sys);
253 if (oop_ret == OOP_CONTINUE) {
261 return on failure or (with 0) if all fd events are removed
263 static int oop_event_loop_wait(struct event_context *ev)
266 oop_source_sys *oop_sys = ev->additional_data;
268 oop_ret = oop_sys_run(oop_sys);
269 if (oop_ret == OOP_CONTINUE) {
276 static const struct event_ops event_oop_ops = {
277 .context_init = oop_event_context_init,
278 .add_fd = oop_event_add_fd,
279 .get_fd_flags = oop_event_get_fd_flags,
280 .set_fd_flags = oop_event_set_fd_flags,
281 .add_timed = oop_event_add_timed,
282 .add_signal = common_event_add_signal,
283 .loop_once = oop_event_loop_once,
284 .loop_wait = oop_event_loop_wait,
287 const struct event_ops *event_liboop_get_ops(void)
289 return &event_oop_ops;