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 send_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 "Received message of type %u and size %u\n",
227 if (NULL == mq->handlers)
229 for (handler = mq->handlers; NULL != handler->cb; handler++)
231 if (handler->type == mtype)
233 handled = GNUNET_YES;
234 if ( (handler->expected_size > msize) ||
235 ( (handler->expected_size != msize) &&
236 (NULL == handler->mv) ) )
238 /* Too small, or not an exact size and
239 no 'mv' handler to check rest */
240 LOG (GNUNET_ERROR_TYPE_ERROR,
241 "Received malformed message of type %u\n",
242 (unsigned int) handler->type);
243 GNUNET_MQ_inject_error (mq,
244 GNUNET_MQ_ERROR_MALFORMED);
247 if ( (NULL == handler->mv) ||
249 handler->mv (handler->cls, mh)) )
251 /* message well-formed, pass to handler */
252 handler->cb (handler->cls, mh);
256 /* Message rejected by check routine */
257 LOG (GNUNET_ERROR_TYPE_ERROR,
258 "Received malformed message of type %u\n",
259 (unsigned int) handler->type);
260 GNUNET_MQ_inject_error (mq,
261 GNUNET_MQ_ERROR_MALFORMED);
267 if (GNUNET_NO == handled)
268 LOG (GNUNET_ERROR_TYPE_INFO,
269 "No handler for message of type %u and size %u\n",
275 * Call the error handler of a message queue with the given
276 * error code. If there is no error handler, log a warning.
278 * This function is intended to be used by the implementation
281 * @param mq message queue
282 * @param error the error type
285 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
286 enum GNUNET_MQ_Error error)
288 if (NULL == mq->error_handler)
290 LOG (GNUNET_ERROR_TYPE_WARNING,
291 "Got error %d, but no handler installed\n",
295 mq->error_handler (mq->error_handler_cls,
301 * Discard the message queue message, free all
302 * allocated resources. Must be called in the event
303 * that a message is created but should not actually be sent.
305 * @param mqm the message to discard
308 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *ev)
310 GNUNET_assert (NULL == ev->parent_queue);
316 * Obtain the current length of the message queue.
318 * @param mq queue to inspect
319 * @return number of queued, non-transmitted messages
322 GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq)
324 if (GNUNET_YES != mq->in_flight)
326 return mq->queue_length;
328 return mq->queue_length - 1;
333 * Send a message with the given message queue.
334 * May only be called once per message.
336 * @param mq message queue
337 * @param ev the envelope with the message to send.
340 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
341 struct GNUNET_MQ_Envelope *ev)
343 GNUNET_assert (NULL != mq);
344 GNUNET_assert (NULL == ev->parent_queue);
347 GNUNET_break (mq->queue_length < 10000); /* This would seem like a bug... */
348 ev->parent_queue = mq;
349 /* is the implementation busy? queue it! */
350 if ( (NULL != mq->current_envelope) ||
351 (NULL != mq->send_task) )
353 GNUNET_CONTAINER_DLL_insert_tail (mq->envelope_head,
358 GNUNET_assert (NULL == mq->envelope_head);
359 mq->current_envelope = ev;
367 * Remove the first envelope that has not yet been sent from the message
368 * queue and return it.
370 * @param mq queue to remove envelope from
371 * @return NULL if queue is empty (or has no envelope that is not under transmission)
373 struct GNUNET_MQ_Envelope *
374 GNUNET_MQ_unsent_head (struct GNUNET_MQ_Handle *mq)
376 struct GNUNET_MQ_Envelope *env;
378 env = mq->envelope_head;
379 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
383 env->parent_queue = NULL;
389 * Function to copy an envelope. The envelope must not yet
390 * be in any queue or have any options or callbacks set.
392 * @param env envelope to copy
393 * @return copy of @a env
395 struct GNUNET_MQ_Envelope *
396 GNUNET_MQ_env_copy (struct GNUNET_MQ_Envelope *env)
398 GNUNET_assert (NULL == env->next);
399 GNUNET_assert (NULL == env->parent_queue);
400 GNUNET_assert (NULL == env->sent_cb);
401 GNUNET_assert (GNUNET_NO == env->have_custom_options);
402 return GNUNET_MQ_msg_copy (env->mh);
407 * Send a copy of a message with the given message queue.
408 * Can be called repeatedly on the same envelope.
410 * @param mq message queue
411 * @param ev the envelope with the message to send.
414 GNUNET_MQ_send_copy (struct GNUNET_MQ_Handle *mq,
415 const struct GNUNET_MQ_Envelope *ev)
417 struct GNUNET_MQ_Envelope *env;
420 msize = ntohs (ev->mh->size);
421 env = GNUNET_malloc (sizeof (struct GNUNET_MQ_Envelope) +
423 env->mh = (struct GNUNET_MessageHeader *) &env[1];
424 env->sent_cb = ev->sent_cb;
425 env->sent_cls = ev->sent_cls;
426 GNUNET_memcpy (&env[1],
435 * Task run to call the send implementation for the next queued
436 * message, if any. Only useful for implementing message queues,
437 * results in undefined behavior if not used carefully.
439 * @param cls message queue to send the next message with
442 impl_send_continue (void *cls)
444 struct GNUNET_MQ_Handle *mq = cls;
446 mq->send_task = NULL;
447 /* call is only valid if we're actually currently sending
449 if (NULL == mq->envelope_head)
451 mq->current_envelope = mq->envelope_head;
452 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
454 mq->current_envelope);
456 mq->current_envelope->mh,
462 * Call the send implementation for the next queued message, if any.
463 * Only useful for implementing message queues, results in undefined
464 * behavior if not used carefully.
466 * @param mq message queue to send the next message with
469 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq)
471 struct GNUNET_MQ_Envelope *current_envelope;
472 GNUNET_SCHEDULER_TaskCallback cb;
474 GNUNET_assert (0 < mq->queue_length);
476 mq->in_flight = GNUNET_NO;
477 current_envelope = mq->current_envelope;
478 current_envelope->parent_queue = NULL;
479 mq->current_envelope = NULL;
480 GNUNET_assert (NULL == mq->send_task);
481 mq->send_task = GNUNET_SCHEDULER_add_now (&impl_send_continue,
483 if (NULL != (cb = current_envelope->sent_cb))
485 current_envelope->sent_cb = NULL;
486 cb (current_envelope->sent_cls);
488 GNUNET_free (current_envelope);
493 * Call the send notification for the current message, but do not
494 * try to send the next message until #GNUNET_MQ_impl_send_continue
497 * Only useful for implementing message queues, results in undefined
498 * behavior if not used carefully.
500 * @param mq message queue to send the next message with
503 GNUNET_MQ_impl_send_in_flight (struct GNUNET_MQ_Handle *mq)
505 struct GNUNET_MQ_Envelope *current_envelope;
506 GNUNET_SCHEDULER_TaskCallback cb;
508 mq->in_flight = GNUNET_YES;
509 /* call is only valid if we're actually currently sending
511 current_envelope = mq->current_envelope;
512 GNUNET_assert (NULL != current_envelope);
513 /* can't call cancel from now on anymore */
514 current_envelope->parent_queue = NULL;
515 if (NULL != (cb = current_envelope->sent_cb))
517 current_envelope->sent_cb = NULL;
518 cb (current_envelope->sent_cls);
524 * Create a message queue for the specified handlers.
526 * @param send function the implements sending messages
527 * @param destroy function that implements destroying the queue
528 * @param cancel function that implements canceling a message
529 * @param impl_state for the queue, passed to 'send' and 'destroy'
530 * @param handlers array of message handlers
531 * @param error_handler handler for read and write errors
532 * @param error_handler_cls closure for @a error_handler
533 * @return a new message queue
535 struct GNUNET_MQ_Handle *
536 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
537 GNUNET_MQ_DestroyImpl destroy,
538 GNUNET_MQ_CancelImpl cancel,
540 const struct GNUNET_MQ_MessageHandler *handlers,
541 GNUNET_MQ_ErrorHandler error_handler,
542 void *error_handler_cls)
544 struct GNUNET_MQ_Handle *mq;
546 mq = GNUNET_new (struct GNUNET_MQ_Handle);
547 mq->send_impl = send;
548 mq->destroy_impl = destroy;
549 mq->cancel_impl = cancel;
550 mq->handlers = GNUNET_MQ_copy_handlers (handlers);
551 mq->error_handler = error_handler;
552 mq->error_handler_cls = error_handler_cls;
553 mq->impl_state = impl_state;
560 * Change the closure argument in all of the `handlers` of the
563 * @param mq to modify
564 * @param handlers_cls new closure to use
567 GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq,
572 if (NULL == mq->handlers)
574 for (i=0;NULL != mq->handlers[i].cb; i++)
575 mq->handlers[i].cls = handlers_cls;
580 * Get the message that should currently be sent.
581 * Fails if there is no current message.
582 * Only useful for implementing message queues,
583 * results in undefined behavior if not used carefully.
585 * @param mq message queue with the current message
586 * @return message to send, never NULL
588 const struct GNUNET_MessageHeader *
589 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq)
591 GNUNET_assert (NULL != mq->current_envelope);
592 GNUNET_assert (NULL != mq->current_envelope->mh);
593 return mq->current_envelope->mh;
598 * Get the implementation state associated with the
601 * While the GNUNET_MQ_Impl* callbacks receive the
602 * implementation state, continuations that are scheduled
603 * by the implementation function often only have one closure
604 * argument, with this function it is possible to get at the
605 * implementation state when only passing the GNUNET_MQ_Handle
608 * @param mq message queue with the current message
609 * @return message to send, never NULL
612 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq)
614 return mq->impl_state;
618 struct GNUNET_MQ_Envelope *
619 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
623 struct GNUNET_MQ_Envelope *ev;
625 ev = GNUNET_malloc (size + sizeof (struct GNUNET_MQ_Envelope));
626 ev->mh = (struct GNUNET_MessageHeader *) &ev[1];
627 ev->mh->size = htons (size);
628 ev->mh->type = htons (type);
636 * Create a new envelope by copying an existing message.
638 * @param hdr header of the message to copy
639 * @return envelope containing @a hdr
641 struct GNUNET_MQ_Envelope *
642 GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr)
644 struct GNUNET_MQ_Envelope *mqm;
645 uint16_t size = ntohs (hdr->size);
647 mqm = GNUNET_malloc (sizeof (*mqm) + size);
648 mqm->mh = (struct GNUNET_MessageHeader *) &mqm[1];
649 GNUNET_memcpy (mqm->mh,
657 * Implementation of the #GNUNET_MQ_msg_nested_mh macro.
659 * @param mhp pointer to the message header pointer that will be changed to allocate at
660 * the newly allocated space for the message.
661 * @param base_size size of the data before the nested message
662 * @param type type of the message in the envelope
663 * @param nested_mh the message to append to the message after base_size
665 struct GNUNET_MQ_Envelope *
666 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
669 const struct GNUNET_MessageHeader *nested_mh)
671 struct GNUNET_MQ_Envelope *mqm;
674 if (NULL == nested_mh)
675 return GNUNET_MQ_msg_ (mhp, base_size, type);
677 size = base_size + ntohs (nested_mh->size);
679 /* check for uint16_t overflow */
680 if (size < base_size)
683 mqm = GNUNET_MQ_msg_ (mhp, size, type);
684 GNUNET_memcpy ((char *) mqm->mh + base_size,
686 ntohs (nested_mh->size));
693 * Associate the assoc_data in mq with a unique request id.
695 * @param mq message queue, id will be unique for the queue
696 * @param assoc_data to associate
699 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
704 if (NULL == mq->assoc_map)
706 mq->assoc_map = GNUNET_CONTAINER_multihashmap32_create (8);
710 GNUNET_assert (GNUNET_OK ==
711 GNUNET_CONTAINER_multihashmap32_put (mq->assoc_map,
714 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
720 * Get the data associated with a @a request_id in a queue
722 * @param mq the message queue with the association
723 * @param request_id the request id we are interested in
724 * @return the associated data
727 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
730 if (NULL == mq->assoc_map)
732 return GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map,
738 * Remove the association for a @a request_id
740 * @param mq the message queue with the association
741 * @param request_id the request id we want to remove
742 * @return the associated data
745 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
750 if (NULL == mq->assoc_map)
752 val = GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map,
754 GNUNET_CONTAINER_multihashmap32_remove_all (mq->assoc_map,
761 * Call a callback once the envelope has been sent, that is,
762 * sending it can not be canceled anymore.
763 * There can be only one notify sent callback per envelope.
765 * @param ev message to call the notify callback for
766 * @param cb the notify callback
767 * @param cb_cls closure for the callback
770 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
771 GNUNET_SCHEDULER_TaskCallback cb,
774 GNUNET_assert (NULL == ev->sent_cb);
776 ev->sent_cls = cb_cls;
781 * Handle we return for callbacks registered to be
782 * notified when #GNUNET_MQ_destroy() is called on a queue.
784 struct GNUNET_MQ_DestroyNotificationHandle
789 struct GNUNET_MQ_DestroyNotificationHandle *prev;
794 struct GNUNET_MQ_DestroyNotificationHandle *next;
797 * Queue to notify about.
799 struct GNUNET_MQ_Handle *mq;
804 GNUNET_SCHEDULER_TaskCallback cb;
814 * Destroy the message queue.
816 * @param mq message queue to destroy
819 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq)
821 struct GNUNET_MQ_DestroyNotificationHandle *dnh;
823 if (NULL != mq->destroy_impl)
825 mq->destroy_impl (mq, mq->impl_state);
827 if (NULL != mq->send_task)
829 GNUNET_SCHEDULER_cancel (mq->send_task);
830 mq->send_task = NULL;
832 while (NULL != mq->envelope_head)
834 struct GNUNET_MQ_Envelope *ev;
836 ev = mq->envelope_head;
837 ev->parent_queue = NULL;
838 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
841 GNUNET_assert (0 < mq->queue_length);
843 GNUNET_MQ_discard (ev);
845 if (NULL != mq->current_envelope)
847 /* we can only discard envelopes that
849 mq->current_envelope->parent_queue = NULL;
850 GNUNET_MQ_discard (mq->current_envelope);
851 mq->current_envelope = NULL;
852 GNUNET_assert (0 < mq->queue_length);
855 GNUNET_assert (0 == mq->queue_length);
856 while (NULL != (dnh = mq->dnh_head))
858 dnh->cb (dnh->cb_cls);
859 GNUNET_MQ_destroy_notify_cancel (dnh);
861 if (NULL != mq->assoc_map)
863 GNUNET_CONTAINER_multihashmap32_destroy (mq->assoc_map);
864 mq->assoc_map = NULL;
866 GNUNET_free_non_null (mq->handlers);
871 const struct GNUNET_MessageHeader *
872 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
876 uint16_t nested_size;
877 const struct GNUNET_MessageHeader *nested_msg;
879 whole_size = ntohs (mh->size);
880 GNUNET_assert (whole_size >= base_size);
881 nested_size = whole_size - base_size;
882 if (0 == nested_size)
884 if (nested_size < sizeof (struct GNUNET_MessageHeader))
889 nested_msg = (const struct GNUNET_MessageHeader *) ((char *) mh + base_size);
890 if (ntohs (nested_msg->size) != nested_size)
900 * Cancel sending the message. Message must have been sent with
901 * #GNUNET_MQ_send before. May not be called after the notify sent
902 * callback has been called
904 * @param ev queued envelope to cancel
907 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev)
909 struct GNUNET_MQ_Handle *mq = ev->parent_queue;
911 GNUNET_assert (NULL != mq);
912 GNUNET_assert (NULL != mq->cancel_impl);
914 mq->evacuate_called = GNUNET_NO;
916 if (mq->current_envelope == ev)
918 /* complex case, we already started with transmitting
919 the message using the callbacks. */
920 GNUNET_assert (0 < mq->queue_length);
924 /* continue sending the next message, if any */
925 mq->current_envelope = mq->envelope_head;
926 if (NULL != mq->current_envelope)
928 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
930 mq->current_envelope);
932 mq->current_envelope->mh,
938 /* simple case, message is still waiting in the queue */
939 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
942 GNUNET_assert (0 < mq->queue_length);
946 if (GNUNET_YES != mq->evacuate_called)
948 ev->parent_queue = NULL;
957 * Function to obtain the current envelope
958 * from within #GNUNET_MQ_SendImpl implementations.
960 * @param mq message queue to interrogate
961 * @return the current envelope
963 struct GNUNET_MQ_Envelope *
964 GNUNET_MQ_get_current_envelope (struct GNUNET_MQ_Handle *mq)
966 return mq->current_envelope;
971 * Function to obtain the last envelope in the queue.
973 * @param mq message queue to interrogate
974 * @return the last envelope in the queue
976 struct GNUNET_MQ_Envelope *
977 GNUNET_MQ_get_last_envelope (struct GNUNET_MQ_Handle *mq)
979 if (NULL != mq->envelope_tail)
980 return mq->envelope_tail;
982 return mq->current_envelope;
987 * Set application-specific options for this envelope.
988 * Overrides the options set for the queue with
989 * #GNUNET_MQ_set_options() for this message only.
991 * @param env message to set options for
992 * @param flags flags to use (meaning is queue-specific)
993 * @param extra additional buffer for further data (also queue-specific)
996 GNUNET_MQ_env_set_options (struct GNUNET_MQ_Envelope *env,
1002 env->have_custom_options = GNUNET_YES;
1007 * Get application-specific options for this envelope.
1009 * @param env message to set options for
1010 * @param[out] flags set to flags to use (meaning is queue-specific)
1011 * @return extra additional buffer for further data (also queue-specific)
1014 GNUNET_MQ_env_get_options (struct GNUNET_MQ_Envelope *env,
1017 struct GNUNET_MQ_Handle *mq = env->parent_queue;
1019 if (GNUNET_YES == env->have_custom_options)
1021 *flags = env->flags;
1029 *flags = mq->default_flags;
1030 return mq->default_extra;
1035 * Set application-specific options for this queue.
1037 * @param mq message queue to set options for
1038 * @param flags flags to use (meaning is queue-specific)
1039 * @param extra additional buffer for further data (also queue-specific)
1042 GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq,
1046 mq->default_flags = flags;
1047 mq->default_extra = extra;
1052 * Register function to be called whenever @a mq is being
1055 * @param mq message queue to watch
1056 * @param cb function to call on @a mq destruction
1057 * @param cb_cls closure for @a cb
1058 * @return handle for #GNUNET_MQ_destroy_notify_cancel().
1060 struct GNUNET_MQ_DestroyNotificationHandle *
1061 GNUNET_MQ_destroy_notify (struct GNUNET_MQ_Handle *mq,
1062 GNUNET_SCHEDULER_TaskCallback cb,
1065 struct GNUNET_MQ_DestroyNotificationHandle *dnh;
1067 dnh = GNUNET_new (struct GNUNET_MQ_DestroyNotificationHandle);
1070 dnh->cb_cls = cb_cls;
1071 GNUNET_CONTAINER_DLL_insert (mq->dnh_head,
1079 * Cancel registration from #GNUNET_MQ_destroy_notify().
1081 * @param dnh handle for registration to cancel
1084 GNUNET_MQ_destroy_notify_cancel (struct GNUNET_MQ_DestroyNotificationHandle *dnh)
1086 struct GNUNET_MQ_Handle *mq = dnh->mq;
1088 GNUNET_CONTAINER_DLL_remove (mq->dnh_head,
1096 * Insert @a env into the envelope DLL starting at @a env_head
1097 * Note that @a env must not be in any MQ while this function
1098 * is used with DLLs defined outside of the MQ module. This
1099 * is just in case some application needs to also manage a
1100 * FIFO of envelopes independent of MQ itself and wants to
1101 * re-use the pointers internal to @a env. Use with caution.
1103 * @param[in|out] env_head of envelope DLL
1104 * @param[in|out] env_tail tail of envelope DLL
1105 * @param[in|out] env element to insert at the tail
1108 GNUNET_MQ_dll_insert_tail (struct GNUNET_MQ_Envelope **env_head,
1109 struct GNUNET_MQ_Envelope **env_tail,
1110 struct GNUNET_MQ_Envelope *env)
1112 GNUNET_CONTAINER_DLL_insert_tail (*env_head,
1119 * Remove @a env from the envelope DLL starting at @a env_head.
1120 * Note that @a env must not be in any MQ while this function
1121 * is used with DLLs defined outside of the MQ module. This
1122 * is just in case some application needs to also manage a
1123 * FIFO of envelopes independent of MQ itself and wants to
1124 * re-use the pointers internal to @a env. Use with caution.
1126 * @param[in|out] env_head of envelope DLL
1127 * @param[in|out] env_tail tail of envelope DLL
1128 * @param[in|out] env element to remove from the DLL
1131 GNUNET_MQ_dll_remove (struct GNUNET_MQ_Envelope **env_head,
1132 struct GNUNET_MQ_Envelope **env_tail,
1133 struct GNUNET_MQ_Envelope *env)
1135 GNUNET_CONTAINER_DLL_remove (*env_head,
1142 * Copy an array of handlers.
1144 * Useful if the array has been delared in local memory and needs to be
1145 * persisted for future use.
1147 * @param handlers Array of handlers to be copied. Can be NULL (nothing done).
1148 * @return A newly allocated array of handlers.
1149 * Needs to be freed with #GNUNET_free.
1151 struct GNUNET_MQ_MessageHandler *
1152 GNUNET_MQ_copy_handlers (const struct GNUNET_MQ_MessageHandler *handlers)
1154 struct GNUNET_MQ_MessageHandler *copy;
1157 if (NULL == handlers)
1160 count = GNUNET_MQ_count_handlers (handlers);
1161 copy = GNUNET_new_array (count + 1,
1162 struct GNUNET_MQ_MessageHandler);
1163 GNUNET_memcpy (copy,
1165 count * sizeof (struct GNUNET_MQ_MessageHandler));
1171 * Count the handlers in a handler array.
1173 * @param handlers Array of handlers to be counted.
1174 * @return The number of handlers in the array.
1177 GNUNET_MQ_count_handlers (const struct GNUNET_MQ_MessageHandler *handlers)
1181 if (NULL == handlers)
1184 for (i=0; NULL != handlers[i].cb; i++) ;