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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "events_internal.h"
28 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
30 NOTE: this code compiles fine, but is completly *UNTESTED*
31 and is only commited as example
33 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
36 static int oop_event_context_destructor(struct tevent_context *ev)
38 oop_source_sys *oop_sys = ev->additional_data;
40 oop_sys_delete(oop_sys);
46 create a oop_event_context structure.
48 static int oop_event_context_init(struct tevent_context *ev, void *private_data)
50 oop_source_sys *oop_sys = private_data;
53 oop_sys = oop_sys_new();
58 talloc_set_destructor(ev, oop_event_context_destructor);
61 ev->additional_data = oop_sys;
66 static void *oop_event_fd_handler(oop_source *oop, int fd, oop_event oop_type, void *ptr)
68 struct tevent_fd *fde = ptr;
70 if (fd != fde->fd) return OOP_ERROR;
74 fde->handler(fde->event_ctx, fde, EVENT_FD_READ, fde->private_data);
77 fde->handler(fde->event_ctx, fde, EVENT_FD_WRITE, fde->private_data);
91 static int oop_event_fd_destructor(struct tevent_fd *fde)
93 struct tevent_context *ev = fde->event_ctx;
94 oop_source_sys *oop_sys = ev->additional_data;
95 oop_source *oop = oop_sys_source(oop_sys);
97 if (fde->flags & EVENT_FD_READ)
98 oop->cancel_fd(oop, fde->fd, OOP_READ);
99 if (fde->flags & EVENT_FD_WRITE)
100 oop->cancel_fd(oop, fde->fd, OOP_WRITE);
102 if (fde->flags & EVENT_FD_AUTOCLOSE) {
112 return NULL on failure (memory allocation error)
114 static struct tevent_fd *oop_event_add_fd(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
115 int fd, uint16_t flags,
116 event_fd_handler_t handler,
119 struct tevent_fd *fde;
120 oop_source_sys *oop_sys = ev->additional_data;
121 oop_source *oop = oop_sys_source(oop_sys);
123 fde = talloc(mem_ctx?mem_ctx:ev, struct tevent_fd);
124 if (!fde) return NULL;
129 fde->handler = handler;
130 fde->private_data = private_data;
131 fde->additional_flags = 0;
132 fde->additional_data = NULL;
134 if (fde->flags & EVENT_FD_READ)
135 oop->on_fd(oop, fde->fd, OOP_READ, oop_event_fd_handler, fde);
136 if (fde->flags & EVENT_FD_WRITE)
137 oop->on_fd(oop, fde->fd, OOP_WRITE, oop_event_fd_handler, fde);
139 talloc_set_destructor(fde, oop_event_fd_destructor);
145 return the fd event flags
147 static uint16_t oop_event_get_fd_flags(struct tevent_fd *fde)
153 set the fd event flags
155 static void oop_event_set_fd_flags(struct tevent_fd *fde, uint16_t flags)
157 oop_source_sys *oop_sys;
160 oop_sys = fde->event_ctx->additional_data;
161 oop = oop_sys_source(oop_sys);
163 if ((fde->flags & EVENT_FD_READ)&&(!(flags & EVENT_FD_READ)))
164 oop->cancel_fd(oop, fde->fd, OOP_READ);
166 if ((!(fde->flags & EVENT_FD_READ))&&(flags & EVENT_FD_READ))
167 oop->on_fd(oop, fde->fd, OOP_READ, oop_event_fd_handler, fde);
169 if ((fde->flags & EVENT_FD_WRITE)&&(!(flags & EVENT_FD_WRITE)))
170 oop->cancel_fd(oop, fde->fd, OOP_WRITE);
172 if ((!(fde->flags & EVENT_FD_WRITE))&&(flags & EVENT_FD_WRITE))
173 oop->on_fd(oop, fde->fd, OOP_WRITE, oop_event_fd_handler, fde);
178 static int oop_event_timed_destructor(struct tevent_timer *te);
180 static int oop_event_timed_deny_destructor(struct tevent_timer *te)
185 static void *oop_event_timed_handler(oop_source *oop, struct timeval t, void *ptr)
187 struct tevent_timer *te = ptr;
189 /* deny the handler to free the event */
190 talloc_set_destructor(te, oop_event_timed_deny_destructor);
191 te->handler(te->event_ctx, te, t, te->private_data);
193 talloc_set_destructor(te, oop_event_timed_destructor);
200 destroy a timed event
202 static int oop_event_timed_destructor(struct tevent_timer *te)
204 struct tevent_context *ev = te->event_ctx;
205 oop_source_sys *oop_sys = ev->additional_data;
206 oop_source *oop = oop_sys_source(oop_sys);
208 oop->cancel_time(oop, te->next_event, oop_event_timed_handler, te);
215 return NULL on failure (memory allocation error)
217 static struct tevent_timer *oop_event_add_timed(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
218 struct timeval next_event,
219 event_timed_handler_t handler,
222 oop_source_sys *oop_sys = ev->additional_data;
223 oop_source *oop = oop_sys_source(oop_sys);
224 struct tevent_timer *te;
226 te = talloc(mem_ctx?mem_ctx:ev, struct tevent_timer);
227 if (te == NULL) return NULL;
230 te->next_event = next_event;
231 te->handler = handler;
232 te->private_data = private_data;
233 te->additional_data = NULL;
235 oop->on_time(oop, te->next_event, oop_event_timed_handler, te);
237 talloc_set_destructor(te, oop_event_timed_destructor);
243 do a single event loop using the events defined in ev
245 static int oop_event_loop_once(struct tevent_context *ev)
248 oop_source_sys *oop_sys = ev->additional_data;
250 oop_ret = oop_sys_run_once(oop_sys);
251 if (oop_ret == OOP_CONTINUE) {
259 return on failure or (with 0) if all fd events are removed
261 static int oop_event_loop_wait(struct tevent_context *ev)
264 oop_source_sys *oop_sys = ev->additional_data;
266 oop_ret = oop_sys_run(oop_sys);
267 if (oop_ret == OOP_CONTINUE) {
274 static const struct event_ops event_oop_ops = {
275 .context_init = oop_event_context_init,
276 .add_fd = oop_event_add_fd,
277 .get_fd_flags = oop_event_get_fd_flags,
278 .set_fd_flags = oop_event_set_fd_flags,
279 .add_timer = oop_event_add_timed,
280 .add_signal = common_event_add_signal,
281 .loop_once = oop_event_loop_once,
282 .loop_wait = oop_event_loop_wait,
285 const struct event_ops *event_liboop_get_ops(void)
287 return &event_oop_ops;