tevent: add tevent_req_received() function
[Samba/bb.git] / lib / tevent / tevent_internal.h
blob5a645ecb6082d1e5ddbce1ef140283cc437ed16b
1 /*
2 Unix SMB/CIFS implementation.
4 generalised event loop handling
6 Internal structs
8 Copyright (C) Stefan Metzmacher 2005-2009
10 ** NOTE! The following LGPL license applies to the tevent
11 ** library. This does NOT imply that all of Samba is released
12 ** under the LGPL
14 This library is free software; you can redistribute it and/or
15 modify it under the terms of the GNU Lesser General Public
16 License as published by the Free Software Foundation; either
17 version 3 of the License, or (at your option) any later version.
19 This library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
24 You should have received a copy of the GNU Lesser General Public
25 License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 struct tevent_req {
29 /**
30 * @brief What to do on completion
32 * This is used for the user of an async request, fn is called when
33 * the request completes, either successfully or with an error.
35 struct {
36 /**
37 * @brief Completion function
38 * Completion function, to be filled by the API user
40 tevent_req_fn fn;
41 /**
42 * @brief Private data for the completion function
44 void *private_data;
45 } async;
47 /**
48 * @brief Private state pointer for the actual implementation
50 * The implementation doing the work for the async request needs to
51 * keep around current data like for example a fd event. The user of
52 * an async request should not touch this.
54 void *data;
56 /**
57 * @brief A function to overwrite the default print function
59 * The implementation doing the work may want to implement a
60 * custom function to print the text representation of the async
61 * request.
63 tevent_req_print_fn private_print;
65 /**
66 * @brief Internal state of the request
68 * Callers should only access this via functions and never directly.
70 struct {
71 /**
72 * @brief The talloc type of the data pointer
74 * This is filled by the tevent_req_create() macro.
76 * This for debugging only.
78 const char *private_type;
80 /**
81 * @brief The location where the request was created
83 * This uses the __location__ macro via the tevent_req_create()
84 * macro.
86 * This for debugging only.
88 const char *location;
90 /**
91 * @brief The external state - will be queried by the caller
93 * While the async request is being processed, state will remain in
94 * TEVENT_REQ_IN_PROGRESS. A request is finished if
95 * req->state>=TEVENT_REQ_DONE.
97 enum tevent_req_state state;
99 /**
100 * @brief status code when finished
102 * This status can be queried in the async completion function. It
103 * will be set to 0 when everything went fine.
105 uint64_t error;
108 * @brief the timer event if tevent_req_post was used
111 struct tevent_timer *trigger;
114 * @brief the timer event if tevent_req_set_timeout was used
117 struct tevent_timer *timer;
118 } internal;
121 struct tevent_ops {
122 /* conntext init */
123 int (*context_init)(struct tevent_context *ev);
125 /* fd_event functions */
126 struct tevent_fd *(*add_fd)(struct tevent_context *ev,
127 TALLOC_CTX *mem_ctx,
128 int fd, uint16_t flags,
129 tevent_fd_handler_t handler,
130 void *private_data,
131 const char *handler_name,
132 const char *location);
133 void (*set_fd_close_fn)(struct tevent_fd *fde,
134 tevent_fd_close_fn_t close_fn);
135 uint16_t (*get_fd_flags)(struct tevent_fd *fde);
136 void (*set_fd_flags)(struct tevent_fd *fde, uint16_t flags);
138 /* timed_event functions */
139 struct tevent_timer *(*add_timer)(struct tevent_context *ev,
140 TALLOC_CTX *mem_ctx,
141 struct timeval next_event,
142 tevent_timer_handler_t handler,
143 void *private_data,
144 const char *handler_name,
145 const char *location);
146 /* signal functions */
147 struct tevent_signal *(*add_signal)(struct tevent_context *ev,
148 TALLOC_CTX *mem_ctx,
149 int signum, int sa_flags,
150 tevent_signal_handler_t handler,
151 void *private_data,
152 const char *handler_name,
153 const char *location);
155 /* loop functions */
156 int (*loop_once)(struct tevent_context *ev);
157 int (*loop_wait)(struct tevent_context *ev);
160 struct tevent_fd {
161 struct tevent_fd *prev, *next;
162 struct tevent_context *event_ctx;
163 int fd;
164 uint16_t flags; /* see TEVENT_FD_* flags */
165 tevent_fd_handler_t handler;
166 tevent_fd_close_fn_t close_fn;
167 /* this is private for the specific handler */
168 void *private_data;
169 /* this is for debugging only! */
170 const char *handler_name;
171 const char *location;
172 /* this is private for the events_ops implementation */
173 uint16_t additional_flags;
174 void *additional_data;
177 struct tevent_timer {
178 struct tevent_timer *prev, *next;
179 struct tevent_context *event_ctx;
180 struct timeval next_event;
181 tevent_timer_handler_t handler;
182 /* this is private for the specific handler */
183 void *private_data;
184 /* this is for debugging only! */
185 const char *handler_name;
186 const char *location;
187 /* this is private for the events_ops implementation */
188 void *additional_data;
191 struct tevent_signal {
192 struct tevent_signal *prev, *next;
193 struct tevent_context *event_ctx;
194 int signum;
195 int sa_flags;
196 tevent_signal_handler_t handler;
197 /* this is private for the specific handler */
198 void *private_data;
199 /* this is for debugging only! */
200 const char *handler_name;
201 const char *location;
202 /* this is private for the events_ops implementation */
203 void *additional_data;
206 struct tevent_debug_ops {
207 void (*debug)(void *context, enum tevent_debug_level level,
208 const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
209 void *context;
212 void tevent_debug(struct tevent_context *ev, enum tevent_debug_level level,
213 const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
215 struct tevent_context {
216 /* the specific events implementation */
217 const struct tevent_ops *ops;
219 /* list of fd events - used by common code */
220 struct tevent_fd *fd_events;
222 /* list of timed events - used by common code */
223 struct tevent_timer *timer_events;
225 /* list of signal events - used by common code */
226 struct tevent_signal *signal_events;
228 /* this is private for the events_ops implementation */
229 void *additional_data;
231 /* pipe hack used with signal handlers */
232 struct tevent_fd *pipe_fde;
234 /* debugging operations */
235 struct tevent_debug_ops debug_ops;
239 bool tevent_register_backend(const char *name, const struct tevent_ops *ops);
241 int tevent_common_context_destructor(struct tevent_context *ev);
243 int tevent_common_fd_destructor(struct tevent_fd *fde);
244 struct tevent_fd *tevent_common_add_fd(struct tevent_context *ev,
245 TALLOC_CTX *mem_ctx,
246 int fd,
247 uint16_t flags,
248 tevent_fd_handler_t handler,
249 void *private_data,
250 const char *handler_name,
251 const char *location);
252 void tevent_common_fd_set_close_fn(struct tevent_fd *fde,
253 tevent_fd_close_fn_t close_fn);
254 uint16_t tevent_common_fd_get_flags(struct tevent_fd *fde);
255 void tevent_common_fd_set_flags(struct tevent_fd *fde, uint16_t flags);
257 struct tevent_timer *tevent_common_add_timer(struct tevent_context *ev,
258 TALLOC_CTX *mem_ctx,
259 struct timeval next_event,
260 tevent_timer_handler_t handler,
261 void *private_data,
262 const char *handler_name,
263 const char *location);
264 struct timeval tevent_common_loop_timer_delay(struct tevent_context *);
266 struct tevent_signal *tevent_common_add_signal(struct tevent_context *ev,
267 TALLOC_CTX *mem_ctx,
268 int signum,
269 int sa_flags,
270 tevent_signal_handler_t handler,
271 void *private_data,
272 const char *handler_name,
273 const char *location);
274 int tevent_common_check_signal(struct tevent_context *ev);
276 bool tevent_standard_init(void);
277 bool tevent_select_init(void);
278 #ifdef HAVE_EPOLL
279 bool tevent_epoll_init(void);
280 #endif