2 This file is part of GNUnet.
3 Copyright (C) 2012-2014 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, "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_MQ_NotifyCallback 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 * Implementation-specific state for connection to
206 * client (MQ for server).
208 struct ServerClientSocketState
211 * Handle of the client that connected to the server.
213 struct GNUNET_SERVER_Client *client;
216 * Active transmission request to the client.
218 struct GNUNET_SERVER_TransmitHandle *th;
223 * Call the message message handler that was registered
224 * for the type of the given message in the given message queue.
226 * This function is indended to be used for the implementation
229 * @param mq message queue with the handlers
230 * @param mh message to dispatch
233 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
234 const struct GNUNET_MessageHeader *mh)
236 const struct GNUNET_MQ_MessageHandler *handler;
237 int handled = GNUNET_NO;
238 uint16_t ms = ntohs (mh->size);
240 if (NULL == mq->handlers)
242 for (handler = mq->handlers; NULL != handler->cb; handler++)
244 if (handler->type == ntohs (mh->type))
246 handled = GNUNET_YES;
247 if ( (handler->expected_size > ms) ||
248 ( (handler->expected_size != ms) &&
249 (NULL == handler->mv) ) )
251 /* Too small, or not an exact size and
252 no 'mv' handler to check rest */
253 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
254 "Received malformed message of type %u\n",
255 (unsigned int) handler->type);
256 GNUNET_MQ_inject_error (mq,
257 GNUNET_MQ_ERROR_MALFORMED);
260 if ( (NULL == handler->mv) ||
262 handler->mv (handler->cls, mh)) )
264 /* message well-formed, pass to handler */
265 handler->cb (handler->cls, mh);
269 /* Message rejected by check routine */
270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
271 "Received malformed message of type %u\n",
272 (unsigned int) handler->type);
273 GNUNET_MQ_inject_error (mq,
274 GNUNET_MQ_ERROR_MALFORMED);
280 if (GNUNET_NO == handled)
281 LOG (GNUNET_ERROR_TYPE_INFO,
282 "No handler for message of type %d and size %d\n",
289 * Call the error handler of a message queue with the given
290 * error code. If there is no error handler, log a warning.
292 * This function is intended to be used by the implementation
295 * @param mq message queue
296 * @param error the error type
299 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
300 enum GNUNET_MQ_Error error)
302 if (NULL == mq->error_handler)
304 LOG (GNUNET_ERROR_TYPE_WARNING,
305 "Got error %d, but no handler installed\n",
309 mq->error_handler (mq->error_handler_cls,
315 * Discard the message queue message, free all
316 * allocated resources. Must be called in the event
317 * that a message is created but should not actually be sent.
319 * @param mqm the message to discard
322 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *ev)
324 GNUNET_assert (NULL == ev->parent_queue);
330 * Obtain the current length of the message queue.
332 * @param mq queue to inspect
333 * @return number of queued, non-transmitted messages
336 GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq)
338 if (GNUNET_YES != mq->in_flight)
340 return mq->queue_length;
342 return mq->queue_length - 1;
347 * Send a message with the given message queue.
348 * May only be called once per message.
350 * @param mq message queue
351 * @param ev the envelope with the message to send.
354 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
355 struct GNUNET_MQ_Envelope *ev)
357 GNUNET_assert (NULL != mq);
358 GNUNET_assert (NULL == ev->parent_queue);
361 ev->parent_queue = mq;
362 /* is the implementation busy? queue it! */
363 if ( (NULL != mq->current_envelope) ||
364 (NULL != mq->send_task) )
366 GNUNET_CONTAINER_DLL_insert_tail (mq->envelope_head,
371 mq->current_envelope = ev;
379 * Send a copy of a message with the given message queue.
380 * Can be called repeatedly on the same envelope.
382 * @param mq message queue
383 * @param ev the envelope with the message to send.
386 GNUNET_MQ_send_copy (struct GNUNET_MQ_Handle *mq,
387 const struct GNUNET_MQ_Envelope *ev)
389 struct GNUNET_MQ_Envelope *env;
392 msize = ntohs (ev->mh->size);
393 env = GNUNET_malloc (sizeof (struct GNUNET_MQ_Envelope) +
395 env->mh = (struct GNUNET_MessageHeader *) &env[1];
396 env->sent_cb = ev->sent_cb;
397 env->sent_cls = ev->sent_cls;
398 GNUNET_memcpy (&env[1],
407 * Task run to call the send implementation for the next queued
408 * message, if any. Only useful for implementing message queues,
409 * results in undefined behavior if not used carefully.
411 * @param cls message queue to send the next message with
414 impl_send_continue (void *cls)
416 struct GNUNET_MQ_Handle *mq = cls;
418 mq->send_task = NULL;
419 /* call is only valid if we're actually currently sending
421 if (NULL == mq->envelope_head)
423 mq->current_envelope = mq->envelope_head;
424 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
426 mq->current_envelope);
428 mq->current_envelope->mh,
434 * Call the send implementation for the next queued message, if any.
435 * Only useful for implementing message queues, results in undefined
436 * behavior if not used carefully.
438 * @param mq message queue to send the next message with
441 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq)
443 struct GNUNET_MQ_Envelope *current_envelope;
444 GNUNET_MQ_NotifyCallback cb;
446 GNUNET_assert (0 < mq->queue_length);
448 current_envelope = mq->current_envelope;
449 current_envelope->parent_queue = NULL;
450 mq->current_envelope = NULL;
451 GNUNET_assert (NULL == mq->send_task);
452 mq->send_task = GNUNET_SCHEDULER_add_now (&impl_send_continue,
454 if (NULL != (cb = current_envelope->sent_cb))
456 current_envelope->sent_cb = NULL;
457 cb (current_envelope->sent_cls);
459 GNUNET_free (current_envelope);
464 * Call the send notification for the current message, but do not
465 * try to send the next message until #GNUNET_MQ_impl_send_continue
468 * Only useful for implementing message queues, results in undefined
469 * behavior if not used carefully.
471 * @param mq message queue to send the next message with
474 GNUNET_MQ_impl_send_in_flight (struct GNUNET_MQ_Handle *mq)
476 struct GNUNET_MQ_Envelope *current_envelope;
477 GNUNET_MQ_NotifyCallback cb;
479 mq->in_flight = GNUNET_YES;
480 /* call is only valid if we're actually currently sending
482 current_envelope = mq->current_envelope;
483 GNUNET_assert (NULL != current_envelope);
484 /* can't call cancel from now on anymore */
485 current_envelope->parent_queue = NULL;
486 if (NULL != (cb = current_envelope->sent_cb))
488 current_envelope->sent_cb = NULL;
489 cb (current_envelope->sent_cls);
495 * Create a message queue for the specified handlers.
497 * @param send function the implements sending messages
498 * @param destroy function that implements destroying the queue
499 * @param cancel function that implements canceling a message
500 * @param impl_state for the queue, passed to 'send' and 'destroy'
501 * @param handlers array of message handlers
502 * @param error_handler handler for read and write errors
503 * @param error_handler_cls closure for @a error_handler
504 * @return a new message queue
506 struct GNUNET_MQ_Handle *
507 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
508 GNUNET_MQ_DestroyImpl destroy,
509 GNUNET_MQ_CancelImpl cancel,
511 const struct GNUNET_MQ_MessageHandler *handlers,
512 GNUNET_MQ_ErrorHandler error_handler,
513 void *error_handler_cls)
515 struct GNUNET_MQ_Handle *mq;
518 mq = GNUNET_new (struct GNUNET_MQ_Handle);
519 mq->send_impl = send;
520 mq->destroy_impl = destroy;
521 mq->cancel_impl = cancel;
522 if (NULL != handlers)
524 for (i=0;NULL != handlers[i].cb; i++) ;
525 mq->handlers = GNUNET_new_array (i + 1,
526 struct GNUNET_MQ_MessageHandler);
527 GNUNET_memcpy (mq->handlers,
529 i * sizeof (struct GNUNET_MQ_MessageHandler));
531 mq->error_handler = error_handler;
532 mq->error_handler_cls = error_handler_cls;
533 mq->impl_state = impl_state;
540 * Change the closure argument in all of the `handlers` of the
543 * @param mq to modify
544 * @param handlers_cls new closure to use
547 GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq,
552 if (NULL == mq->handlers)
554 for (i=0;NULL != mq->handlers[i].cb; i++)
555 mq->handlers[i].cls = handlers_cls;
560 * Get the message that should currently be sent.
561 * Fails if there is no current message.
562 * Only useful for implementing message queues,
563 * results in undefined behavior if not used carefully.
565 * @param mq message queue with the current message
566 * @return message to send, never NULL
568 const struct GNUNET_MessageHeader *
569 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq)
571 GNUNET_assert (NULL != mq->current_envelope);
572 GNUNET_assert (NULL != mq->current_envelope->mh);
573 return mq->current_envelope->mh;
578 * Get the implementation state associated with the
581 * While the GNUNET_MQ_Impl* callbacks receive the
582 * implementation state, continuations that are scheduled
583 * by the implementation function often only have one closure
584 * argument, with this function it is possible to get at the
585 * implementation state when only passing the GNUNET_MQ_Handle
588 * @param mq message queue with the current message
589 * @return message to send, never NULL
592 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq)
594 return mq->impl_state;
598 struct GNUNET_MQ_Envelope *
599 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
603 struct GNUNET_MQ_Envelope *ev;
605 ev = GNUNET_malloc (size + sizeof (struct GNUNET_MQ_Envelope));
606 ev->mh = (struct GNUNET_MessageHeader *) &ev[1];
607 ev->mh->size = htons (size);
608 ev->mh->type = htons (type);
616 * Create a new envelope by copying an existing message.
618 * @param hdr header of the message to copy
619 * @return envelope containing @a hdr
621 struct GNUNET_MQ_Envelope *
622 GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr)
624 struct GNUNET_MQ_Envelope *mqm;
625 uint16_t size = ntohs (hdr->size);
627 mqm = GNUNET_malloc (sizeof (*mqm) + size);
628 mqm->mh = (struct GNUNET_MessageHeader *) &mqm[1];
629 GNUNET_memcpy (mqm->mh,
637 * Implementation of the #GNUNET_MQ_msg_nested_mh macro.
639 * @param mhp pointer to the message header pointer that will be changed to allocate at
640 * the newly allocated space for the message.
641 * @param base_size size of the data before the nested message
642 * @param type type of the message in the envelope
643 * @param nested_mh the message to append to the message after base_size
645 struct GNUNET_MQ_Envelope *
646 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
649 const struct GNUNET_MessageHeader *nested_mh)
651 struct GNUNET_MQ_Envelope *mqm;
654 if (NULL == nested_mh)
655 return GNUNET_MQ_msg_ (mhp, base_size, type);
657 size = base_size + ntohs (nested_mh->size);
659 /* check for uint16_t overflow */
660 if (size < base_size)
663 mqm = GNUNET_MQ_msg_ (mhp, size, type);
664 GNUNET_memcpy ((char *) mqm->mh + base_size,
666 ntohs (nested_mh->size));
673 * Transmit a queued message to the session's client.
675 * @param cls consensus session
676 * @param size number of bytes available in @a buf
677 * @param buf where the callee should write the message
678 * @return number of bytes written to @a buf
681 transmit_queued (void *cls,
685 struct GNUNET_MQ_Handle *mq = cls;
686 struct ServerClientSocketState *state = GNUNET_MQ_impl_state (mq);
687 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
690 GNUNET_assert (NULL != buf);
691 msg_size = ntohs (msg->size);
692 GNUNET_assert (size >= msg_size);
693 GNUNET_memcpy (buf, msg, msg_size);
696 GNUNET_MQ_impl_send_continue (mq);
703 server_client_destroy_impl (struct GNUNET_MQ_Handle *mq,
706 struct ServerClientSocketState *state = impl_state;
708 if (NULL != state->th)
710 GNUNET_SERVER_notify_transmit_ready_cancel (state->th);
714 GNUNET_assert (NULL != mq);
715 GNUNET_assert (NULL != state);
716 GNUNET_SERVER_client_drop (state->client);
722 server_client_send_impl (struct GNUNET_MQ_Handle *mq,
723 const struct GNUNET_MessageHeader *msg,
726 struct ServerClientSocketState *state = impl_state;
728 GNUNET_assert (NULL != mq);
729 state->th = GNUNET_SERVER_notify_transmit_ready (state->client,
731 GNUNET_TIME_UNIT_FOREVER_REL,
732 &transmit_queued, mq);
736 struct GNUNET_MQ_Handle *
737 GNUNET_MQ_queue_for_server_client (struct GNUNET_SERVER_Client *client)
739 struct GNUNET_MQ_Handle *mq;
740 struct ServerClientSocketState *scss;
742 mq = GNUNET_new (struct GNUNET_MQ_Handle);
743 scss = GNUNET_new (struct ServerClientSocketState);
744 mq->impl_state = scss;
745 scss->client = client;
746 GNUNET_SERVER_client_keep (client);
747 mq->send_impl = &server_client_send_impl;
748 mq->destroy_impl = &server_client_destroy_impl;
754 * Associate the assoc_data in mq with a unique request id.
756 * @param mq message queue, id will be unique for the queue
757 * @param assoc_data to associate
760 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
765 if (NULL == mq->assoc_map)
767 mq->assoc_map = GNUNET_CONTAINER_multihashmap32_create (8);
771 GNUNET_CONTAINER_multihashmap32_put (mq->assoc_map, id, assoc_data,
772 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
778 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
781 if (NULL == mq->assoc_map)
783 return GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map, request_id);
788 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
793 if (NULL == mq->assoc_map)
795 val = GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map,
797 GNUNET_CONTAINER_multihashmap32_remove_all (mq->assoc_map,
804 * Call a callback once the envelope has been sent, that is,
805 * sending it can not be canceled anymore.
806 * There can be only one notify sent callback per envelope.
808 * @param ev message to call the notify callback for
809 * @param cb the notify callback
810 * @param cb_cls closure for the callback
813 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *mqm,
814 GNUNET_MQ_NotifyCallback cb,
818 mqm->sent_cls = cb_cls;
823 * Handle we return for callbacks registered to be
824 * notified when #GNUNET_MQ_destroy() is called on a queue.
826 struct GNUNET_MQ_DestroyNotificationHandle
831 struct GNUNET_MQ_DestroyNotificationHandle *prev;
836 struct GNUNET_MQ_DestroyNotificationHandle *next;
839 * Queue to notify about.
841 struct GNUNET_MQ_Handle *mq;
846 GNUNET_SCHEDULER_TaskCallback cb;
856 * Destroy the message queue.
858 * @param mq message queue to destroy
861 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq)
863 struct GNUNET_MQ_DestroyNotificationHandle *dnh;
865 if (NULL != mq->destroy_impl)
867 mq->destroy_impl (mq, mq->impl_state);
869 if (NULL != mq->send_task)
871 GNUNET_SCHEDULER_cancel (mq->send_task);
872 mq->send_task = NULL;
874 while (NULL != mq->envelope_head)
876 struct GNUNET_MQ_Envelope *ev;
878 ev = mq->envelope_head;
879 ev->parent_queue = NULL;
880 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
883 GNUNET_assert (0 < mq->queue_length);
885 GNUNET_MQ_discard (ev);
887 if (NULL != mq->current_envelope)
889 /* we can only discard envelopes that
891 mq->current_envelope->parent_queue = NULL;
892 GNUNET_MQ_discard (mq->current_envelope);
893 mq->current_envelope = NULL;
894 GNUNET_assert (0 < mq->queue_length);
897 GNUNET_assert (0 == mq->queue_length);
898 while (NULL != (dnh = mq->dnh_head))
900 dnh->cb (dnh->cb_cls);
901 GNUNET_MQ_destroy_notify_cancel (dnh);
903 if (NULL != mq->assoc_map)
905 GNUNET_CONTAINER_multihashmap32_destroy (mq->assoc_map);
906 mq->assoc_map = NULL;
908 GNUNET_free_non_null (mq->handlers);
913 const struct GNUNET_MessageHeader *
914 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
918 uint16_t nested_size;
919 const struct GNUNET_MessageHeader *nested_msg;
921 whole_size = ntohs (mh->size);
922 GNUNET_assert (whole_size >= base_size);
923 nested_size = whole_size - base_size;
924 if (0 == nested_size)
926 if (nested_size < sizeof (struct GNUNET_MessageHeader))
931 nested_msg = (const struct GNUNET_MessageHeader *) ((char *) mh + base_size);
932 if (ntohs (nested_msg->size) != nested_size)
942 * Cancel sending the message. Message must have been sent with
943 * #GNUNET_MQ_send before. May not be called after the notify sent
944 * callback has been called
946 * @param ev queued envelope to cancel
949 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev)
951 struct GNUNET_MQ_Handle *mq = ev->parent_queue;
953 GNUNET_assert (NULL != mq);
954 GNUNET_assert (NULL != mq->cancel_impl);
956 mq->evacuate_called = GNUNET_NO;
958 if (mq->current_envelope == ev)
960 // complex case, we already started with transmitting
962 GNUNET_assert (0 < mq->queue_length);
966 // continue sending the next message, if any
967 if (NULL == mq->envelope_head)
969 mq->current_envelope = NULL;
973 mq->current_envelope = mq->envelope_head;
974 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
976 mq->current_envelope);
978 mq->current_envelope->mh,
984 // simple case, message is still waiting in the queue
985 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
988 GNUNET_assert (0 < mq->queue_length);
992 if (GNUNET_YES != mq->evacuate_called)
994 ev->parent_queue = NULL;
1003 * Function to obtain the current envelope
1004 * from within #GNUNET_MQ_SendImpl implementations.
1006 * @param mq message queue to interrogate
1007 * @return the current envelope
1009 struct GNUNET_MQ_Envelope *
1010 GNUNET_MQ_get_current_envelope (struct GNUNET_MQ_Handle *mq)
1012 return mq->current_envelope;
1017 * Function to obtain the last envelope in the queue.
1019 * @param mq message queue to interrogate
1020 * @return the last envelope in the queue
1022 struct GNUNET_MQ_Envelope *
1023 GNUNET_MQ_get_last_envelope (struct GNUNET_MQ_Handle *mq)
1025 if (NULL != mq->envelope_tail)
1026 return mq->envelope_tail;
1028 return mq->current_envelope;
1033 * Set application-specific options for this envelope.
1034 * Overrides the options set for the queue with
1035 * #GNUNET_MQ_set_options() for this message only.
1037 * @param env message 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_env_set_options (struct GNUNET_MQ_Envelope *env,
1048 env->have_custom_options = GNUNET_YES;
1053 * Get application-specific options for this envelope.
1055 * @param env message to set options for
1056 * @param[out] flags set to flags to use (meaning is queue-specific)
1057 * @return extra additional buffer for further data (also queue-specific)
1060 GNUNET_MQ_env_get_options (struct GNUNET_MQ_Envelope *env,
1063 struct GNUNET_MQ_Handle *mq = env->parent_queue;
1065 if (GNUNET_YES == env->have_custom_options)
1067 *flags = env->flags;
1075 *flags = mq->default_flags;
1076 return mq->default_extra;
1081 * Set application-specific options for this queue.
1083 * @param mq message queue to set options for
1084 * @param flags flags to use (meaning is queue-specific)
1085 * @param extra additional buffer for further data (also queue-specific)
1088 GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq,
1092 mq->default_flags = flags;
1093 mq->default_extra = extra;
1098 * Register function to be called whenever @a mq is being
1101 * @param mq message queue to watch
1102 * @param cb function to call on @a mq destruction
1103 * @param cb_cls closure for @a cb
1104 * @return handle for #GNUNET_MQ_destroy_notify_cancel().
1106 struct GNUNET_MQ_DestroyNotificationHandle *
1107 GNUNET_MQ_destroy_notify (struct GNUNET_MQ_Handle *mq,
1108 GNUNET_SCHEDULER_TaskCallback cb,
1111 struct GNUNET_MQ_DestroyNotificationHandle *dnh;
1113 dnh = GNUNET_new (struct GNUNET_MQ_DestroyNotificationHandle);
1116 dnh->cb_cls = cb_cls;
1117 GNUNET_CONTAINER_DLL_insert (mq->dnh_head,
1125 * Cancel registration from #GNUNET_MQ_destroy_notify().
1127 * @param dnh handle for registration to cancel
1130 GNUNET_MQ_destroy_notify_cancel (struct GNUNET_MQ_DestroyNotificationHandle *dnh)
1132 struct GNUNET_MQ_Handle *mq = dnh->mq;
1134 GNUNET_CONTAINER_DLL_remove (mq->dnh_head,