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
46 * @param esize extra space to allocate after the message
47 * @param type type of the message
48 * @return the MQ message
50 #define GNUNET_MQ_msg_extra(mvar, esize, type) GNUNET_MQ_msg_(((struct GNUNET_MessageHeader**) &(mvar)), (esize) + sizeof *(mvar), (type))
53 * Allocate a GNUNET_MQ_Envelope.
54 * The contained message will already have the type and size field set.
56 * @param mvar variable to store the allocated message in;
57 * must have a header field
58 * @param type type of the message
59 * @return the allocated envelope
61 #define GNUNET_MQ_msg(mvar, type) GNUNET_MQ_msg_extra(mvar, 0, type)
65 * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header.
66 * The allocated message will already have the type and size field set.
68 * @param type type of the message
70 #define GNUNET_MQ_msg_header(type) GNUNET_MQ_msg_ (NULL, sizeof (struct GNUNET_MessageHeader), type)
74 * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header and extra space.
75 * The allocated message will already have the type and size field set.
77 * @param mh pointer that will changed to point at to the allocated message header
78 * @param esize extra space to allocate after the message header
79 * @param type type of the message
81 #define GNUNET_MQ_msg_header_extra(mh, esize, type) GNUNET_MQ_msg_ (&mh, (esize) + sizeof (struct GNUNET_MessageHeader), type)
85 * Allocate a GNUNET_MQ_Envelope, and append a payload message after the given
88 * @param mvar pointer to a message struct, will be changed to point at the newly allocated message,
89 * whose size is 'sizeof(*mvar) + ntohs (mh->size)'
90 * @param type message type of the allocated message, has no effect on the nested message
91 * @param mh message to nest
92 * @return a newly allocated 'struct GNUNET_MQ_Envelope *'
94 #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)
98 * Return a pointer to the message at the end of the given message.
100 * @param var pointer to a message struct, the type of the expression determines the base size,
101 * the space after the base size is the nested message
102 * @return a 'struct GNUNET_MessageHeader *' that points at the nested message of the given message,
103 * or NULL if the given message in @a var does not have any space after the message struct
105 #define GNUNET_MQ_extract_nested_mh(var) GNUNET_MQ_extract_nested_mh_ ((struct GNUNET_MessageHeader *) (var), sizeof (*(var)))
109 * Implementation of the #GNUNET_MQ_extract_nexted_mh macro.
111 * @param mh message header to extract nested message header from
112 * @param base_size size of the message before the nested message's header appears
113 * @return pointer to the nested message, does not copy the message
115 const struct GNUNET_MessageHeader *
116 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
121 * Opaque handle to an envelope.
123 struct GNUNET_MQ_Envelope;
127 * Implementation of the #GNUNET_MQ_msg_nested_mh macro.
129 * @param mhp pointer to the message header pointer that will be changed to allocate at
130 * the newly allocated space for the message.
131 * @param base_size size of the data before the nested message
132 * @param type type of the message in the envelope
133 * @param nested_mh the message to append to the message after base_size
135 struct GNUNET_MQ_Envelope *
136 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
139 const struct GNUNET_MessageHeader *nested_mh);
143 * Opaque handle to a message queue.
145 struct GNUNET_MQ_Handle;
149 * Error codes for the queue.
154 * Failed to read message from the network.
155 * FIXME: Likely not properly distinguished
156 * from TIMEOUT case in the code!
158 GNUNET_MQ_ERROR_READ = 1,
163 GNUNET_MQ_ERROR_WRITE = 2,
168 GNUNET_MQ_ERROR_TIMEOUT = 4,
171 * We received a message that was malformed and thus
172 * could not be passed to its handler.
174 GNUNET_MQ_ERROR_MALFORMED = 8
179 * Called when a message has been received.
182 * @param msg the received message
185 (*GNUNET_MQ_MessageCallback) (void *cls,
186 const struct GNUNET_MessageHeader *msg);
190 * Called when a message needs to be validated.
193 * @param msg the received message
194 * @return #GNUNET_OK if the message is well-formed,
195 * #GNUNET_SYSERR if not
198 (*GNUNET_MQ_MessageValidationCallback) (void *cls,
199 const struct GNUNET_MessageHeader *msg);
203 * Signature of functions implementing the
204 * sending functionality of a message queue.
206 * @param mq the message queue
207 * @param msg the message to send
208 * @param impl_state state of the implementation
211 (*GNUNET_MQ_SendImpl) (struct GNUNET_MQ_Handle *mq,
212 const struct GNUNET_MessageHeader *msg,
217 * Signature of functions implementing the
218 * destruction of a message queue.
219 * Implementations must not free @a mq, but should
220 * take care of @a impl_state.
222 * @param mq the message queue to destroy
223 * @param impl_state state of the implementation
226 (*GNUNET_MQ_DestroyImpl) (struct GNUNET_MQ_Handle *mq,
231 * Implementation function that cancels the currently sent message.
233 * @param mq message queue
234 * @param impl_state state specific to the implementation
237 (*GNUNET_MQ_CancelImpl) (struct GNUNET_MQ_Handle *mq,
242 * Callback used for notifications
247 (*GNUNET_MQ_NotifyCallback) (void *cls);
251 * Generic error handler, called with the appropriate
252 * error code and the same closure specified at the creation of
254 * Not every message queue implementation supports an error handler.
257 * @param error error code
260 (*GNUNET_MQ_ErrorHandler) (void *cls,
261 enum GNUNET_MQ_Error error);
265 * Message handler for a specific message type.
267 struct GNUNET_MQ_MessageHandler
270 * Callback to validate a message of the specified @e type.
271 * The closure given to @e mv will be this struct (not @e ctx).
272 * Using NULL means only size-validation using
273 * @e expected_size. In this case, @e expected_size must
276 GNUNET_MQ_MessageValidationCallback mv;
279 * Callback, called every time a new message of
280 * the specified @e type has been receied.
281 * The closure given to @e mv will be this struct (not @e ctx).
283 GNUNET_MQ_MessageCallback cb;
286 * Closure for @e mv and @e cb.
291 * Type of the message this handler covers, in host byte order.
296 * Expected size of messages of this type. Minimum size of the
297 * message if @e mv is non-NULL. Messages of the given type will be
298 * discarded (and the connection closed with an error reported to
299 * the application) if they do not have the right size.
301 uint16_t expected_size;
306 * End-marker for the handlers array
308 #define GNUNET_MQ_handler_end() { NULL, NULL, NULL, 0, 0 }
312 * Defines a static function @a name which takes as a single argument
313 * a message handler for fixed-sized messages of type @a code and with
314 * a message type argument of @a str. Given such an argument, the
315 * function @name will return a `struct GNUNET_MQ_MessageHandler`
316 * for the given message type.
318 * The macro is to be used as follows:
320 * struct GNUNET_MessageTest { ... }; // must be fixed size
322 * handle_test_message (void *cls,
323 * const struct GNUNET_MessageTest *msg)
326 * GNUNET_MQ_hd_fixed_size(test_message,
327 * GNUNET_MESSAGE_TYPE_TEST,
328 * struct GNUNET_MessageTest);
329 * struct GNUNET_MQ_MessageHandler handlers[] = {
330 * make_test_message_handler (),
331 * GNUNET_MQ_handler_end()
334 * @param name unique basename for the functions
335 * @param code message type constant
336 * @param str type of the message (a struct)
338 #define GNUNET_MQ_hd_fixed_size(name,code,str) \
339 struct GNUNET_MQ_MessageHandler \
340 make_##name##_handler (void *cls) { \
341 void (*cb)(void *cls, const str *msg) = &handle_##name; \
342 struct GNUNET_MQ_MessageHandler mh = { \
343 NULL, (GNUNET_MQ_MessageCallback) cb, \
344 cls, code, sizeof (str) }; \
350 * Defines a static function @a name which takes two arguments and a
351 * context-pointer for validating and handling variable-sized messages
352 * of type @a code and with a message type argument of @a str. Given
353 * such arguments, the function @name will return a `struct
354 * GNUNET_MQ_MessageHandler` for the given message type.
356 * The macro is to be used as follows:
358 * struct GNUNET_MessageTest { ... }; // can be variable size
359 * GNUNET_MQ_hd_var_size(test_message,
360 * GNUNET_MESSAGE_TYPE_TEST,
361 * struct GNUNET_MessageTest);
363 * check_test (void *cls,
364 * const struct GNUNET_MessageTest *msg)
366 * const char *ctx = cls;
367 * GNUNET_assert (0 == strcmp ("context", ctx));
371 * handle_test (void *cls,
372 * const struct GNUNET_MessageTest *msg)
374 * const char *ctx = cls;
375 * GNUNET_assert (0 == strcmp ("context", ctx));
379 * struct GNUNET_MQ_MessageHandler handlers[] = {
380 * make_test_message_handler ("context"),
381 * GNUNET_MQ_handler_end()
384 * @param name unique basename for the functions
385 * @param code message type constant
386 * @param str type of the message (a struct)
388 #define GNUNET_MQ_hd_var_size(name,code,str) \
389 struct GNUNET_MQ_MessageHandler \
390 make_##name##_handler (void *ctx) { \
391 int (*mv)(void *cls, const str *msg) = &check_##name; \
392 void (*cb)(void *cls, const str *msg) = &handle_##name;\
393 struct GNUNET_MQ_MessageHandler mh = \
394 { (GNUNET_MQ_MessageValidationCallback) mv, \
395 (GNUNET_MQ_MessageCallback) cb, \
396 ctx, code, sizeof (str) }; \
402 * Create a new envelope.
404 * @param mhp message header to store the allocated message header in, can be NULL
405 * @param size size of the message to allocate
406 * @param type type of the message, will be set in the allocated message
407 * @return the allocated MQ message
409 struct GNUNET_MQ_Envelope *
410 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
416 * Create a new envelope by copying an existing message.
418 * @param hdr header of the message to copy
419 * @return envelope containing @a hdr
421 struct GNUNET_MQ_Envelope *
422 GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr);
426 * Discard the message queue message, free all
427 * allocated resources. Must be called in the event
428 * that a message is created but should not actually be sent.
430 * @param mqm the message to discard
433 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm);
437 * Function to obtain the current envelope from
438 * within #GNUNET_MQ_SendImpl implementations.
440 * @param mq message queue to interrogate
441 * @return the current envelope
443 struct GNUNET_MQ_Envelope *
444 GNUNET_MQ_get_current_envelope (struct GNUNET_MQ_Handle *mq);
448 * Set application-specific options for this envelope.
449 * Overrides the options set for the queue with
450 * #GNUNET_MQ_set_options() for this message only.
452 * @param env message to set options for
453 * @param flags flags to use (meaning is queue-specific)
454 * @param extra additional buffer for further data (also queue-specific)
457 GNUNET_MQ_env_set_options (struct GNUNET_MQ_Envelope *env,
463 * Get application-specific options for this envelope.
465 * @param env message to set options for
466 * @param[out] flags set to flags to use (meaning is queue-specific)
467 * @return extra additional buffer for further data (also queue-specific)
470 GNUNET_MQ_env_get_options (struct GNUNET_MQ_Envelope *env,
475 * Set application-specific options for this queue.
477 * @param mq message queue to set options for
478 * @param flags flags to use (meaning is queue-specific)
479 * @param extra additional buffer for further data (also queue-specific)
482 GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq,
488 * Obtain the current length of the message queue.
490 * @param mq queue to inspect
491 * @return number of queued, non-transmitted messages
494 GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq);
498 * Send a message with the given message queue.
499 * May only be called once per message.
501 * @param mq message queue
502 * @param ev the envelope with the message to send.
505 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
506 struct GNUNET_MQ_Envelope *ev);
510 * Send a copy of a message with the given message queue.
511 * Can be called repeatedly on the same envelope.
513 * @param mq message queue
514 * @param ev the envelope with the message to send.
517 GNUNET_MQ_send_copy (struct GNUNET_MQ_Handle *mq,
518 const struct GNUNET_MQ_Envelope *ev);
522 * Cancel sending the message. Message must have been sent with
523 * #GNUNET_MQ_send before. May not be called after the notify sent
524 * callback has been called
526 * @param ev queued envelope to cancel
529 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev);
533 * Associate the assoc_data in @a mq with a unique request id.
535 * @param mq message queue, id will be unique for the queue
536 * @param assoc_data to associate
539 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
544 * Get the data associated with a @a request_id in a queue
546 * @param mq the message queue with the association
547 * @param request_id the request id we are interested in
548 * @return the associated data
551 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
552 uint32_t request_id);
556 * Remove the association for a @a request_id
558 * @param mq the message queue with the association
559 * @param request_id the request id we want to remove
560 * @return the associated data
563 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
564 uint32_t request_id);
568 * Create a message queue for the specified handlers.
570 * @param send function the implements sending messages
571 * @param destroy function that implements destroying the queue
572 * @param cancel function that implements canceling a message
573 * @param impl_state for the queue, passed to @a send, @a destroy and @a cancel
574 * @param handlers array of message handlers
575 * @param error_handler handler for read and write errors
576 * @param cls closure for message handlers and error handler
577 * @return a new message queue
579 struct GNUNET_MQ_Handle *
580 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
581 GNUNET_MQ_DestroyImpl destroy,
582 GNUNET_MQ_CancelImpl cancel,
584 const struct GNUNET_MQ_MessageHandler *handlers,
585 GNUNET_MQ_ErrorHandler error_handler,
590 * Change the closure argument in all of the `handlers` of the
593 * @param mq to modify
594 * @param handlers_cls new closure to use
597 GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq,
602 * Call a callback once the envelope has been sent, that is,
603 * sending it can not be canceled anymore.
604 * There can be only one notify sent callback per envelope.
606 * @param ev message to call the notify callback for
607 * @param cb the notify callback
608 * @param cls closure for the callback
611 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
612 GNUNET_MQ_NotifyCallback cb,
617 * Destroy the message queue.
619 * @param mq message queue to destroy
622 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq);
626 * Call the message message handler that was registered
627 * for the type of the given message in the given message queue.
629 * This function is indended to be used for the implementation
632 * @param mq message queue with the handlers
633 * @param mh message to dispatch
636 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
637 const struct GNUNET_MessageHeader *mh);
641 * Call the error handler of a message queue with the given
642 * error code. If there is no error handler, log a warning.
644 * This function is intended to be used for the implementation
647 * @param mq message queue
648 * @param error the error type
651 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
652 enum GNUNET_MQ_Error error);
656 * Call the send implementation for the next queued message, if any.
657 * Only useful for implementing message queues, results in undefined
658 * behavior if not used carefully.
660 * @param mq message queue to send the next message with
663 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq);
667 * Get the message that should currently be sent. The returned
668 * message is only valid until #GNUNET_MQ_impl_send_continue is
669 * called. Fails if there is no current message. Only useful for
670 * implementing message queues, results in undefined behavior if not
673 * @param mq message queue with the current message, only valid
674 * until #GNUNET_MQ_impl_send_continue() is called
675 * @return message to send, never NULL
677 const struct GNUNET_MessageHeader *
678 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq);
682 * Get the implementation state associated with the
685 * While the GNUNET_MQ_Impl* callbacks receive the
686 * implementation state, continuations that are scheduled
687 * by the implementation function often only have one closure
688 * argument, with this function it is possible to get at the
689 * implementation state when only passing the `struct GNUNET_MQ_Handle`
692 * @param mq message queue with the current message
693 * @return message to send, never NULL
696 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq);
701 /** @} */ /* end of group mq */