2 This file is part of GNUnet.
3 Copyright (C) 2012-2016 GNUnet e.V.
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
23 * @author Christian Grothoff
26 * General-purpose message queue
28 * @defgroup mq MQ library
29 * General-purpose message queue
31 * @see [Documentation](https://gnunet.org/message-queue-api)
40 * Allocate an envelope, with extra space allocated after the space needed
41 * by the message struct.
42 * The allocated message will already have the type and size field set.
44 * @param mvar variable to store the allocated message in;
45 * must have a header field;
47 * @param esize extra space to allocate after the message
48 * @param type type of the message
49 * @return the MQ message
51 #define GNUNET_MQ_msg_extra(mvar, esize, type) GNUNET_MQ_msg_(((struct GNUNET_MessageHeader**) &(mvar)), (esize) + sizeof *(mvar), (type))
54 * Allocate a GNUNET_MQ_Envelope.
55 * The contained message will already have the type and size field set.
57 * @param mvar variable to store the allocated message in;
58 * must have a header field;
60 * @param type type of the message
61 * @return the allocated envelope
63 #define GNUNET_MQ_msg(mvar, type) GNUNET_MQ_msg_extra(mvar, 0, type)
67 * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header.
68 * The allocated message will already have the type and size field set.
70 * @param type type of the message
72 #define GNUNET_MQ_msg_header(type) GNUNET_MQ_msg_ (NULL, sizeof (struct GNUNET_MessageHeader), type)
76 * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header and extra space.
77 * The allocated message will already have the type and size field set.
79 * @param mh pointer that will changed to point at to the allocated message header
80 * @param esize extra space to allocate after the message header
81 * @param type type of the message
83 #define GNUNET_MQ_msg_header_extra(mh, esize, type) GNUNET_MQ_msg_ (&mh, (esize) + sizeof (struct GNUNET_MessageHeader), type)
87 * Allocate a GNUNET_MQ_Envelope, and append a payload message after the given
90 * @param mvar pointer to a message struct, will be changed to point at the newly allocated message,
91 * whose size is 'sizeof(*mvar) + ntohs (mh->size)'
92 * @param type message type of the allocated message, has no effect on the nested message
93 * @param mh message to nest
94 * @return a newly allocated 'struct GNUNET_MQ_Envelope *'
96 #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)
100 * Return a pointer to the message at the end of the given message.
102 * @param var pointer to a message struct, the type of the expression determines the base size,
103 * the space after the base size is the nested message
104 * @return a 'struct GNUNET_MessageHeader *' that points at the nested message of the given message,
105 * or NULL if the given message in @a var does not have any space after the message struct
107 #define GNUNET_MQ_extract_nested_mh(var) GNUNET_MQ_extract_nested_mh_ ((struct GNUNET_MessageHeader *) (var), sizeof (*(var)))
111 * Implementation of the #GNUNET_MQ_extract_nexted_mh macro.
113 * @param mh message header to extract nested message header from
114 * @param base_size size of the message before the nested message's header appears
115 * @return pointer to the nested message, does not copy the message
116 * OR NULL in case of a malformed message.
118 const struct GNUNET_MessageHeader *
119 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
124 * Opaque handle to an envelope.
126 struct GNUNET_MQ_Envelope;
130 * Implementation of the #GNUNET_MQ_msg_nested_mh macro.
132 * @param mhp pointer to the message header pointer that will be changed to allocate at
133 * the newly allocated space for the message.
134 * @param base_size size of the data before the nested message
135 * @param type type of the message in the envelope
136 * @param nested_mh the message to append to the message after base_size
138 struct GNUNET_MQ_Envelope *
139 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
142 const struct GNUNET_MessageHeader *nested_mh);
146 * Opaque handle to a message queue.
148 struct GNUNET_MQ_Handle;
152 * Error codes for the queue.
157 * Failed to read message from the network.
158 * FIXME: Likely not properly distinguished
159 * from TIMEOUT case in the code!
161 GNUNET_MQ_ERROR_READ = 1,
166 GNUNET_MQ_ERROR_WRITE = 2,
171 GNUNET_MQ_ERROR_TIMEOUT = 4,
174 * We received a message that was malformed and thus
175 * could not be passed to its handler.
177 GNUNET_MQ_ERROR_MALFORMED = 8,
180 * We received a message for which we have no matching
183 GNUNET_MQ_ERROR_NO_MATCH = 16
188 * Called when a message has been received.
191 * @param msg the received message
194 (*GNUNET_MQ_MessageCallback) (void *cls,
195 const struct GNUNET_MessageHeader *msg);
199 * Called when a message needs to be validated.
202 * @param msg the received message
203 * @return #GNUNET_OK if the message is well-formed,
204 * #GNUNET_SYSERR if not
207 (*GNUNET_MQ_MessageValidationCallback) (void *cls,
208 const struct GNUNET_MessageHeader *msg);
212 * Signature of functions implementing the
213 * sending functionality of a message queue.
215 * @param mq the message queue
216 * @param msg the message to send
217 * @param impl_state state of the implementation
220 (*GNUNET_MQ_SendImpl) (struct GNUNET_MQ_Handle *mq,
221 const struct GNUNET_MessageHeader *msg,
226 * Signature of functions implementing the
227 * destruction of a message queue.
228 * Implementations must not free @a mq, but should
229 * take care of @a impl_state.
231 * @param mq the message queue to destroy
232 * @param impl_state state of the implementation
235 (*GNUNET_MQ_DestroyImpl) (struct GNUNET_MQ_Handle *mq,
240 * Implementation function that cancels the currently sent message.
242 * @param mq message queue
243 * @param impl_state state specific to the implementation
246 (*GNUNET_MQ_CancelImpl) (struct GNUNET_MQ_Handle *mq,
251 * Callback used for notifications
256 (*GNUNET_MQ_NotifyCallback) (void *cls);
260 * Generic error handler, called with the appropriate
261 * error code and the same closure specified at the creation of
263 * Not every message queue implementation supports an error handler.
266 * @param error error code
269 (*GNUNET_MQ_ErrorHandler) (void *cls,
270 enum GNUNET_MQ_Error error);
274 * Message handler for a specific message type.
276 struct GNUNET_MQ_MessageHandler
279 * Callback to validate a message of the specified @e type.
280 * The closure given to @e mv will be this struct (not @e ctx).
281 * Using NULL means only size-validation using
282 * @e expected_size. In this case, @e expected_size must
285 GNUNET_MQ_MessageValidationCallback mv;
288 * Callback, called every time a new message of
289 * the specified @e type has been receied.
290 * The closure given to @e mv will be this struct (not @e ctx).
292 GNUNET_MQ_MessageCallback cb;
295 * Closure for @e mv and @e cb.
300 * Type of the message this handler covers, in host byte order.
305 * Expected size of messages of this type. Minimum size of the
306 * message if @e mv is non-NULL. Messages of the given type will be
307 * discarded (and the connection closed with an error reported to
308 * the application) if they do not have the right size.
310 uint16_t expected_size;
315 * End-marker for the handlers array
317 #define GNUNET_MQ_handler_end() { NULL, NULL, NULL, 0, 0 }
321 * Defines a static function @a name which takes as a single argument
322 * a message handler for fixed-sized messages of type @a code and with
323 * a message type argument of @a str. Given such an argument, the
324 * function @name will return a `struct GNUNET_MQ_MessageHandler`
325 * for the given message type.
327 * The macro is to be used as follows:
329 * struct GNUNET_MessageTest { ... }; // must be fixed size
331 * handle_test_message (void *cls,
332 * const struct GNUNET_MessageTest *msg)
335 * struct GNUNET_MQ_MessageHandler handlers[] = {
336 * GNUNET_MQ_hd_fixed_size(test_message,
337 * GNUNET_MESSAGE_TYPE_TEST,
338 * struct GNUNET_MessageTest,
340 * GNUNET_MQ_handler_end()
343 * @param name unique basename for the functions
344 * @param code message type constant
345 * @param str type of the message (a struct)
346 * @param ctx context for the callbacks
348 #define GNUNET_MQ_hd_fixed_size(name,code,str,ctx) \
350 void (*_cb)(void *cls, const str *msg) = &handle_##name; \
351 ((struct GNUNET_MQ_MessageHandler) { \
352 NULL, (GNUNET_MQ_MessageCallback) _cb, \
353 (ctx), (code), sizeof (str) }); \
358 * Defines a static function @a name which takes two arguments and a
359 * context-pointer for validating and handling variable-sized messages
360 * of type @a code and with a message type argument of @a str. Given
361 * such arguments, the function @name will return a `struct
362 * GNUNET_MQ_MessageHandler` for the given message type.
364 * The macro is to be used as follows:
366 * struct GNUNET_MessageTest { ... }; // can be variable size
368 * check_test (void *cls,
369 * const struct GNUNET_MessageTest *msg)
371 * const char *ctx = cls;
372 * GNUNET_assert (0 == strcmp ("context", ctx));
376 * handle_test (void *cls,
377 * const struct GNUNET_MessageTest *msg)
379 * const char *ctx = cls;
380 * GNUNET_assert (0 == strcmp ("context", ctx));
384 * struct GNUNET_MQ_MessageHandler handlers[] = {
385 * GNUNET_MQ_hd_var_size(test_message,
386 * GNUNET_MESSAGE_TYPE_TEST,
387 * struct GNUNET_MessageTest,
389 * GNUNET_MQ_handler_end()
392 * @param name unique basename for the functions
393 * @param code message type constant
394 * @param str type of the message (a struct)
395 * @param ctx context for the callbacks
397 #define GNUNET_MQ_hd_var_size(name,code,str,ctx) \
399 int (*_mv)(void *cls, const str *msg) = &check_##name; \
400 void (*_cb)(void *cls, const str *msg) = &handle_##name; \
401 ((struct GNUNET_MQ_MessageHandler) \
402 { (GNUNET_MQ_MessageValidationCallback) _mv, \
403 (GNUNET_MQ_MessageCallback) _cb, \
404 (ctx), (code), sizeof (str) }); \
409 * Create a new envelope.
411 * @param mhp message header to store the allocated message header in, can be NULL
412 * @param size size of the message to allocate
413 * @param type type of the message, will be set in the allocated message
414 * @return the allocated MQ message
416 struct GNUNET_MQ_Envelope *
417 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
424 * Create a new envelope by copying an existing message.
426 * @param hdr header of the message to copy
427 * @return envelope containing @a hdr
429 struct GNUNET_MQ_Envelope *
430 GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr);
434 * Discard the message queue message, free all
435 * allocated resources. Must be called in the event
436 * that a message is created but should not actually be sent.
438 * @param mqm the message to discard
441 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm);
445 * Function to obtain the current envelope
446 * from within #GNUNET_MQ_SendImpl implementations.
448 * @param mq message queue to interrogate
449 * @return the current envelope
451 struct GNUNET_MQ_Envelope *
452 GNUNET_MQ_get_current_envelope (struct GNUNET_MQ_Handle *mq);
456 * Function to obtain the last envelope in the queue.
458 * @param mq message queue to interrogate
459 * @return the last envelope in the queue
461 struct GNUNET_MQ_Envelope *
462 GNUNET_MQ_get_last_envelope (struct GNUNET_MQ_Handle *mq);
466 * Set application-specific options for this envelope.
467 * Overrides the options set for the queue with
468 * #GNUNET_MQ_set_options() for this message only.
470 * @param env message to set options for
471 * @param flags flags to use (meaning is queue-specific)
472 * @param extra additional buffer for further data (also queue-specific)
475 GNUNET_MQ_env_set_options (struct GNUNET_MQ_Envelope *env,
481 * Get application-specific options for this envelope.
483 * @param env message to set options for
484 * @param[out] flags set to flags to use (meaning is queue-specific)
485 * @return extra additional buffer for further data (also queue-specific)
488 GNUNET_MQ_env_get_options (struct GNUNET_MQ_Envelope *env,
493 * Set application-specific options for this queue.
495 * @param mq message queue to set options for
496 * @param flags flags to use (meaning is queue-specific)
497 * @param extra additional buffer for further data (also queue-specific)
500 GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq,
506 * Obtain the current length of the message queue.
508 * @param mq queue to inspect
509 * @return number of queued, non-transmitted messages
512 GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq);
516 * Send a message with the given message queue.
517 * May only be called once per message.
519 * @param mq message queue
520 * @param ev the envelope with the message to send.
523 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
524 struct GNUNET_MQ_Envelope *ev);
528 * Send a copy of a message with the given message queue.
529 * Can be called repeatedly on the same envelope.
531 * @param mq message queue
532 * @param ev the envelope with the message to send.
535 GNUNET_MQ_send_copy (struct GNUNET_MQ_Handle *mq,
536 const struct GNUNET_MQ_Envelope *ev);
540 * Cancel sending the message. Message must have been sent with
541 * #GNUNET_MQ_send before. May not be called after the notify sent
542 * callback has been called
544 * @param ev queued envelope to cancel
547 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev);
551 * Associate the assoc_data in @a mq with a unique request id.
553 * @param mq message queue, id will be unique for the queue
554 * @param assoc_data to associate
557 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
562 * Get the data associated with a @a request_id in a queue
564 * @param mq the message queue with the association
565 * @param request_id the request id we are interested in
566 * @return the associated data
569 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
570 uint32_t request_id);
574 * Remove the association for a @a request_id
576 * @param mq the message queue with the association
577 * @param request_id the request id we want to remove
578 * @return the associated data
581 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
582 uint32_t request_id);
586 * Create a message queue for the specified handlers.
588 * @param send function the implements sending messages
589 * @param destroy function that implements destroying the queue
590 * @param cancel function that implements canceling a message
591 * @param impl_state for the queue, passed to @a send, @a destroy and @a cancel
592 * @param handlers array of message handlers
593 * @param error_handler handler for read and write errors
594 * @param cls closure for message handlers and error handler
595 * @return a new message queue
597 struct GNUNET_MQ_Handle *
598 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
599 GNUNET_MQ_DestroyImpl destroy,
600 GNUNET_MQ_CancelImpl cancel,
602 const struct GNUNET_MQ_MessageHandler *handlers,
603 GNUNET_MQ_ErrorHandler error_handler,
608 * Change the closure argument in all of the `handlers` of the
611 * @param mq to modify
612 * @param handlers_cls new closure to use
615 GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq,
620 * Call a callback once the envelope has been sent, that is,
621 * sending it can not be canceled anymore.
622 * There can be only one notify sent callback per envelope.
624 * @param ev message to call the notify callback for
625 * @param cb the notify callback
626 * @param cb_cls closure for the callback
629 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
630 GNUNET_MQ_NotifyCallback cb,
635 * Destroy the message queue.
637 * @param mq message queue to destroy
640 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq);
644 * Handle we return for callbacks registered to be
645 * notified when #GNUNET_MQ_destroy() is called on a queue.
647 struct GNUNET_MQ_DestroyNotificationHandle;
651 * Register function to be called whenever @a mq is being
654 * @param mq message queue to watch
655 * @param cb function to call on @a mq destruction
656 * @param cb_cls closure for @a cb
657 * @return handle for #GNUNET_MQ_destroy_notify_cancel().
659 struct GNUNET_MQ_DestroyNotificationHandle *
660 GNUNET_MQ_destroy_notify (struct GNUNET_MQ_Handle *mq,
661 GNUNET_SCHEDULER_TaskCallback cb,
665 * Cancel registration from #GNUNET_MQ_destroy_notify().
667 * @param dnh handle for registration to cancel
670 GNUNET_MQ_destroy_notify_cancel (struct GNUNET_MQ_DestroyNotificationHandle *dnh);
674 * Call the message message handler that was registered
675 * for the type of the given message in the given message queue.
677 * This function is indended to be used for the implementation
680 * @param mq message queue with the handlers
681 * @param mh message to dispatch
684 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
685 const struct GNUNET_MessageHeader *mh);
689 * Call the error handler of a message queue with the given
690 * error code. If there is no error handler, log a warning.
692 * This function is intended to be used for the implementation
695 * @param mq message queue
696 * @param error the error type
699 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
700 enum GNUNET_MQ_Error error);
704 * Call the send implementation for the next queued message, if any.
705 * Calls the send notification for the current message unless
706 * #GNUNET_MQ_impl_send_in_flight was called for this envelope.
708 * Only useful for implementing message queues, results in undefined
709 * behavior if not used carefully.
711 * @param mq message queue to send the next message with
714 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq);
718 * Call the send notification for the current message, but do not
719 * try to send the next message until #gnunet_mq_impl_send_continue
722 * only useful for implementing message queues, results in undefined
723 * behavior if not used carefully.
725 * @param mq message queue to send the next message with
728 GNUNET_MQ_impl_send_in_flight (struct GNUNET_MQ_Handle *mq);
732 * Get the implementation state associated with the
735 * While the GNUNET_MQ_Impl* callbacks receive the
736 * implementation state, continuations that are scheduled
737 * by the implementation function often only have one closure
738 * argument, with this function it is possible to get at the
739 * implementation state when only passing the `struct GNUNET_MQ_Handle`
742 * @param mq message queue with the current message
743 * @return message to send, never NULL
746 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq);
750 * Get the message that should currently be sent.
751 * Fails if there is no current message.
752 * Only useful for implementing message queues,
753 * results in undefined behavior if not used carefully.
755 * @param mq message queue with the current message
756 * @return message to send, never NULL
758 const struct GNUNET_MessageHeader *
759 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq);
764 /** @} */ /* end of group mq */