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 //#include "execinfo.h"
35 // * Use lsof to generate file descriptor reports on select error?
36 // * (turn off for stable releases).
38 //#define USE_LSOF GNUNET_NO
41 // * Obtain trace information for all scheduler calls that schedule tasks.
43 //#define EXECINFO GNUNET_NO
46 // * Check each file descriptor before adding
48 //#define DEBUG_FDS GNUNET_NO
51 // * Depth of the traces collected via EXECINFO.
53 //#define MAX_TRACE_DEPTH 50
56 struct GNUNET_MQ_Envelope
59 * Messages are stored in a linked list.
60 * Each queue has its own list of envelopes.
62 struct GNUNET_MQ_Envelope *next;
65 * Messages are stored in a linked list
66 * Each queue has its own list of envelopes.
68 struct GNUNET_MQ_Envelope *prev;
71 * Actual allocated message header.
72 * The GNUNET_MQ_Envelope header is allocated at
73 * the end of the message.
75 struct GNUNET_MessageHeader *mh;
78 * Queue the message is queued in, NULL if message is not queued.
80 struct GNUNET_MQ_Handle *parent_queue;
83 * Called after the message was sent irrevocably.
85 GNUNET_SCHEDULER_TaskCallback sent_cb;
88 * Closure for @e sent_cb
93 * Flags that were set for this envelope by
94 * #GNUNET_MQ_env_set_options(). Only valid if
95 * @e have_custom_options is set.
100 * Additional options buffer set for this envelope by
101 * #GNUNET_MQ_env_set_options(). Only valid if
102 * @e have_custom_options is set.
107 * Did the application call #GNUNET_MQ_env_set_options()?
109 int have_custom_options;
114 * Handle to a message queue.
116 struct GNUNET_MQ_Handle
119 * Handlers array, or NULL if the queue should not receive messages
121 struct GNUNET_MQ_MessageHandler *handlers;
124 * Actual implementation of message sending,
125 * called when a message is added
127 GNUNET_MQ_SendImpl send_impl;
130 * Implementation-dependent queue destruction function
132 GNUNET_MQ_DestroyImpl destroy_impl;
135 * Implementation-dependent send cancel function
137 GNUNET_MQ_CancelImpl cancel_impl;
140 * Implementation-specific state
145 * Callback will be called when an error occurs.
147 GNUNET_MQ_ErrorHandler error_handler;
150 * Closure for the error handler.
152 void *error_handler_cls;
155 * Task to asynchronously run #impl_send_continue().
157 struct GNUNET_SCHEDULER_Task *send_task;
160 * Linked list of messages pending to be sent
162 struct GNUNET_MQ_Envelope *envelope_head;
165 * Linked list of messages pending to be sent
167 struct GNUNET_MQ_Envelope *envelope_tail;
170 * Message that is currently scheduled to be
171 * sent. Not the head of the message queue, as the implementation
172 * needs to know if sending has been already scheduled or not.
174 struct GNUNET_MQ_Envelope *current_envelope;
177 * Map of associations, lazily allocated
179 struct GNUNET_CONTAINER_MultiHashMap32 *assoc_map;
182 * Functions to call on queue destruction; kept in a DLL.
184 struct GNUNET_MQ_DestroyNotificationHandle *dnh_head;
187 * Functions to call on queue destruction; kept in a DLL.
189 struct GNUNET_MQ_DestroyNotificationHandle *dnh_tail;
192 * Additional options buffer set for this queue by
193 * #GNUNET_MQ_set_options(). Default is 0.
195 const void *default_extra;
198 * Flags that were set for this queue by
199 * #GNUNET_MQ_set_options(). Default is 0.
201 uint64_t default_flags;
204 * Next id that should be used for the @e assoc_map,
205 * initialized lazily to a random value together with
211 * Number of entries we have in the envelope-DLL.
213 unsigned int queue_length;
216 * #GNUNET_YES if GNUNET_MQ_impl_evacuate was called.
217 * FIXME: is this dead?
222 * #GNUNET_YES if GNUNET_MQ_impl_send_in_flight() was called.
229 * Call the message message handler that was registered
230 * for the type of the given message in the given message queue.
232 * This function is indended to be used for the implementation
235 * @param mq message queue with the handlers
236 * @param mh message to dispatch
239 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
240 const struct GNUNET_MessageHeader *mh)
242 const struct GNUNET_MQ_MessageHandler *handler;
243 int handled = GNUNET_NO;
244 uint16_t msize = ntohs (mh->size);
245 uint16_t mtype = ntohs (mh->type);
247 LOG (GNUNET_ERROR_TYPE_DEBUG,
248 "Queue %p received message of type %u and size %u\n",
253 if (NULL == mq->handlers)
255 for (handler = mq->handlers; NULL != handler->cb; handler++)
257 if (handler->type == mtype)
259 handled = GNUNET_YES;
260 if ( (handler->expected_size > msize) ||
261 ( (handler->expected_size != msize) &&
262 (NULL == handler->mv) ) )
264 /* Too small, or not an exact size and
265 no 'mv' handler to check rest */
266 LOG (GNUNET_ERROR_TYPE_ERROR,
267 "Received malformed message of type %u\n",
268 (unsigned int) handler->type);
269 GNUNET_MQ_inject_error (mq,
270 GNUNET_MQ_ERROR_MALFORMED);
273 if ( (NULL == handler->mv) ||
275 handler->mv (handler->cls, mh)) )
277 /* message well-formed, pass to handler */
278 handler->cb (handler->cls, mh);
282 /* Message rejected by check routine */
283 LOG (GNUNET_ERROR_TYPE_ERROR,
284 "Received malformed message of type %u\n",
285 (unsigned int) handler->type);
286 GNUNET_MQ_inject_error (mq,
287 GNUNET_MQ_ERROR_MALFORMED);
293 if (GNUNET_NO == handled)
294 LOG (GNUNET_ERROR_TYPE_INFO,
295 "No handler for message of type %u and size %u\n",
301 * Call the error handler of a message queue with the given
302 * error code. If there is no error handler, log a warning.
304 * This function is intended to be used by the implementation
307 * @param mq message queue
308 * @param error the error type
311 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
312 enum GNUNET_MQ_Error error)
314 if (NULL == mq->error_handler)
316 LOG (GNUNET_ERROR_TYPE_WARNING,
317 "Got error %d, but no handler installed\n",
321 mq->error_handler (mq->error_handler_cls,
327 * Discard the message queue message, free all
328 * allocated resources. Must be called in the event
329 * that a message is created but should not actually be sent.
331 * @param mqm the message to discard
334 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *ev)
336 GNUNET_assert (NULL == ev->parent_queue);
342 * Obtain the current length of the message queue.
344 * @param mq queue to inspect
345 * @return number of queued, non-transmitted messages
348 GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq)
350 if (GNUNET_YES != mq->in_flight)
352 return mq->queue_length;
354 return mq->queue_length - 1;
359 * Send a message with the given message queue.
360 * May only be called once per message.
362 * @param mq message queue
363 * @param ev the envelope with the message to send.
366 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
367 struct GNUNET_MQ_Envelope *ev)
369 GNUNET_assert (NULL != mq);
370 GNUNET_assert (NULL == ev->parent_queue);
373 GNUNET_break (mq->queue_length < 10000); /* This would seem like a bug... */
374 ev->parent_queue = mq;
375 /* is the implementation busy? queue it! */
376 if ( (NULL != mq->current_envelope) ||
377 (NULL != mq->send_task) )
379 GNUNET_CONTAINER_DLL_insert_tail (mq->envelope_head,
384 GNUNET_assert (NULL == mq->envelope_head);
385 mq->current_envelope = ev;
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
388 "mq: sending message of type %u, queue empty (MQ: %p)\n",
399 * Remove the first envelope that has not yet been sent from the message
400 * queue and return it.
402 * @param mq queue to remove envelope from
403 * @return NULL if queue is empty (or has no envelope that is not under transmission)
405 struct GNUNET_MQ_Envelope *
406 GNUNET_MQ_unsent_head (struct GNUNET_MQ_Handle *mq)
408 struct GNUNET_MQ_Envelope *env;
410 env = mq->envelope_head;
411 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
415 env->parent_queue = NULL;
421 * Function to copy an envelope. The envelope must not yet
422 * be in any queue or have any options or callbacks set.
424 * @param env envelope to copy
425 * @return copy of @a env
427 struct GNUNET_MQ_Envelope *
428 GNUNET_MQ_env_copy (struct GNUNET_MQ_Envelope *env)
430 GNUNET_assert (NULL == env->next);
431 GNUNET_assert (NULL == env->parent_queue);
432 GNUNET_assert (NULL == env->sent_cb);
433 GNUNET_assert (GNUNET_NO == env->have_custom_options);
434 return GNUNET_MQ_msg_copy (env->mh);
439 * Send a copy of a message with the given message queue.
440 * Can be called repeatedly on the same envelope.
442 * @param mq message queue
443 * @param ev the envelope with the message to send.
446 GNUNET_MQ_send_copy (struct GNUNET_MQ_Handle *mq,
447 const struct GNUNET_MQ_Envelope *ev)
449 struct GNUNET_MQ_Envelope *env;
452 msize = ntohs (ev->mh->size);
453 env = GNUNET_malloc (sizeof (struct GNUNET_MQ_Envelope) +
455 env->mh = (struct GNUNET_MessageHeader *) &env[1];
456 env->sent_cb = ev->sent_cb;
457 env->sent_cls = ev->sent_cls;
458 GNUNET_memcpy (&env[1],
467 * Task run to call the send implementation for the next queued
468 * message, if any. Only useful for implementing message queues,
469 * results in undefined behavior if not used carefully.
471 * @param cls message queue to send the next message with
474 impl_send_continue (void *cls)
476 struct GNUNET_MQ_Handle *mq = cls;
478 mq->send_task = NULL;
479 /* call is only valid if we're actually currently sending
481 if (NULL == mq->envelope_head)
483 mq->current_envelope = mq->envelope_head;
484 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
486 mq->current_envelope);
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
489 "mq: sending message of type %u from queue\n",
490 ntohs(mq->current_envelope->mh->type));
493 mq->current_envelope->mh,
499 * Call the send implementation for the next queued message, if any.
500 * Only useful for implementing message queues, results in undefined
501 * behavior if not used carefully.
503 * @param mq message queue to send the next message with
506 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq)
508 struct GNUNET_MQ_Envelope *current_envelope;
509 GNUNET_SCHEDULER_TaskCallback cb;
511 GNUNET_assert (0 < mq->queue_length);
513 mq->in_flight = GNUNET_NO;
514 current_envelope = mq->current_envelope;
515 current_envelope->parent_queue = NULL;
516 mq->current_envelope = NULL;
517 GNUNET_assert (NULL == mq->send_task);
518 mq->send_task = GNUNET_SCHEDULER_add_now (&impl_send_continue,
520 if (NULL != (cb = current_envelope->sent_cb))
522 current_envelope->sent_cb = NULL;
523 cb (current_envelope->sent_cls);
525 GNUNET_free (current_envelope);
530 * Call the send notification for the current message, but do not
531 * try to send the next message until #GNUNET_MQ_impl_send_continue
534 * Only useful for implementing message queues, results in undefined
535 * behavior if not used carefully.
537 * @param mq message queue to send the next message with
540 GNUNET_MQ_impl_send_in_flight (struct GNUNET_MQ_Handle *mq)
542 struct GNUNET_MQ_Envelope *current_envelope;
543 GNUNET_SCHEDULER_TaskCallback cb;
545 mq->in_flight = GNUNET_YES;
546 /* call is only valid if we're actually currently sending
548 current_envelope = mq->current_envelope;
549 GNUNET_assert (NULL != current_envelope);
550 /* can't call cancel from now on anymore */
551 current_envelope->parent_queue = NULL;
552 if (NULL != (cb = current_envelope->sent_cb))
554 current_envelope->sent_cb = NULL;
555 cb (current_envelope->sent_cls);
561 * Create a message queue for the specified handlers.
563 * @param send function the implements sending messages
564 * @param destroy function that implements destroying the queue
565 * @param cancel function that implements canceling a message
566 * @param impl_state for the queue, passed to 'send' and 'destroy'
567 * @param handlers array of message handlers
568 * @param error_handler handler for read and write errors
569 * @param error_handler_cls closure for @a error_handler
570 * @return a new message queue
572 struct GNUNET_MQ_Handle *
573 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
574 GNUNET_MQ_DestroyImpl destroy,
575 GNUNET_MQ_CancelImpl cancel,
577 const struct GNUNET_MQ_MessageHandler *handlers,
578 GNUNET_MQ_ErrorHandler error_handler,
579 void *error_handler_cls)
581 struct GNUNET_MQ_Handle *mq;
583 mq = GNUNET_new (struct GNUNET_MQ_Handle);
584 mq->send_impl = send;
585 mq->destroy_impl = destroy;
586 mq->cancel_impl = cancel;
587 mq->handlers = GNUNET_MQ_copy_handlers (handlers);
588 mq->error_handler = error_handler;
589 mq->error_handler_cls = error_handler_cls;
590 mq->impl_state = impl_state;
597 * Change the closure argument in all of the `handlers` of the
600 * @param mq to modify
601 * @param handlers_cls new closure to use
604 GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq,
609 if (NULL == mq->handlers)
611 for (i=0;NULL != mq->handlers[i].cb; i++)
612 mq->handlers[i].cls = handlers_cls;
617 * Get the message that should currently be sent.
618 * Fails if there is no current message.
619 * Only useful for implementing message queues,
620 * results in undefined behavior if not used carefully.
622 * @param mq message queue with the current message
623 * @return message to send, never NULL
625 const struct GNUNET_MessageHeader *
626 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq)
628 GNUNET_assert (NULL != mq->current_envelope);
629 GNUNET_assert (NULL != mq->current_envelope->mh);
630 return mq->current_envelope->mh;
635 * Get the implementation state associated with the
638 * While the GNUNET_MQ_Impl* callbacks receive the
639 * implementation state, continuations that are scheduled
640 * by the implementation function often only have one closure
641 * argument, with this function it is possible to get at the
642 * implementation state when only passing the GNUNET_MQ_Handle
645 * @param mq message queue with the current message
646 * @return message to send, never NULL
649 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq)
651 return mq->impl_state;
655 struct GNUNET_MQ_Envelope *
656 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
660 struct GNUNET_MQ_Envelope *ev;
662 ev = GNUNET_malloc (size + sizeof (struct GNUNET_MQ_Envelope));
663 ev->mh = (struct GNUNET_MessageHeader *) &ev[1];
664 ev->mh->size = htons (size);
665 ev->mh->type = htons (type);
673 * Create a new envelope by copying an existing message.
675 * @param hdr header of the message to copy
676 * @return envelope containing @a hdr
678 struct GNUNET_MQ_Envelope *
679 GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr)
681 struct GNUNET_MQ_Envelope *mqm;
682 uint16_t size = ntohs (hdr->size);
684 mqm = GNUNET_malloc (sizeof (*mqm) + size);
685 mqm->mh = (struct GNUNET_MessageHeader *) &mqm[1];
686 GNUNET_memcpy (mqm->mh,
694 * Implementation of the #GNUNET_MQ_msg_nested_mh macro.
696 * @param mhp pointer to the message header pointer that will be changed to allocate at
697 * the newly allocated space for the message.
698 * @param base_size size of the data before the nested message
699 * @param type type of the message in the envelope
700 * @param nested_mh the message to append to the message after base_size
702 struct GNUNET_MQ_Envelope *
703 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
706 const struct GNUNET_MessageHeader *nested_mh)
708 struct GNUNET_MQ_Envelope *mqm;
711 if (NULL == nested_mh)
712 return GNUNET_MQ_msg_ (mhp, base_size, type);
714 size = base_size + ntohs (nested_mh->size);
716 /* check for uint16_t overflow */
717 if (size < base_size)
720 mqm = GNUNET_MQ_msg_ (mhp, size, type);
721 GNUNET_memcpy ((char *) mqm->mh + base_size,
723 ntohs (nested_mh->size));
730 * Associate the assoc_data in mq with a unique request id.
732 * @param mq message queue, id will be unique for the queue
733 * @param assoc_data to associate
736 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
741 if (NULL == mq->assoc_map)
743 mq->assoc_map = GNUNET_CONTAINER_multihashmap32_create (8);
747 GNUNET_assert (GNUNET_OK ==
748 GNUNET_CONTAINER_multihashmap32_put (mq->assoc_map,
751 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
757 * Get the data associated with a @a request_id in a queue
759 * @param mq the message queue with the association
760 * @param request_id the request id we are interested in
761 * @return the associated data
764 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
767 if (NULL == mq->assoc_map)
769 return GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map,
775 * Remove the association for a @a request_id
777 * @param mq the message queue with the association
778 * @param request_id the request id we want to remove
779 * @return the associated data
782 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
787 if (NULL == mq->assoc_map)
789 val = GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map,
791 GNUNET_CONTAINER_multihashmap32_remove_all (mq->assoc_map,
798 * Call a callback once the envelope has been sent, that is,
799 * sending it can not be canceled anymore.
800 * There can be only one notify sent callback per envelope.
802 * @param ev message to call the notify callback for
803 * @param cb the notify callback
804 * @param cb_cls closure for the callback
807 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
808 GNUNET_SCHEDULER_TaskCallback cb,
811 GNUNET_assert (NULL == ev->sent_cb);
813 ev->sent_cls = cb_cls;
818 * Handle we return for callbacks registered to be
819 * notified when #GNUNET_MQ_destroy() is called on a queue.
821 struct GNUNET_MQ_DestroyNotificationHandle
826 struct GNUNET_MQ_DestroyNotificationHandle *prev;
831 struct GNUNET_MQ_DestroyNotificationHandle *next;
834 * Queue to notify about.
836 struct GNUNET_MQ_Handle *mq;
841 GNUNET_SCHEDULER_TaskCallback cb;
851 * Destroy the message queue.
853 * @param mq message queue to destroy
856 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq)
858 struct GNUNET_MQ_DestroyNotificationHandle *dnh;
860 if (NULL != mq->destroy_impl)
862 mq->destroy_impl (mq, mq->impl_state);
864 if (NULL != mq->send_task)
866 GNUNET_SCHEDULER_cancel (mq->send_task);
867 mq->send_task = NULL;
869 while (NULL != mq->envelope_head)
871 struct GNUNET_MQ_Envelope *ev;
873 ev = mq->envelope_head;
874 ev->parent_queue = NULL;
875 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
878 GNUNET_assert (0 < mq->queue_length);
880 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
881 "MQ destroy drops message of type %u\n",
882 ntohs (ev->mh->type));
883 GNUNET_MQ_discard (ev);
885 if (NULL != mq->current_envelope)
887 /* we can only discard envelopes that
889 mq->current_envelope->parent_queue = NULL;
890 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
891 "MQ destroy drops current message of type %u\n",
892 ntohs (mq->current_envelope->mh->type));
894 // void *backtrace_array[MAX_TRACE_DEPTH];
895 // int num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
896 // char **backtrace_strings =
897 // backtrace_symbols (backtrace_array,
898 // t->num_backtrace_strings);
899 // for (unsigned int i = 0; i < num_backtrace_strings; i++)
900 // LOG (GNUNET_ERROR_TYPE_DEBUG,
901 // "client drop trace %u: %s\n",
903 // backtrace_strings[i]);
905 GNUNET_MQ_discard (mq->current_envelope);
906 mq->current_envelope = NULL;
907 GNUNET_assert (0 < mq->queue_length);
910 GNUNET_assert (0 == mq->queue_length);
911 while (NULL != (dnh = mq->dnh_head))
913 dnh->cb (dnh->cb_cls);
914 GNUNET_MQ_destroy_notify_cancel (dnh);
916 if (NULL != mq->assoc_map)
918 GNUNET_CONTAINER_multihashmap32_destroy (mq->assoc_map);
919 mq->assoc_map = NULL;
921 GNUNET_free_non_null (mq->handlers);
926 const struct GNUNET_MessageHeader *
927 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
931 uint16_t nested_size;
932 const struct GNUNET_MessageHeader *nested_msg;
934 whole_size = ntohs (mh->size);
935 GNUNET_assert (whole_size >= base_size);
936 nested_size = whole_size - base_size;
937 if (0 == nested_size)
939 if (nested_size < sizeof (struct GNUNET_MessageHeader))
944 nested_msg = (const struct GNUNET_MessageHeader *) ((char *) mh + base_size);
945 if (ntohs (nested_msg->size) != nested_size)
955 * Cancel sending the message. Message must have been sent with
956 * #GNUNET_MQ_send before. May not be called after the notify sent
957 * callback has been called
959 * @param ev queued envelope to cancel
962 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev)
964 struct GNUNET_MQ_Handle *mq = ev->parent_queue;
966 GNUNET_assert (NULL != mq);
967 GNUNET_assert (NULL != mq->cancel_impl);
969 mq->evacuate_called = GNUNET_NO;
971 if (mq->current_envelope == ev)
973 /* complex case, we already started with transmitting
974 the message using the callbacks. */
975 GNUNET_assert (0 < mq->queue_length);
979 /* continue sending the next message, if any */
980 mq->current_envelope = mq->envelope_head;
981 if (NULL != mq->current_envelope)
983 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
985 mq->current_envelope);
987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
988 "mq: sending canceled message of type %u queue\n",
989 ntohs(ev->mh->type));
992 mq->current_envelope->mh,
998 /* simple case, message is still waiting in the queue */
999 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
1002 GNUNET_assert (0 < mq->queue_length);
1006 if (GNUNET_YES != mq->evacuate_called)
1008 ev->parent_queue = NULL;
1017 * Function to obtain the current envelope
1018 * from within #GNUNET_MQ_SendImpl implementations.
1020 * @param mq message queue to interrogate
1021 * @return the current envelope
1023 struct GNUNET_MQ_Envelope *
1024 GNUNET_MQ_get_current_envelope (struct GNUNET_MQ_Handle *mq)
1026 return mq->current_envelope;
1031 * Function to obtain the last envelope in the queue.
1033 * @param mq message queue to interrogate
1034 * @return the last envelope in the queue
1036 struct GNUNET_MQ_Envelope *
1037 GNUNET_MQ_get_last_envelope (struct GNUNET_MQ_Handle *mq)
1039 if (NULL != mq->envelope_tail)
1040 return mq->envelope_tail;
1042 return mq->current_envelope;
1047 * Set application-specific options for this envelope.
1048 * Overrides the options set for the queue with
1049 * #GNUNET_MQ_set_options() for this message only.
1051 * @param env message to set options for
1052 * @param flags flags to use (meaning is queue-specific)
1053 * @param extra additional buffer for further data (also queue-specific)
1056 GNUNET_MQ_env_set_options (struct GNUNET_MQ_Envelope *env,
1062 env->have_custom_options = GNUNET_YES;
1067 * Get application-specific options for this envelope.
1069 * @param env message to set options for
1070 * @param[out] flags set to flags to use (meaning is queue-specific)
1071 * @return extra additional buffer for further data (also queue-specific)
1074 GNUNET_MQ_env_get_options (struct GNUNET_MQ_Envelope *env,
1077 struct GNUNET_MQ_Handle *mq = env->parent_queue;
1079 if (GNUNET_YES == env->have_custom_options)
1081 *flags = env->flags;
1089 *flags = mq->default_flags;
1090 return mq->default_extra;
1095 * Set application-specific options for this queue.
1097 * @param mq message queue to set options for
1098 * @param flags flags to use (meaning is queue-specific)
1099 * @param extra additional buffer for further data (also queue-specific)
1102 GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq,
1106 mq->default_flags = flags;
1107 mq->default_extra = extra;
1112 * Register function to be called whenever @a mq is being
1115 * @param mq message queue to watch
1116 * @param cb function to call on @a mq destruction
1117 * @param cb_cls closure for @a cb
1118 * @return handle for #GNUNET_MQ_destroy_notify_cancel().
1120 struct GNUNET_MQ_DestroyNotificationHandle *
1121 GNUNET_MQ_destroy_notify (struct GNUNET_MQ_Handle *mq,
1122 GNUNET_SCHEDULER_TaskCallback cb,
1125 struct GNUNET_MQ_DestroyNotificationHandle *dnh;
1127 dnh = GNUNET_new (struct GNUNET_MQ_DestroyNotificationHandle);
1130 dnh->cb_cls = cb_cls;
1131 GNUNET_CONTAINER_DLL_insert (mq->dnh_head,
1139 * Cancel registration from #GNUNET_MQ_destroy_notify().
1141 * @param dnh handle for registration to cancel
1144 GNUNET_MQ_destroy_notify_cancel (struct GNUNET_MQ_DestroyNotificationHandle *dnh)
1146 struct GNUNET_MQ_Handle *mq = dnh->mq;
1148 GNUNET_CONTAINER_DLL_remove (mq->dnh_head,
1156 * Insert @a env into the envelope DLL starting at @a env_head
1157 * Note that @a env must not be in any MQ while this function
1158 * is used with DLLs defined outside of the MQ module. This
1159 * is just in case some application needs to also manage a
1160 * FIFO of envelopes independent of MQ itself and wants to
1161 * re-use the pointers internal to @a env. Use with caution.
1163 * @param[in|out] env_head of envelope DLL
1164 * @param[in|out] env_tail tail of envelope DLL
1165 * @param[in|out] env element to insert at the tail
1168 GNUNET_MQ_dll_insert_tail (struct GNUNET_MQ_Envelope **env_head,
1169 struct GNUNET_MQ_Envelope **env_tail,
1170 struct GNUNET_MQ_Envelope *env)
1172 GNUNET_CONTAINER_DLL_insert_tail (*env_head,
1179 * Remove @a env from the envelope DLL starting at @a env_head.
1180 * Note that @a env must not be in any MQ while this function
1181 * is used with DLLs defined outside of the MQ module. This
1182 * is just in case some application needs to also manage a
1183 * FIFO of envelopes independent of MQ itself and wants to
1184 * re-use the pointers internal to @a env. Use with caution.
1186 * @param[in|out] env_head of envelope DLL
1187 * @param[in|out] env_tail tail of envelope DLL
1188 * @param[in|out] env element to remove from the DLL
1191 GNUNET_MQ_dll_remove (struct GNUNET_MQ_Envelope **env_head,
1192 struct GNUNET_MQ_Envelope **env_tail,
1193 struct GNUNET_MQ_Envelope *env)
1195 GNUNET_CONTAINER_DLL_remove (*env_head,
1202 * Copy an array of handlers.
1204 * Useful if the array has been delared in local memory and needs to be
1205 * persisted for future use.
1207 * @param handlers Array of handlers to be copied. Can be NULL (nothing done).
1208 * @return A newly allocated array of handlers.
1209 * Needs to be freed with #GNUNET_free.
1211 struct GNUNET_MQ_MessageHandler *
1212 GNUNET_MQ_copy_handlers (const struct GNUNET_MQ_MessageHandler *handlers)
1214 struct GNUNET_MQ_MessageHandler *copy;
1217 if (NULL == handlers)
1220 count = GNUNET_MQ_count_handlers (handlers);
1221 copy = GNUNET_new_array (count + 1,
1222 struct GNUNET_MQ_MessageHandler);
1223 GNUNET_memcpy (copy,
1225 count * sizeof (struct GNUNET_MQ_MessageHandler));
1231 * Count the handlers in a handler array.
1233 * @param handlers Array of handlers to be counted.
1234 * @return The number of handlers in the array.
1237 GNUNET_MQ_count_handlers (const struct GNUNET_MQ_MessageHandler *handlers)
1241 if (NULL == handlers)
1244 for (i=0; NULL != handlers[i].cb; i++) ;