2 This file is part of GNUnet.
3 Copyright (C) 2012-2017 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
24 * @brief general purpose request queue
27 #include "gnunet_util_lib.h"
29 #define LOG(kind,...) GNUNET_log_from (kind, "util-mq",__VA_ARGS__)
32 struct GNUNET_MQ_Envelope
35 * Messages are stored in a linked list.
36 * Each queue has its own list of envelopes.
38 struct GNUNET_MQ_Envelope *next;
41 * Messages are stored in a linked list
42 * Each queue has its own list of envelopes.
44 struct GNUNET_MQ_Envelope *prev;
47 * Actual allocated message header.
48 * The GNUNET_MQ_Envelope header is allocated at
49 * the end of the message.
51 struct GNUNET_MessageHeader *mh;
54 * Queue the message is queued in, NULL if message is not queued.
56 struct GNUNET_MQ_Handle *parent_queue;
59 * Called after the message was sent irrevocably.
61 GNUNET_SCHEDULER_TaskCallback sent_cb;
64 * Closure for @e sent_cb
69 * Flags that were set for this envelope by
70 * #GNUNET_MQ_env_set_options(). Only valid if
71 * @e have_custom_options is set.
76 * Additional options buffer set for this envelope by
77 * #GNUNET_MQ_env_set_options(). Only valid if
78 * @e have_custom_options is set.
83 * Did the application call #GNUNET_MQ_env_set_options()?
85 int have_custom_options;
90 * Handle to a message queue.
92 struct GNUNET_MQ_Handle
95 * Handlers array, or NULL if the queue should not receive messages
97 struct GNUNET_MQ_MessageHandler *handlers;
100 * Actual implementation of message sending,
101 * called when a message is added
103 GNUNET_MQ_SendImpl send_impl;
106 * Implementation-dependent queue destruction function
108 GNUNET_MQ_DestroyImpl destroy_impl;
111 * Implementation-dependent send cancel function
113 GNUNET_MQ_CancelImpl cancel_impl;
116 * Implementation-specific state
121 * Callback will be called when an error occurs.
123 GNUNET_MQ_ErrorHandler error_handler;
126 * Closure for the error handler.
128 void *error_handler_cls;
131 * Task to asynchronously run #impl_send_continue().
133 struct GNUNET_SCHEDULER_Task *send_task;
136 * Linked list of messages pending to be sent
138 struct GNUNET_MQ_Envelope *envelope_head;
141 * Linked list of messages pending to be sent
143 struct GNUNET_MQ_Envelope *envelope_tail;
146 * Message that is currently scheduled to be
147 * sent. Not the head of the message queue, as the implementation
148 * needs to know if sending has been already scheduled or not.
150 struct GNUNET_MQ_Envelope *current_envelope;
153 * Map of associations, lazily allocated
155 struct GNUNET_CONTAINER_MultiHashMap32 *assoc_map;
158 * Functions to call on queue destruction; kept in a DLL.
160 struct GNUNET_MQ_DestroyNotificationHandle *dnh_head;
163 * Functions to call on queue destruction; kept in a DLL.
165 struct GNUNET_MQ_DestroyNotificationHandle *dnh_tail;
168 * Additional options buffer set for this queue by
169 * #GNUNET_MQ_set_options(). Default is 0.
171 const void *default_extra;
174 * Flags that were set for this queue by
175 * #GNUNET_MQ_set_options(). Default is 0.
177 uint64_t default_flags;
180 * Next id that should be used for the @e assoc_map,
181 * initialized lazily to a random value together with
187 * Number of entries we have in the envelope-DLL.
189 unsigned int queue_length;
192 * #GNUNET_YES if GNUNET_MQ_impl_evacuate was called.
193 * FIXME: is this dead?
198 * #GNUNET_YES if GNUNET_MQ_impl_send_in_flight() was called.
205 * Call the message message handler that was registered
206 * for the type of the given message in the given message queue.
208 * This function is indended to be used for the implementation
211 * @param mq message queue with the handlers
212 * @param mh message to dispatch
215 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
216 const struct GNUNET_MessageHeader *mh)
218 const struct GNUNET_MQ_MessageHandler *handler;
219 int handled = GNUNET_NO;
220 uint16_t msize = ntohs (mh->size);
221 uint16_t mtype = ntohs (mh->type);
223 LOG (GNUNET_ERROR_TYPE_DEBUG,
224 "Queue %p received message of type %u and size %u\n",
229 if (NULL == mq->handlers)
231 for (handler = mq->handlers; NULL != handler->cb; handler++)
233 if (handler->type == mtype)
235 handled = GNUNET_YES;
236 if ( (handler->expected_size > msize) ||
237 ( (handler->expected_size != msize) &&
238 (NULL == handler->mv) ) )
240 /* Too small, or not an exact size and
241 no 'mv' handler to check rest */
242 LOG (GNUNET_ERROR_TYPE_ERROR,
243 "Received malformed message of type %u\n",
244 (unsigned int) handler->type);
245 GNUNET_MQ_inject_error (mq,
246 GNUNET_MQ_ERROR_MALFORMED);
249 if ( (NULL == handler->mv) ||
251 handler->mv (handler->cls, mh)) )
253 /* message well-formed, pass to handler */
254 handler->cb (handler->cls, mh);
258 /* Message rejected by check routine */
259 LOG (GNUNET_ERROR_TYPE_ERROR,
260 "Received malformed message of type %u\n",
261 (unsigned int) handler->type);
262 GNUNET_MQ_inject_error (mq,
263 GNUNET_MQ_ERROR_MALFORMED);
269 if (GNUNET_NO == handled)
270 LOG (GNUNET_ERROR_TYPE_INFO,
271 "No handler for message of type %u and size %u\n",
277 * Call the error handler of a message queue with the given
278 * error code. If there is no error handler, log a warning.
280 * This function is intended to be used by the implementation
283 * @param mq message queue
284 * @param error the error type
287 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
288 enum GNUNET_MQ_Error error)
290 if (NULL == mq->error_handler)
292 LOG (GNUNET_ERROR_TYPE_WARNING,
293 "Got error %d, but no handler installed\n",
297 mq->error_handler (mq->error_handler_cls,
303 * Discard the message queue message, free all
304 * allocated resources. Must be called in the event
305 * that a message is created but should not actually be sent.
307 * @param mqm the message to discard
310 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *ev)
312 GNUNET_assert (NULL == ev->parent_queue);
318 * Obtain the current length of the message queue.
320 * @param mq queue to inspect
321 * @return number of queued, non-transmitted messages
324 GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq)
326 if (GNUNET_YES != mq->in_flight)
328 return mq->queue_length;
330 return mq->queue_length - 1;
335 * Send a message with the given message queue.
336 * May only be called once per message.
338 * @param mq message queue
339 * @param ev the envelope with the message to send.
342 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
343 struct GNUNET_MQ_Envelope *ev)
345 GNUNET_assert (NULL != mq);
346 GNUNET_assert (NULL == ev->parent_queue);
349 GNUNET_break (mq->queue_length < 10000); /* This would seem like a bug... */
350 ev->parent_queue = mq;
351 /* is the implementation busy? queue it! */
352 if ( (NULL != mq->current_envelope) ||
353 (NULL != mq->send_task) )
355 GNUNET_CONTAINER_DLL_insert_tail (mq->envelope_head,
360 GNUNET_assert (NULL == mq->envelope_head);
361 mq->current_envelope = ev;
363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
364 "mq: sending message of type %u, queue empty (MQ: %p)\n",
375 * Remove the first envelope that has not yet been sent from the message
376 * queue and return it.
378 * @param mq queue to remove envelope from
379 * @return NULL if queue is empty (or has no envelope that is not under transmission)
381 struct GNUNET_MQ_Envelope *
382 GNUNET_MQ_unsent_head (struct GNUNET_MQ_Handle *mq)
384 struct GNUNET_MQ_Envelope *env;
386 env = mq->envelope_head;
387 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
391 env->parent_queue = NULL;
397 * Function to copy an envelope. The envelope must not yet
398 * be in any queue or have any options or callbacks set.
400 * @param env envelope to copy
401 * @return copy of @a env
403 struct GNUNET_MQ_Envelope *
404 GNUNET_MQ_env_copy (struct GNUNET_MQ_Envelope *env)
406 GNUNET_assert (NULL == env->next);
407 GNUNET_assert (NULL == env->parent_queue);
408 GNUNET_assert (NULL == env->sent_cb);
409 GNUNET_assert (GNUNET_NO == env->have_custom_options);
410 return GNUNET_MQ_msg_copy (env->mh);
415 * Send a copy of a message with the given message queue.
416 * Can be called repeatedly on the same envelope.
418 * @param mq message queue
419 * @param ev the envelope with the message to send.
422 GNUNET_MQ_send_copy (struct GNUNET_MQ_Handle *mq,
423 const struct GNUNET_MQ_Envelope *ev)
425 struct GNUNET_MQ_Envelope *env;
428 msize = ntohs (ev->mh->size);
429 env = GNUNET_malloc (sizeof (struct GNUNET_MQ_Envelope) +
431 env->mh = (struct GNUNET_MessageHeader *) &env[1];
432 env->sent_cb = ev->sent_cb;
433 env->sent_cls = ev->sent_cls;
434 GNUNET_memcpy (&env[1],
443 * Task run to call the send implementation for the next queued
444 * message, if any. Only useful for implementing message queues,
445 * results in undefined behavior if not used carefully.
447 * @param cls message queue to send the next message with
450 impl_send_continue (void *cls)
452 struct GNUNET_MQ_Handle *mq = cls;
454 mq->send_task = NULL;
455 /* call is only valid if we're actually currently sending
457 if (NULL == mq->envelope_head)
459 mq->current_envelope = mq->envelope_head;
460 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
462 mq->current_envelope);
464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
465 "mq: sending message of type %u from queue\n",
466 ntohs(mq->current_envelope->mh->type));
469 mq->current_envelope->mh,
475 * Call the send implementation for the next queued message, if any.
476 * Only useful for implementing message queues, results in undefined
477 * behavior if not used carefully.
479 * @param mq message queue to send the next message with
482 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq)
484 struct GNUNET_MQ_Envelope *current_envelope;
485 GNUNET_SCHEDULER_TaskCallback cb;
487 GNUNET_assert (0 < mq->queue_length);
489 mq->in_flight = GNUNET_NO;
490 current_envelope = mq->current_envelope;
491 current_envelope->parent_queue = NULL;
492 mq->current_envelope = NULL;
493 GNUNET_assert (NULL == mq->send_task);
494 mq->send_task = GNUNET_SCHEDULER_add_now (&impl_send_continue,
496 if (NULL != (cb = current_envelope->sent_cb))
498 current_envelope->sent_cb = NULL;
499 cb (current_envelope->sent_cls);
501 GNUNET_free (current_envelope);
506 * Call the send notification for the current message, but do not
507 * try to send the next message until #GNUNET_MQ_impl_send_continue
510 * Only useful for implementing message queues, results in undefined
511 * behavior if not used carefully.
513 * @param mq message queue to send the next message with
516 GNUNET_MQ_impl_send_in_flight (struct GNUNET_MQ_Handle *mq)
518 struct GNUNET_MQ_Envelope *current_envelope;
519 GNUNET_SCHEDULER_TaskCallback cb;
521 mq->in_flight = GNUNET_YES;
522 /* call is only valid if we're actually currently sending
524 current_envelope = mq->current_envelope;
525 GNUNET_assert (NULL != current_envelope);
526 /* can't call cancel from now on anymore */
527 current_envelope->parent_queue = NULL;
528 if (NULL != (cb = current_envelope->sent_cb))
530 current_envelope->sent_cb = NULL;
531 cb (current_envelope->sent_cls);
537 * Create a message queue for the specified handlers.
539 * @param send function the implements sending messages
540 * @param destroy function that implements destroying the queue
541 * @param cancel function that implements canceling a message
542 * @param impl_state for the queue, passed to 'send' and 'destroy'
543 * @param handlers array of message handlers
544 * @param error_handler handler for read and write errors
545 * @param error_handler_cls closure for @a error_handler
546 * @return a new message queue
548 struct GNUNET_MQ_Handle *
549 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
550 GNUNET_MQ_DestroyImpl destroy,
551 GNUNET_MQ_CancelImpl cancel,
553 const struct GNUNET_MQ_MessageHandler *handlers,
554 GNUNET_MQ_ErrorHandler error_handler,
555 void *error_handler_cls)
557 struct GNUNET_MQ_Handle *mq;
559 mq = GNUNET_new (struct GNUNET_MQ_Handle);
560 mq->send_impl = send;
561 mq->destroy_impl = destroy;
562 mq->cancel_impl = cancel;
563 mq->handlers = GNUNET_MQ_copy_handlers (handlers);
564 mq->error_handler = error_handler;
565 mq->error_handler_cls = error_handler_cls;
566 mq->impl_state = impl_state;
573 * Change the closure argument in all of the `handlers` of the
576 * @param mq to modify
577 * @param handlers_cls new closure to use
580 GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq,
585 if (NULL == mq->handlers)
587 for (i=0;NULL != mq->handlers[i].cb; i++)
588 mq->handlers[i].cls = handlers_cls;
593 * Get the message that should currently be sent.
594 * Fails if there is no current message.
595 * Only useful for implementing message queues,
596 * results in undefined behavior if not used carefully.
598 * @param mq message queue with the current message
599 * @return message to send, never NULL
601 const struct GNUNET_MessageHeader *
602 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq)
604 GNUNET_assert (NULL != mq->current_envelope);
605 GNUNET_assert (NULL != mq->current_envelope->mh);
606 return mq->current_envelope->mh;
611 * Get the implementation state associated with the
614 * While the GNUNET_MQ_Impl* callbacks receive the
615 * implementation state, continuations that are scheduled
616 * by the implementation function often only have one closure
617 * argument, with this function it is possible to get at the
618 * implementation state when only passing the GNUNET_MQ_Handle
621 * @param mq message queue with the current message
622 * @return message to send, never NULL
625 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq)
627 return mq->impl_state;
631 struct GNUNET_MQ_Envelope *
632 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
636 struct GNUNET_MQ_Envelope *ev;
638 ev = GNUNET_malloc (size + sizeof (struct GNUNET_MQ_Envelope));
639 ev->mh = (struct GNUNET_MessageHeader *) &ev[1];
640 ev->mh->size = htons (size);
641 ev->mh->type = htons (type);
649 * Create a new envelope by copying an existing message.
651 * @param hdr header of the message to copy
652 * @return envelope containing @a hdr
654 struct GNUNET_MQ_Envelope *
655 GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr)
657 struct GNUNET_MQ_Envelope *mqm;
658 uint16_t size = ntohs (hdr->size);
660 mqm = GNUNET_malloc (sizeof (*mqm) + size);
661 mqm->mh = (struct GNUNET_MessageHeader *) &mqm[1];
662 GNUNET_memcpy (mqm->mh,
670 * Implementation of the #GNUNET_MQ_msg_nested_mh macro.
672 * @param mhp pointer to the message header pointer that will be changed to allocate at
673 * the newly allocated space for the message.
674 * @param base_size size of the data before the nested message
675 * @param type type of the message in the envelope
676 * @param nested_mh the message to append to the message after base_size
678 struct GNUNET_MQ_Envelope *
679 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
682 const struct GNUNET_MessageHeader *nested_mh)
684 struct GNUNET_MQ_Envelope *mqm;
687 if (NULL == nested_mh)
688 return GNUNET_MQ_msg_ (mhp, base_size, type);
690 size = base_size + ntohs (nested_mh->size);
692 /* check for uint16_t overflow */
693 if (size < base_size)
696 mqm = GNUNET_MQ_msg_ (mhp, size, type);
697 GNUNET_memcpy ((char *) mqm->mh + base_size,
699 ntohs (nested_mh->size));
706 * Associate the assoc_data in mq with a unique request id.
708 * @param mq message queue, id will be unique for the queue
709 * @param assoc_data to associate
712 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
717 if (NULL == mq->assoc_map)
719 mq->assoc_map = GNUNET_CONTAINER_multihashmap32_create (8);
723 GNUNET_assert (GNUNET_OK ==
724 GNUNET_CONTAINER_multihashmap32_put (mq->assoc_map,
727 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
733 * Get the data associated with a @a request_id in a queue
735 * @param mq the message queue with the association
736 * @param request_id the request id we are interested in
737 * @return the associated data
740 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
743 if (NULL == mq->assoc_map)
745 return GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map,
751 * Remove the association for a @a request_id
753 * @param mq the message queue with the association
754 * @param request_id the request id we want to remove
755 * @return the associated data
758 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
763 if (NULL == mq->assoc_map)
765 val = GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map,
767 GNUNET_CONTAINER_multihashmap32_remove_all (mq->assoc_map,
774 * Call a callback once the envelope has been sent, that is,
775 * sending it can not be canceled anymore.
776 * There can be only one notify sent callback per envelope.
778 * @param ev message to call the notify callback for
779 * @param cb the notify callback
780 * @param cb_cls closure for the callback
783 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
784 GNUNET_SCHEDULER_TaskCallback cb,
787 GNUNET_assert (NULL == ev->sent_cb);
789 ev->sent_cls = cb_cls;
794 * Handle we return for callbacks registered to be
795 * notified when #GNUNET_MQ_destroy() is called on a queue.
797 struct GNUNET_MQ_DestroyNotificationHandle
802 struct GNUNET_MQ_DestroyNotificationHandle *prev;
807 struct GNUNET_MQ_DestroyNotificationHandle *next;
810 * Queue to notify about.
812 struct GNUNET_MQ_Handle *mq;
817 GNUNET_SCHEDULER_TaskCallback cb;
827 * Destroy the message queue.
829 * @param mq message queue to destroy
832 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq)
834 struct GNUNET_MQ_DestroyNotificationHandle *dnh;
836 if (NULL != mq->destroy_impl)
838 mq->destroy_impl (mq, mq->impl_state);
840 if (NULL != mq->send_task)
842 GNUNET_SCHEDULER_cancel (mq->send_task);
843 mq->send_task = NULL;
845 while (NULL != mq->envelope_head)
847 struct GNUNET_MQ_Envelope *ev;
849 ev = mq->envelope_head;
850 ev->parent_queue = NULL;
851 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
854 GNUNET_assert (0 < mq->queue_length);
856 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
857 "MQ destroy drops message of type %u\n",
858 ntohs (ev->mh->type));
859 GNUNET_MQ_discard (ev);
861 if (NULL != mq->current_envelope)
863 /* we can only discard envelopes that
865 mq->current_envelope->parent_queue = NULL;
866 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
867 "MQ destroy drops current message of type %u\n",
868 ntohs (mq->current_envelope->mh->type));
869 GNUNET_MQ_discard (mq->current_envelope);
870 mq->current_envelope = NULL;
871 GNUNET_assert (0 < mq->queue_length);
874 GNUNET_assert (0 == mq->queue_length);
875 while (NULL != (dnh = mq->dnh_head))
877 dnh->cb (dnh->cb_cls);
878 GNUNET_MQ_destroy_notify_cancel (dnh);
880 if (NULL != mq->assoc_map)
882 GNUNET_CONTAINER_multihashmap32_destroy (mq->assoc_map);
883 mq->assoc_map = NULL;
885 GNUNET_free_non_null (mq->handlers);
890 const struct GNUNET_MessageHeader *
891 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
895 uint16_t nested_size;
896 const struct GNUNET_MessageHeader *nested_msg;
898 whole_size = ntohs (mh->size);
899 GNUNET_assert (whole_size >= base_size);
900 nested_size = whole_size - base_size;
901 if (0 == nested_size)
903 if (nested_size < sizeof (struct GNUNET_MessageHeader))
908 nested_msg = (const struct GNUNET_MessageHeader *) ((char *) mh + base_size);
909 if (ntohs (nested_msg->size) != nested_size)
919 * Cancel sending the message. Message must have been sent with
920 * #GNUNET_MQ_send before. May not be called after the notify sent
921 * callback has been called
923 * @param ev queued envelope to cancel
926 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev)
928 struct GNUNET_MQ_Handle *mq = ev->parent_queue;
930 GNUNET_assert (NULL != mq);
931 GNUNET_assert (NULL != mq->cancel_impl);
933 mq->evacuate_called = GNUNET_NO;
935 if (mq->current_envelope == ev)
937 /* complex case, we already started with transmitting
938 the message using the callbacks. */
939 GNUNET_assert (0 < mq->queue_length);
943 /* continue sending the next message, if any */
944 mq->current_envelope = mq->envelope_head;
945 if (NULL != mq->current_envelope)
947 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
949 mq->current_envelope);
951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
952 "mq: sending canceled message of type %u queue\n",
953 ntohs(ev->mh->type));
956 mq->current_envelope->mh,
962 /* simple case, message is still waiting in the queue */
963 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
966 GNUNET_assert (0 < mq->queue_length);
970 if (GNUNET_YES != mq->evacuate_called)
972 ev->parent_queue = NULL;
981 * Function to obtain the current envelope
982 * from within #GNUNET_MQ_SendImpl implementations.
984 * @param mq message queue to interrogate
985 * @return the current envelope
987 struct GNUNET_MQ_Envelope *
988 GNUNET_MQ_get_current_envelope (struct GNUNET_MQ_Handle *mq)
990 return mq->current_envelope;
995 * Function to obtain the last envelope in the queue.
997 * @param mq message queue to interrogate
998 * @return the last envelope in the queue
1000 struct GNUNET_MQ_Envelope *
1001 GNUNET_MQ_get_last_envelope (struct GNUNET_MQ_Handle *mq)
1003 if (NULL != mq->envelope_tail)
1004 return mq->envelope_tail;
1006 return mq->current_envelope;
1011 * Set application-specific options for this envelope.
1012 * Overrides the options set for the queue with
1013 * #GNUNET_MQ_set_options() for this message only.
1015 * @param env message to set options for
1016 * @param flags flags to use (meaning is queue-specific)
1017 * @param extra additional buffer for further data (also queue-specific)
1020 GNUNET_MQ_env_set_options (struct GNUNET_MQ_Envelope *env,
1026 env->have_custom_options = GNUNET_YES;
1031 * Get application-specific options for this envelope.
1033 * @param env message to set options for
1034 * @param[out] flags set to flags to use (meaning is queue-specific)
1035 * @return extra additional buffer for further data (also queue-specific)
1038 GNUNET_MQ_env_get_options (struct GNUNET_MQ_Envelope *env,
1041 struct GNUNET_MQ_Handle *mq = env->parent_queue;
1043 if (GNUNET_YES == env->have_custom_options)
1045 *flags = env->flags;
1053 *flags = mq->default_flags;
1054 return mq->default_extra;
1059 * Set application-specific options for this queue.
1061 * @param mq message queue to set options for
1062 * @param flags flags to use (meaning is queue-specific)
1063 * @param extra additional buffer for further data (also queue-specific)
1066 GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq,
1070 mq->default_flags = flags;
1071 mq->default_extra = extra;
1076 * Register function to be called whenever @a mq is being
1079 * @param mq message queue to watch
1080 * @param cb function to call on @a mq destruction
1081 * @param cb_cls closure for @a cb
1082 * @return handle for #GNUNET_MQ_destroy_notify_cancel().
1084 struct GNUNET_MQ_DestroyNotificationHandle *
1085 GNUNET_MQ_destroy_notify (struct GNUNET_MQ_Handle *mq,
1086 GNUNET_SCHEDULER_TaskCallback cb,
1089 struct GNUNET_MQ_DestroyNotificationHandle *dnh;
1091 dnh = GNUNET_new (struct GNUNET_MQ_DestroyNotificationHandle);
1094 dnh->cb_cls = cb_cls;
1095 GNUNET_CONTAINER_DLL_insert (mq->dnh_head,
1103 * Cancel registration from #GNUNET_MQ_destroy_notify().
1105 * @param dnh handle for registration to cancel
1108 GNUNET_MQ_destroy_notify_cancel (struct GNUNET_MQ_DestroyNotificationHandle *dnh)
1110 struct GNUNET_MQ_Handle *mq = dnh->mq;
1112 GNUNET_CONTAINER_DLL_remove (mq->dnh_head,
1120 * Insert @a env into the envelope DLL starting at @a env_head
1121 * Note that @a env must not be in any MQ while this function
1122 * is used with DLLs defined outside of the MQ module. This
1123 * is just in case some application needs to also manage a
1124 * FIFO of envelopes independent of MQ itself and wants to
1125 * re-use the pointers internal to @a env. Use with caution.
1127 * @param[in|out] env_head of envelope DLL
1128 * @param[in|out] env_tail tail of envelope DLL
1129 * @param[in|out] env element to insert at the tail
1132 GNUNET_MQ_dll_insert_tail (struct GNUNET_MQ_Envelope **env_head,
1133 struct GNUNET_MQ_Envelope **env_tail,
1134 struct GNUNET_MQ_Envelope *env)
1136 GNUNET_CONTAINER_DLL_insert_tail (*env_head,
1143 * Remove @a env from the envelope DLL starting at @a env_head.
1144 * Note that @a env must not be in any MQ while this function
1145 * is used with DLLs defined outside of the MQ module. This
1146 * is just in case some application needs to also manage a
1147 * FIFO of envelopes independent of MQ itself and wants to
1148 * re-use the pointers internal to @a env. Use with caution.
1150 * @param[in|out] env_head of envelope DLL
1151 * @param[in|out] env_tail tail of envelope DLL
1152 * @param[in|out] env element to remove from the DLL
1155 GNUNET_MQ_dll_remove (struct GNUNET_MQ_Envelope **env_head,
1156 struct GNUNET_MQ_Envelope **env_tail,
1157 struct GNUNET_MQ_Envelope *env)
1159 GNUNET_CONTAINER_DLL_remove (*env_head,
1166 * Copy an array of handlers.
1168 * Useful if the array has been delared in local memory and needs to be
1169 * persisted for future use.
1171 * @param handlers Array of handlers to be copied. Can be NULL (nothing done).
1172 * @return A newly allocated array of handlers.
1173 * Needs to be freed with #GNUNET_free.
1175 struct GNUNET_MQ_MessageHandler *
1176 GNUNET_MQ_copy_handlers (const struct GNUNET_MQ_MessageHandler *handlers)
1178 struct GNUNET_MQ_MessageHandler *copy;
1181 if (NULL == handlers)
1184 count = GNUNET_MQ_count_handlers (handlers);
1185 copy = GNUNET_new_array (count + 1,
1186 struct GNUNET_MQ_MessageHandler);
1187 GNUNET_memcpy (copy,
1189 count * sizeof (struct GNUNET_MQ_MessageHandler));
1195 * Count the handlers in a handler array.
1197 * @param handlers Array of handlers to be counted.
1198 * @return The number of handlers in the array.
1201 GNUNET_MQ_count_handlers (const struct GNUNET_MQ_MessageHandler *handlers)
1205 if (NULL == handlers)
1208 for (i=0; NULL != handlers[i].cb; i++) ;