2 This file is part of GNUnet.
3 Copyright (C) 2012-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @author Florian Dold
25 * General-purpose message queue
27 * @defgroup mq MQ library
28 * General-purpose message queue
36 * Allocate an envelope, with extra space allocated after the space needed
37 * by the message struct.
38 * The allocated message will already have the type and size field set.
40 * @param mvar variable to store the allocated message in;
41 * must have a header field
42 * @param esize extra space to allocate after the message
43 * @param type type of the message
44 * @return the MQ message
46 #define GNUNET_MQ_msg_extra(mvar, esize, type) GNUNET_MQ_msg_(((struct GNUNET_MessageHeader**) &(mvar)), (esize) + sizeof *(mvar), (type))
49 * Allocate a GNUNET_MQ_Envelope.
50 * The contained message will already have the type and size field set.
52 * @param mvar variable to store the allocated message in;
53 * must have a header field
54 * @param type type of the message
55 * @return the allocated envelope
57 #define GNUNET_MQ_msg(mvar, type) GNUNET_MQ_msg_extra(mvar, 0, type)
61 * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header.
62 * The allocated message will already have the type and size field set.
64 * @param type type of the message
66 #define GNUNET_MQ_msg_header(type) GNUNET_MQ_msg_ (NULL, sizeof (struct GNUNET_MessageHeader), type)
70 * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header and extra space.
71 * The allocated message will already have the type and size field set.
73 * @param mh pointer that will changed to point at to the allocated message header
74 * @param esize extra space to allocate after the message header
75 * @param type type of the message
77 #define GNUNET_MQ_msg_header_extra(mh, esize, type) GNUNET_MQ_msg_ (&mh, (esize) + sizeof (struct GNUNET_MessageHeader), type)
81 * Allocate a GNUNET_MQ_Envelope, and append a payload message after the given
84 * @param mvar pointer to a message struct, will be changed to point at the newly allocated message,
85 * whose size is 'sizeof(*mvar) + ntohs (mh->size)'
86 * @param type message type of the allocated message, has no effect on the nested message
87 * @param mh message to nest
88 * @return a newly allocated 'struct GNUNET_MQ_Envelope *'
90 #define GNUNET_MQ_msg_nested_mh(mvar, type, mh) GNUNET_MQ_msg_nested_mh_((((void)(mvar)->header), (struct GNUNET_MessageHeader**) &(mvar)), sizeof (*(mvar)), (type), mh)
94 * Return a pointer to the message at the end of the given message.
96 * @param var pointer to a message struct, the type of the expression determines the base size,
97 * the space after the base size is the nested message
98 * @return a 'struct GNUNET_MessageHeader *' that points at the nested message of the given message,
99 * or NULL if the given message in @a var does not have any space after the message struct
101 #define GNUNET_MQ_extract_nested_mh(var) GNUNET_MQ_extract_nested_mh_ ((struct GNUNET_MessageHeader *) (var), sizeof (*(var)))
105 * Implementation of the GNUNET_MQ_extract_nexted_mh macro.
107 * @param mh message header to extract nested message header from
108 * @param base_size size of the message before the nested message's header appears
109 * @return pointer to the nested message, does not copy the message
111 const struct GNUNET_MessageHeader *
112 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
117 * Implementation of the GNUNET_MQ_msg_nested_mh macro.
119 * @param mhp pointer to the message header pointer that will be changed to allocate at
120 * the newly allocated space for the message.
121 * @param base_size size of the data before the nested message
122 * @param type type of the message in the envelope
123 * @param nested_mh the message to append to the message after base_size
125 struct GNUNET_MQ_Envelope *
126 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
129 const struct GNUNET_MessageHeader *nested_mh);
134 * End-marker for the handlers array
136 #define GNUNET_MQ_HANDLERS_END {NULL, 0, 0}
140 * Opaque handle to a message queue.
142 struct GNUNET_MQ_Handle;
145 * Opaque handle to an envelope.
147 struct GNUNET_MQ_Envelope;
151 * Error codes for the queue.
158 GNUNET_MQ_ERROR_READ = 1,
163 GNUNET_MQ_ERROR_WRITE = 2,
168 GNUNET_MQ_ERROR_TIMEOUT = 4
173 * Called when a message has been received.
176 * @param msg the received message
179 (*GNUNET_MQ_MessageCallback) (void *cls,
180 const struct GNUNET_MessageHeader *msg);
184 * Signature of functions implementing the
185 * sending functionality of a message queue.
187 * @param mq the message queue
188 * @param msg the message to send
189 * @param impl_state state of the implementation
192 (*GNUNET_MQ_SendImpl) (struct GNUNET_MQ_Handle *mq,
193 const struct GNUNET_MessageHeader *msg,
198 * Signature of functions implementing the
199 * destruction of a message queue.
200 * Implementations must not free @a mq, but should
201 * take care of @a impl_state.
203 * @param mq the message queue to destroy
204 * @param impl_state state of the implementation
207 (*GNUNET_MQ_DestroyImpl) (struct GNUNET_MQ_Handle *mq,
212 * Implementation function that cancels the currently sent message.
214 * @param mq message queue
215 * @param impl_state state specific to the implementation
218 (*GNUNET_MQ_CancelImpl) (struct GNUNET_MQ_Handle *mq,
223 * Callback used for notifications
228 (*GNUNET_MQ_NotifyCallback) (void *cls);
232 * Generic error handler, called with the appropriate
233 * error code and the same closure specified at the creation of
235 * Not every message queue implementation supports an error handler.
237 * @param cls closure, same closure as for the message handlers
238 * @param error error code
241 (*GNUNET_MQ_ErrorHandler) (void *cls,
242 enum GNUNET_MQ_Error error);
246 * Message handler for a specific message type.
248 struct GNUNET_MQ_MessageHandler
251 * Callback, called every time a new message of
252 * the specified type has been receied.
254 GNUNET_MQ_MessageCallback cb;
257 * Type of the message this handler covers.
262 * Expected size of messages of this type. Use 0 for
263 * variable-size. If non-zero, messages of the given
264 * type will be discarded (and the connection closed)
265 * if they do not have the right size.
267 uint16_t expected_size;
273 * Create a new envelope.
275 * @param mhp message header to store the allocated message header in, can be NULL
276 * @param size size of the message to allocate
277 * @param type type of the message, will be set in the allocated message
278 * @return the allocated MQ message
280 struct GNUNET_MQ_Envelope *
281 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
287 * Discard the message queue message, free all
288 * allocated resources. Must be called in the event
289 * that a message is created but should not actually be sent.
291 * @param mqm the message to discard
294 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm);
298 * Send a message with the give message queue.
299 * May only be called once per message.
301 * @param mq message queue
302 * @param ev the envelope with the message to send.
305 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
306 struct GNUNET_MQ_Envelope *ev);
310 * Cancel sending the message. Message must have been sent with
311 * #GNUNET_MQ_send before. May not be called after the notify sent
312 * callback has been called
314 * @param ev queued envelope to cancel
317 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev);
321 * Associate the assoc_data in mq with a unique request id.
323 * @param mq message queue, id will be unique for the queue
324 * @param assoc_data to associate
327 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq, void *assoc_data);
331 * Get the data associated with a request id in a queue
333 * @param mq the message queue with the association
334 * @param request_id the request id we are interested in
335 * @return the associated data
338 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
339 uint32_t request_id);
343 * Remove the association for a request id
345 * @param mq the message queue with the association
346 * @param request_id the request id we want to remove
347 * @return the associated data
350 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
351 uint32_t request_id);
355 * Create a message queue for a GNUNET_CLIENT_Connection.
356 * If handlers are specfied, receive messages from the connection.
358 * @param connection the client connection
359 * @param handlers handlers for receiving messages
360 * @param error_handler error handler
361 * @param cls closure for the handlers
362 * @return the message queue
364 struct GNUNET_MQ_Handle *
365 GNUNET_MQ_queue_for_connection_client (struct GNUNET_CLIENT_Connection *connection,
366 const struct GNUNET_MQ_MessageHandler *handlers,
367 GNUNET_MQ_ErrorHandler error_handler,
372 * Create a message queue for a GNUNET_SERVER_Client.
374 * @param client the client
375 * @return the message queue
377 struct GNUNET_MQ_Handle *
378 GNUNET_MQ_queue_for_server_client (struct GNUNET_SERVER_Client *client);
382 * Create a message queue for the specified handlers.
384 * @param send function the implements sending messages
385 * @param destroy function that implements destroying the queue
386 * @param cancel function that implements canceling a message
387 * @param impl_state for the queue, passed to @a send, @a destroy and @a cancel
388 * @param handlers array of message handlers
389 * @param error_handler handler for read and write errors
390 * @param cls closure for message handlers and error handler
391 * @return a new message queue
393 struct GNUNET_MQ_Handle *
394 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
395 GNUNET_MQ_DestroyImpl destroy,
396 GNUNET_MQ_CancelImpl cancel,
398 const struct GNUNET_MQ_MessageHandler *handlers,
399 GNUNET_MQ_ErrorHandler error_handler,
404 * Replace the handlers of a message queue with new handlers. Takes
405 * effect immediately, even for messages that already have been
406 * received, but for with the handler has not been called.
408 * If the message queue does not support receiving messages,
409 * this function has no effect.
411 * @param mq message queue
412 * @param new_handlers new handlers
413 * @param cls new closure for the handlers
416 GNUNET_MQ_replace_handlers (struct GNUNET_MQ_Handle *mq,
417 const struct GNUNET_MQ_MessageHandler *new_handlers,
422 * Call a callback once the envelope has been sent, that is,
423 * sending it can not be canceled anymore.
424 * There can be only one notify sent callback per envelope.
426 * @param ev message to call the notify callback for
427 * @param cb the notify callback
428 * @param cls closure for the callback
431 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
432 GNUNET_MQ_NotifyCallback cb,
437 * Destroy the message queue.
439 * @param mq message queue to destroy
442 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq);
446 * Call the message message handler that was registered
447 * for the type of the given message in the given message queue.
449 * This function is indended to be used for the implementation
452 * @param mq message queue with the handlers
453 * @param mh message to dispatch
456 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
457 const struct GNUNET_MessageHeader *mh);
461 * Call the error handler of a message queue with the given
462 * error code. If there is no error handler, log a warning.
464 * This function is intended to be used for the implementation
467 * @param mq message queue
468 * @param error the error type
471 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
472 enum GNUNET_MQ_Error error);
476 * Call the send implementation for the next queued message,
478 * Only useful for implementing message queues,
479 * results in undefined behavior if not used carefully.
481 * @param mq message queue to send the next message with
484 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq);
488 * Get the message that should currently be sent.
489 * The returned message is only valid until #GNUNET_MQ_impl_send_continue
491 * Fails if there is no current message.
492 * Only useful for implementing message queues,
493 * results in undefined behavior if not used carefully.
495 * @param mq message queue with the current message, only valid
496 * until #GNUNET_MQ_impl_send_continue is called
497 * @return message to send, never NULL
499 const struct GNUNET_MessageHeader *
500 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq);
504 * Get the implementation state associated with the
507 * While the GNUNET_MQ_Impl* callbacks receive the
508 * implementation state, continuations that are scheduled
509 * by the implementation function often only have one closure
510 * argument, with this function it is possible to get at the
511 * implementation state when only passing the GNUNET_MQ_Handle
514 * @param mq message queue with the current message
515 * @return message to send, never NULL
518 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq);
523 /** @} */ /* end of group mq */