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 * Implementation of the #GNUNET_MQ_msg_nested_mh macro.
123 * @param mhp pointer to the message header pointer that will be changed to allocate at
124 * the newly allocated space for the message.
125 * @param base_size size of the data before the nested message
126 * @param type type of the message in the envelope
127 * @param nested_mh the message to append to the message after base_size
129 struct GNUNET_MQ_Envelope *
130 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
133 const struct GNUNET_MessageHeader *nested_mh);
137 * Opaque handle to a message queue.
139 struct GNUNET_MQ_Handle;
142 * Opaque handle to an envelope.
144 struct GNUNET_MQ_Envelope;
148 * Error codes for the queue.
153 * Failed to read message from the network.
154 * FIXME: Likely not properly distinguished
155 * from TIMEOUT case in the code!
157 GNUNET_MQ_ERROR_READ = 1,
162 GNUNET_MQ_ERROR_WRITE = 2,
167 GNUNET_MQ_ERROR_TIMEOUT = 4,
170 * We received a message that was malformed and thus
171 * could not be passed to its handler.
173 GNUNET_MQ_ERROR_MALFORMED = 8
178 * Called when a message has been received.
181 * @param msg the received message
184 (*GNUNET_MQ_MessageCallback) (void *cls,
185 const struct GNUNET_MessageHeader *msg);
189 * Called when a message needs to be validated.
192 * @param msg the received message
193 * @return #GNUNET_OK if the message is well-formed,
194 * #GNUNET_SYSERR if not
197 (*GNUNET_MQ_MessageValidationCallback) (void *cls,
198 const struct GNUNET_MessageHeader *msg);
202 * Signature of functions implementing the
203 * sending functionality of a message queue.
205 * @param mq the message queue
206 * @param msg the message to send
207 * @param impl_state state of the implementation
210 (*GNUNET_MQ_SendImpl) (struct GNUNET_MQ_Handle *mq,
211 const struct GNUNET_MessageHeader *msg,
216 * Signature of functions implementing the
217 * destruction of a message queue.
218 * Implementations must not free @a mq, but should
219 * take care of @a impl_state.
221 * @param mq the message queue to destroy
222 * @param impl_state state of the implementation
225 (*GNUNET_MQ_DestroyImpl) (struct GNUNET_MQ_Handle *mq,
230 * Implementation function that cancels the currently sent message.
232 * @param mq message queue
233 * @param impl_state state specific to the implementation
236 (*GNUNET_MQ_CancelImpl) (struct GNUNET_MQ_Handle *mq,
241 * Callback used for notifications
246 (*GNUNET_MQ_NotifyCallback) (void *cls);
250 * Generic error handler, called with the appropriate
251 * error code and the same closure specified at the creation of
253 * Not every message queue implementation supports an error handler.
256 * @param error error code
259 (*GNUNET_MQ_ErrorHandler) (void *cls,
260 enum GNUNET_MQ_Error error);
264 * Message handler for a specific message type.
266 struct GNUNET_MQ_MessageHandler
269 * Callback to validate a message of the specified @e type.
270 * The closure given to @e mv will be this struct (not @e ctx).
271 * Using NULL means only size-validation using
272 * @e expected_size. In this case, @e expected_size must
275 GNUNET_MQ_MessageValidationCallback mv;
278 * Callback, called every time a new message of
279 * the specified @e type has been receied.
280 * The closure given to @e mv will be this struct (not @e ctx).
282 GNUNET_MQ_MessageCallback cb;
285 * Closure for @e mv and @e cb.
290 * Type of the message this handler covers, in host byte order.
295 * Expected size of messages of this type. Minimum size of the
296 * message if @e mv is non-NULL. Messages of the given type will be
297 * discarded (and the connection closed with an error reported to
298 * the application) if they do not have the right size.
300 uint16_t expected_size;
305 * End-marker for the handlers array
307 #define GNUNET_MQ_handler_end() {NULL, NULL, NULL, 0, 0}
311 * Defines a static function @a name which takes as a single argument
312 * a message handler for fixed-sized messages of type @a code and with
313 * a message type argument of @a str. Given such an argument, the
314 * function @name will return a `struct GNUNET_MQ_MessageHandler`
315 * for the given message type.
317 * The macro is to be used as follows:
319 * struct GNUNET_MessageTest { ... }; // must be fixed size
321 * handle_test_message (void *cls,
322 * const struct GNUNET_MessageTest *msg)
325 * GNUNET_MQ_hd_fixed_size(test_message,
326 * GNUNET_MESSAGE_TYPE_TEST,
327 * struct GNUNET_MessageTest);
328 * struct GNUNET_MQ_MessageHandler handlers[] = {
329 * make_test_message_handler (),
330 * GNUNET_MQ_handler_end()
333 * @param name unique basename for the functions
334 * @param code message type constant
335 * @param str type of the message (a struct)
337 #define GNUNET_MQ_hd_fixed_size(name,code,str) \
338 struct GNUNET_MQ_MessageHandler \
339 make_##name##_handler (void *cls) { \
340 void (*cb)(void *cls, const str *msg) = &handle_##name; \
341 struct GNUNET_MQ_MessageHandler mh = { \
342 NULL, (GNUNET_MQ_MessageCallback) cb, \
343 cls, code, sizeof (str) }; \
349 * Defines a static function @a name which takes two arguments and a
350 * context-pointer for validating and handling variable-sized messages
351 * of type @a code and with a message type argument of @a str. Given
352 * such arguments, the function @name will return a `struct
353 * GNUNET_MQ_MessageHandler` for the given message type.
355 * The macro is to be used as follows:
357 * struct GNUNET_MessageTest { ... }; // can be variable size
358 * GNUNET_MQ_hd_var_size(test_message,
359 * GNUNET_MESSAGE_TYPE_TEST,
360 * struct GNUNET_MessageTest);
362 * check_test (void *cls,
363 * const struct GNUNET_MessageTest *msg)
365 * const char *ctx = cls;
366 * GNUNET_assert (0 == strcmp ("context", ctx));
370 * handle_test (void *cls,
371 * const struct GNUNET_MessageTest *msg)
373 * const char *ctx = cls;
374 * GNUNET_assert (0 == strcmp ("context", ctx));
378 * struct GNUNET_MQ_MessageHandler handlers[] = {
379 * make_test_message_handler ("context"),
380 * GNUNET_MQ_handler_end()
383 * @param name unique basename for the functions
384 * @param code message type constant
385 * @param str type of the message (a struct)
387 #define GNUNET_MQ_hd_var_size(name,code,str) \
388 struct GNUNET_MQ_MessageHandler \
389 make_##name##_handler (void *ctx) { \
390 int (*mv)(void *cls, const str *msg) = &check_##name; \
391 void (*cb)(void *cls, const str *msg) = &handle_##name;\
392 struct GNUNET_MQ_MessageHandler mh = \
393 { (GNUNET_MQ_MessageValidationCallback) mv, \
394 (GNUNET_MQ_MessageCallback) cb, \
395 ctx, code, sizeof (str) }; \
401 * Create a new envelope.
403 * @param mhp message header to store the allocated message header in, can be NULL
404 * @param size size of the message to allocate
405 * @param type type of the message, will be set in the allocated message
406 * @return the allocated MQ message
408 struct GNUNET_MQ_Envelope *
409 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
415 * Create a new envelope by copying an existing message.
417 * @param hdr header of the message to copy
418 * @return envelope containing @a hdr
420 struct GNUNET_MQ_Envelope *
421 GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr);
425 * Discard the message queue message, free all
426 * allocated resources. Must be called in the event
427 * that a message is created but should not actually be sent.
429 * @param mqm the message to discard
432 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm);
436 * Obtain the current length of the message queue.
438 * @param mq queue to inspect
439 * @return number of queued, non-transmitted messages
442 GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq);
446 * Send a message with the give message queue.
447 * May only be called once per message.
449 * @param mq message queue
450 * @param ev the envelope with the message to send.
453 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
454 struct GNUNET_MQ_Envelope *ev);
458 * Send a copy of a message with the give message queue.
459 * Can be called repeatedly on the same envelope.
461 * @param mq message queue
462 * @param ev the envelope with the message to send.
465 GNUNET_MQ_send_copy (struct GNUNET_MQ_Handle *mq,
466 const struct GNUNET_MQ_Envelope *ev);
470 * Cancel sending the message. Message must have been sent with
471 * #GNUNET_MQ_send before. May not be called after the notify sent
472 * callback has been called
474 * @param ev queued envelope to cancel
477 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev);
481 * Associate the assoc_data in @a mq with a unique request id.
483 * @param mq message queue, id will be unique for the queue
484 * @param assoc_data to associate
487 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
492 * Get the data associated with a @a request_id in a queue
494 * @param mq the message queue with the association
495 * @param request_id the request id we are interested in
496 * @return the associated data
499 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
500 uint32_t request_id);
504 * Remove the association for a @a request_id
506 * @param mq the message queue with the association
507 * @param request_id the request id we want to remove
508 * @return the associated data
511 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
512 uint32_t request_id);
516 * Create a message queue for the specified handlers.
518 * @param send function the implements sending messages
519 * @param destroy function that implements destroying the queue
520 * @param cancel function that implements canceling a message
521 * @param impl_state for the queue, passed to @a send, @a destroy and @a cancel
522 * @param handlers array of message handlers
523 * @param error_handler handler for read and write errors
524 * @param cls closure for message handlers and error handler
525 * @return a new message queue
527 struct GNUNET_MQ_Handle *
528 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
529 GNUNET_MQ_DestroyImpl destroy,
530 GNUNET_MQ_CancelImpl cancel,
532 const struct GNUNET_MQ_MessageHandler *handlers,
533 GNUNET_MQ_ErrorHandler error_handler,
538 * Call a callback once the envelope has been sent, that is,
539 * sending it can not be canceled anymore.
540 * There can be only one notify sent callback per envelope.
542 * @param ev message to call the notify callback for
543 * @param cb the notify callback
544 * @param cls closure for the callback
547 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
548 GNUNET_MQ_NotifyCallback cb,
553 * Destroy the message queue.
555 * @param mq message queue to destroy
558 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq);
562 * Call the message message handler that was registered
563 * for the type of the given message in the given message queue.
565 * This function is indended to be used for the implementation
568 * @param mq message queue with the handlers
569 * @param mh message to dispatch
572 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
573 const struct GNUNET_MessageHeader *mh);
577 * Call the error handler of a message queue with the given
578 * error code. If there is no error handler, log a warning.
580 * This function is intended to be used for the implementation
583 * @param mq message queue
584 * @param error the error type
587 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
588 enum GNUNET_MQ_Error error);
592 * Call the send implementation for the next queued message, if any.
593 * Only useful for implementing message queues, results in undefined
594 * behavior if not used carefully.
596 * @param mq message queue to send the next message with
599 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq);
603 * Get the message that should currently be sent. The returned
604 * message is only valid until #GNUNET_MQ_impl_send_continue is
605 * called. Fails if there is no current message. Only useful for
606 * implementing message queues, results in undefined behavior if not
609 * @param mq message queue with the current message, only valid
610 * until #GNUNET_MQ_impl_send_continue() is called
611 * @return message to send, never NULL
613 const struct GNUNET_MessageHeader *
614 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq);
618 * Get the implementation state associated with the
621 * While the GNUNET_MQ_Impl* callbacks receive the
622 * implementation state, continuations that are scheduled
623 * by the implementation function often only have one closure
624 * argument, with this function it is possible to get at the
625 * implementation state when only passing the `struct GNUNET_MQ_Handle`
628 * @param mq message queue with the current message
629 * @return message to send, never NULL
632 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq);
637 /** @} */ /* end of group mq */