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(void *ptr)
40 struct event_context *ev = talloc_get_type(ptr, struct event_context);
41 oop_source_sys *oop_sys = ev->additional_data;
43 oop_sys_delete(oop_sys);
49 create a oop_event_context structure.
51 static int oop_event_context_init(struct event_context *ev, void *private_data)
53 oop_source_sys *oop_sys = private_data;
56 oop_sys = oop_sys_new();
61 talloc_set_destructor(ev, oop_event_context_destructor);
64 ev->additional_data = oop_sys;
69 static void *oop_event_fd_handler(oop_source *oop, int fd, oop_event oop_type, void *ptr)
71 struct fd_event *fde = ptr;
73 if (fd != fde->fd) return OOP_ERROR;
77 fde->handler(fde->event_ctx, fde, EVENT_FD_READ, fde->private_data);
80 fde->handler(fde->event_ctx, fde, EVENT_FD_WRITE, fde->private_data);
94 static int oop_event_fd_destructor(void *ptr)
96 struct fd_event *fde = talloc_get_type(ptr, struct fd_event);
97 struct event_context *ev = fde->event_ctx;
98 oop_source_sys *oop_sys = ev->additional_data;
99 oop_source *oop = oop_sys_source(oop_sys);
101 if (fde->flags & EVENT_FD_READ)
102 oop->cancel_fd(oop, fde->fd, OOP_READ);
103 if (fde->flags & EVENT_FD_WRITE)
104 oop->cancel_fd(oop, fde->fd, OOP_WRITE);
111 return NULL on failure (memory allocation error)
113 static struct fd_event *oop_event_add_fd(struct event_context *ev, TALLOC_CTX *mem_ctx,
114 int fd, uint16_t flags,
115 event_fd_handler_t handler,
118 struct fd_event *fde;
119 oop_source_sys *oop_sys = ev->additional_data;
120 oop_source *oop = oop_sys_source(oop_sys);
122 fde = talloc(mem_ctx?mem_ctx:ev, struct fd_event);
123 if (!fde) return NULL;
128 fde->handler = handler;
129 fde->private_data = private_data;
130 fde->additional_flags = 0;
131 fde->additional_data = NULL;
133 if (fde->flags & EVENT_FD_READ)
134 oop->on_fd(oop, fde->fd, OOP_READ, oop_event_fd_handler, fde);
135 if (fde->flags & EVENT_FD_WRITE)
136 oop->on_fd(oop, fde->fd, OOP_WRITE, oop_event_fd_handler, fde);
138 talloc_set_destructor(fde, oop_event_fd_destructor);
144 return the fd event flags
146 static uint16_t oop_event_get_fd_flags(struct fd_event *fde)
152 set the fd event flags
154 static void oop_event_set_fd_flags(struct fd_event *fde, uint16_t flags)
156 oop_source_sys *oop_sys;
159 oop_sys = fde->event_ctx->additional_data;
160 oop = oop_sys_source(oop_sys);
162 if ((fde->flags & EVENT_FD_READ)&&(!(flags & EVENT_FD_READ)))
163 oop->cancel_fd(oop, fde->fd, OOP_READ);
165 if ((!(fde->flags & EVENT_FD_READ))&&(flags & EVENT_FD_READ))
166 oop->on_fd(oop, fde->fd, OOP_READ, oop_event_fd_handler, fde);
168 if ((fde->flags & EVENT_FD_WRITE)&&(!(flags & EVENT_FD_WRITE)))
169 oop->cancel_fd(oop, fde->fd, OOP_WRITE);
171 if ((!(fde->flags & EVENT_FD_WRITE))&&(flags & EVENT_FD_WRITE))
172 oop->on_fd(oop, fde->fd, OOP_WRITE, oop_event_fd_handler, fde);
177 static int oop_event_timed_destructor(void *ptr);
178 static int oop_event_timed_deny_destructor(void *ptr)
183 static void *oop_event_timed_handler(oop_source *oop, struct timeval t, void *ptr)
185 struct timed_event *te = ptr;
187 /* deny the handler to free the event */
188 talloc_set_destructor(te, oop_event_timed_deny_destructor);
189 te->handler(te->event_ctx, te, t, te->private_data);
191 talloc_set_destructor(te, oop_event_timed_destructor);
198 destroy a timed event
200 static int oop_event_timed_destructor(void *ptr)
202 struct timed_event *te = talloc_get_type(ptr, struct timed_event);
203 struct event_context *ev = te->event_ctx;
204 oop_source_sys *oop_sys = ev->additional_data;
205 oop_source *oop = oop_sys_source(oop_sys);
207 oop->cancel_time(oop, te->next_event, oop_event_timed_handler, te);
214 return NULL on failure (memory allocation error)
216 static struct timed_event *oop_event_add_timed(struct event_context *ev, TALLOC_CTX *mem_ctx,
217 struct timeval next_event,
218 event_timed_handler_t handler,
221 oop_source_sys *oop_sys = ev->additional_data;
222 oop_source *oop = oop_sys_source(oop_sys);
223 struct timed_event *te;
225 te = talloc(mem_ctx?mem_ctx:ev, struct timed_event);
226 if (te == NULL) return NULL;
229 te->next_event = next_event;
230 te->handler = handler;
231 te->private_data = private_data;
232 te->additional_data = NULL;
234 oop->on_time(oop, te->next_event, oop_event_timed_handler, te);
236 talloc_set_destructor(te, oop_event_timed_destructor);
242 do a single event loop using the events defined in ev
244 static int oop_event_loop_once(struct event_context *ev)
247 oop_source_sys *oop_sys = ev->additional_data;
249 oop_ret = oop_sys_run_once(oop_sys);
250 if (oop_ret == OOP_CONTINUE) {
258 return on failure or (with 0) if all fd events are removed
260 static int oop_event_loop_wait(struct event_context *ev)
263 oop_source_sys *oop_sys = ev->additional_data;
265 oop_ret = oop_sys_run(oop_sys);
266 if (oop_ret == OOP_CONTINUE) {
273 static const struct event_ops event_oop_ops = {
274 .context_init = oop_event_context_init,
275 .add_fd = oop_event_add_fd,
276 .get_fd_flags = oop_event_get_fd_flags,
277 .set_fd_flags = oop_event_set_fd_flags,
278 .add_timed = oop_event_add_timed,
279 .loop_once = oop_event_loop_once,
280 .loop_wait = oop_event_loop_wait,
283 const struct event_ops *event_liboop_get_ops(void)
285 return &event_oop_ops;