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 * usually points to the end of the containing GNUNET_MQ_Envelope
50 struct GNUNET_MessageHeader *mh;
53 * Queue the message is queued in, NULL if message is not queued.
55 struct GNUNET_MQ_Handle *parent_queue;
58 * Called after the message was sent irrevocably.
60 GNUNET_MQ_NotifyCallback sent_cb;
63 * Closure for @e send_cb
68 * Flags that were set for this envelope by
69 * #GNUNET_MQ_env_set_options(). Only valid if
70 * @e have_custom_options is set.
75 * Additional options buffer set for this envelope by
76 * #GNUNET_MQ_env_set_options(). Only valid if
77 * @e have_custom_options is set.
82 * Did the application call #GNUNET_MQ_env_set_options()?
84 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 * Linked list of messages pending to be sent
133 struct GNUNET_MQ_Envelope *envelope_head;
136 * Linked list of messages pending to be sent
138 struct GNUNET_MQ_Envelope *envelope_tail;
141 * Message that is currently scheduled to be
142 * sent. Not the head of the message queue, as the implementation
143 * needs to know if sending has been already scheduled or not.
145 struct GNUNET_MQ_Envelope *current_envelope;
148 * Map of associations, lazily allocated
150 struct GNUNET_CONTAINER_MultiHashMap32 *assoc_map;
153 * Task scheduled during #GNUNET_MQ_impl_send_continue.
155 struct GNUNET_SCHEDULER_Task *continue_task;
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;
194 * Implementation-specific state for connection to
195 * client (MQ for server).
197 struct ServerClientSocketState
200 * Handle of the client that connected to the server.
202 struct GNUNET_SERVER_Client *client;
205 * Active transmission request to the client.
207 struct GNUNET_SERVER_TransmitHandle *th;
212 * Implementation-specific state for connection to
213 * service (MQ for clients).
215 struct ClientConnectionState
218 * Did we call receive alread alreadyy?
223 * Do we also want to receive?
225 int receive_requested;
228 * Connection to the service.
230 struct GNUNET_CLIENT_Connection *connection;
233 * Active transmission request (or NULL).
235 struct GNUNET_CLIENT_TransmitHandle *th;
240 * Call the message message handler that was registered
241 * for the type of the given message in the given message queue.
243 * This function is indended to be used for the implementation
246 * @param mq message queue with the handlers
247 * @param mh message to dispatch
250 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
251 const struct GNUNET_MessageHeader *mh)
253 const struct GNUNET_MQ_MessageHandler *handler;
254 int handled = GNUNET_NO;
255 uint16_t ms = ntohs (mh->size);
257 if (NULL == mq->handlers)
259 for (handler = mq->handlers; NULL != handler->cb; handler++)
261 if (handler->type == ntohs (mh->type))
263 handled = GNUNET_YES;
264 if ( (handler->expected_size > ms) ||
265 ( (handler->expected_size != ms) &&
266 (NULL == handler->mv) ) )
268 /* Too small, or not an exact size and
269 no 'mv' handler to check rest */
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);
277 if ( (NULL == handler->mv) ||
279 handler->mv (handler->cls, mh)) )
281 /* message well-formed, pass to handler */
282 handler->cb (handler->cls, mh);
286 /* Message rejected by check routine */
287 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
288 "Received malformed message of type %u\n",
289 (unsigned int) handler->type);
290 GNUNET_MQ_inject_error (mq,
291 GNUNET_MQ_ERROR_MALFORMED);
297 if (GNUNET_NO == handled)
298 LOG (GNUNET_ERROR_TYPE_INFO,
299 "No handler for message of type %d and size %d\n",
306 * Call the error handler of a message queue with the given
307 * error code. If there is no error handler, log a warning.
309 * This function is intended to be used by the implementation
312 * @param mq message queue
313 * @param error the error type
316 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
317 enum GNUNET_MQ_Error error)
319 if (NULL == mq->error_handler)
321 LOG (GNUNET_ERROR_TYPE_WARNING,
322 "Got error %d, but no handler installed\n",
326 mq->error_handler (mq->error_handler_cls,
332 * Discard the message queue message, free all
333 * allocated resources. Must be called in the event
334 * that a message is created but should not actually be sent.
336 * @param mqm the message to discard
339 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm)
341 GNUNET_assert (NULL == mqm->parent_queue);
347 * Obtain the current length of the message queue.
349 * @param mq queue to inspect
350 * @return number of queued, non-transmitted messages
353 GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq)
355 return mq->queue_length;
360 * Send a message with the given message queue.
361 * May only be called once per message.
363 * @param mq message queue
364 * @param ev the envelope with the message to send.
367 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
368 struct GNUNET_MQ_Envelope *ev)
370 GNUNET_assert (NULL != mq);
371 GNUNET_assert (NULL == ev->parent_queue);
374 ev->parent_queue = mq;
375 /* is the implementation busy? queue it! */
376 if (NULL != mq->current_envelope)
378 GNUNET_CONTAINER_DLL_insert_tail (mq->envelope_head,
383 mq->current_envelope = ev;
391 * Send a copy of a message with the given message queue.
392 * Can be called repeatedly on the same envelope.
394 * @param mq message queue
395 * @param ev the envelope with the message to send.
398 GNUNET_MQ_send_copy (struct GNUNET_MQ_Handle *mq,
399 const struct GNUNET_MQ_Envelope *ev)
401 struct GNUNET_MQ_Envelope *env;
404 msize = ntohs (ev->mh->size);
405 env = GNUNET_malloc (sizeof (struct GNUNET_MQ_Envelope) +
407 env->mh = (struct GNUNET_MessageHeader *) &env[1];
408 env->sent_cb = ev->sent_cb;
409 env->sent_cls = ev->sent_cls;
410 GNUNET_memcpy (&env[1],
420 * Task run to call the send implementation for the next queued
421 * message, if any. Only useful for implementing message queues,
422 * results in undefined behavior if not used carefully.
424 * @param cls message queue to send the next message with
427 impl_send_continue (void *cls)
429 struct GNUNET_MQ_Handle *mq = cls;
430 struct GNUNET_MQ_Envelope *current_envelope;
432 mq->continue_task = NULL;
433 /* call is only valid if we're actually currently sending
435 current_envelope = mq->current_envelope;
436 GNUNET_assert (NULL != current_envelope);
437 current_envelope->parent_queue = NULL;
438 GNUNET_assert (0 < mq->queue_length);
440 if (NULL == mq->envelope_head)
442 mq->current_envelope = NULL;
446 mq->current_envelope = mq->envelope_head;
447 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
449 mq->current_envelope);
451 mq->current_envelope->mh,
454 if (NULL != current_envelope->sent_cb)
455 current_envelope->sent_cb (current_envelope->sent_cls);
456 GNUNET_free (current_envelope);
461 * Call the send implementation for the next queued message, if any.
462 * Only useful for implementing message queues, results in undefined
463 * behavior if not used carefully.
465 * @param mq message queue to send the next message with
468 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq)
470 GNUNET_assert (NULL == mq->continue_task);
471 mq->continue_task = GNUNET_SCHEDULER_add_now (&impl_send_continue,
477 * Create a message queue for the specified handlers.
479 * @param send function the implements sending messages
480 * @param destroy function that implements destroying the queue
481 * @param cancel function that implements canceling a message
482 * @param impl_state for the queue, passed to 'send' and 'destroy'
483 * @param handlers array of message handlers
484 * @param error_handler handler for read and write errors
485 * @param error_handler_cls closure for @a error_handler
486 * @return a new message queue
488 struct GNUNET_MQ_Handle *
489 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
490 GNUNET_MQ_DestroyImpl destroy,
491 GNUNET_MQ_CancelImpl cancel,
493 const struct GNUNET_MQ_MessageHandler *handlers,
494 GNUNET_MQ_ErrorHandler error_handler,
495 void *error_handler_cls)
497 struct GNUNET_MQ_Handle *mq;
500 mq = GNUNET_new (struct GNUNET_MQ_Handle);
501 mq->send_impl = send;
502 mq->destroy_impl = destroy;
503 mq->cancel_impl = cancel;
504 if (NULL != handlers)
506 for (i=0;NULL != handlers[i].cb; i++) ;
507 mq->handlers = GNUNET_new_array (i + 1,
508 struct GNUNET_MQ_MessageHandler);
509 GNUNET_memcpy (mq->handlers,
511 i * sizeof (struct GNUNET_MQ_MessageHandler));
513 mq->error_handler = error_handler;
514 mq->error_handler_cls = error_handler_cls;
515 mq->impl_state = impl_state;
522 * Change the closure argument in all of the `handlers` of the
525 * @param mq to modify
526 * @param handlers_cls new closure to use
529 GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq,
534 if (NULL == mq->handlers)
536 for (i=0;NULL != mq->handlers[i].cb; i++)
537 mq->handlers[i].cls = handlers_cls;
542 * Get the message that should currently be sent.
543 * Fails if there is no current message.
544 * Only useful for implementing message queues,
545 * results in undefined behavior if not used carefully.
547 * @param mq message queue with the current message
548 * @return message to send, never NULL
550 const struct GNUNET_MessageHeader *
551 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq)
553 if (NULL == mq->current_envelope)
555 if (NULL == mq->current_envelope->mh)
557 return mq->current_envelope->mh;
562 * Get the implementation state associated with the
565 * While the GNUNET_MQ_Impl* callbacks receive the
566 * implementation state, continuations that are scheduled
567 * by the implementation function often only have one closure
568 * argument, with this function it is possible to get at the
569 * implementation state when only passing the GNUNET_MQ_Handle
572 * @param mq message queue with the current message
573 * @return message to send, never NULL
576 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq)
578 return mq->impl_state;
582 struct GNUNET_MQ_Envelope *
583 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
587 struct GNUNET_MQ_Envelope *mqm;
589 mqm = GNUNET_malloc (sizeof *mqm + size);
590 mqm->mh = (struct GNUNET_MessageHeader *) &mqm[1];
591 mqm->mh->size = htons (size);
592 mqm->mh->type = htons (type);
600 * Create a new envelope by copying an existing message.
602 * @param hdr header of the message to copy
603 * @return envelope containing @a hdr
605 struct GNUNET_MQ_Envelope *
606 GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr)
608 struct GNUNET_MQ_Envelope *mqm;
609 uint16_t size = ntohs (hdr->size);
611 mqm = GNUNET_malloc (sizeof (*mqm) + size);
612 mqm->mh = (struct GNUNET_MessageHeader *) &mqm[1];
613 GNUNET_memcpy (mqm->mh,
621 * Implementation of the #GNUNET_MQ_msg_nested_mh macro.
623 * @param mhp pointer to the message header pointer that will be changed to allocate at
624 * the newly allocated space for the message.
625 * @param base_size size of the data before the nested message
626 * @param type type of the message in the envelope
627 * @param nested_mh the message to append to the message after base_size
629 struct GNUNET_MQ_Envelope *
630 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
633 const struct GNUNET_MessageHeader *nested_mh)
635 struct GNUNET_MQ_Envelope *mqm;
638 if (NULL == nested_mh)
639 return GNUNET_MQ_msg_ (mhp, base_size, type);
641 size = base_size + ntohs (nested_mh->size);
643 /* check for uint16_t overflow */
644 if (size < base_size)
647 mqm = GNUNET_MQ_msg_ (mhp, size, type);
648 GNUNET_memcpy ((char *) mqm->mh + base_size,
650 ntohs (nested_mh->size));
657 * Transmit a queued message to the session's client.
659 * @param cls consensus session
660 * @param size number of bytes available in @a buf
661 * @param buf where the callee should write the message
662 * @return number of bytes written to @a buf
665 transmit_queued (void *cls,
669 struct GNUNET_MQ_Handle *mq = cls;
670 struct ServerClientSocketState *state = GNUNET_MQ_impl_state (mq);
671 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
674 GNUNET_assert (NULL != buf);
675 msg_size = ntohs (msg->size);
676 GNUNET_assert (size >= msg_size);
677 GNUNET_memcpy (buf, msg, msg_size);
680 GNUNET_MQ_impl_send_continue (mq);
687 server_client_destroy_impl (struct GNUNET_MQ_Handle *mq,
690 struct ServerClientSocketState *state = impl_state;
692 if (NULL != state->th)
694 GNUNET_SERVER_notify_transmit_ready_cancel (state->th);
698 GNUNET_assert (NULL != mq);
699 GNUNET_assert (NULL != state);
700 GNUNET_SERVER_client_drop (state->client);
706 server_client_send_impl (struct GNUNET_MQ_Handle *mq,
707 const struct GNUNET_MessageHeader *msg,
710 struct ServerClientSocketState *state = impl_state;
712 GNUNET_assert (NULL != mq);
713 state->th = GNUNET_SERVER_notify_transmit_ready (state->client,
715 GNUNET_TIME_UNIT_FOREVER_REL,
716 &transmit_queued, mq);
720 struct GNUNET_MQ_Handle *
721 GNUNET_MQ_queue_for_server_client (struct GNUNET_SERVER_Client *client)
723 struct GNUNET_MQ_Handle *mq;
724 struct ServerClientSocketState *scss;
726 mq = GNUNET_new (struct GNUNET_MQ_Handle);
727 scss = GNUNET_new (struct ServerClientSocketState);
728 mq->impl_state = scss;
729 scss->client = client;
730 GNUNET_SERVER_client_keep (client);
731 mq->send_impl = &server_client_send_impl;
732 mq->destroy_impl = &server_client_destroy_impl;
738 * Type of a function to call when we receive a message
742 * @param msg message received, NULL on timeout or fatal error
745 handle_client_message (void *cls,
746 const struct GNUNET_MessageHeader *msg)
748 struct GNUNET_MQ_Handle *mq = cls;
749 struct ClientConnectionState *state;
751 state = mq->impl_state;
754 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_READ);
757 GNUNET_CLIENT_receive (state->connection,
758 &handle_client_message,
760 GNUNET_TIME_UNIT_FOREVER_REL);
761 GNUNET_MQ_inject_message (mq, msg);
766 * Transmit a queued message to the session's client.
768 * @param cls consensus session
769 * @param size number of bytes available in @a buf
770 * @param buf where the callee should write the message
771 * @return number of bytes written to buf
774 connection_client_transmit_queued (void *cls,
778 struct GNUNET_MQ_Handle *mq = cls;
779 const struct GNUNET_MessageHeader *msg;
780 struct ClientConnectionState *state = mq->impl_state;
783 GNUNET_assert (NULL != mq);
785 msg = GNUNET_MQ_impl_current (mq);
789 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_READ);
793 if ( (GNUNET_YES == state->receive_requested) &&
794 (GNUNET_NO == state->receive_active) )
796 state->receive_active = GNUNET_YES;
797 GNUNET_CLIENT_receive (state->connection,
798 &handle_client_message,
800 GNUNET_TIME_UNIT_FOREVER_REL);
803 msg_size = ntohs (msg->size);
804 GNUNET_assert (size >= msg_size);
805 GNUNET_memcpy (buf, msg, msg_size);
808 GNUNET_MQ_impl_send_continue (mq);
815 connection_client_destroy_impl (struct GNUNET_MQ_Handle *mq,
818 struct ClientConnectionState *state = impl_state;
820 if (NULL != state->th)
822 GNUNET_CLIENT_notify_transmit_ready_cancel (state->th);
825 GNUNET_CLIENT_disconnect (state->connection);
826 GNUNET_free (impl_state);
831 connection_client_send_impl (struct GNUNET_MQ_Handle *mq,
832 const struct GNUNET_MessageHeader *msg,
835 struct ClientConnectionState *state = impl_state;
837 GNUNET_assert (NULL != state);
838 GNUNET_assert (NULL == state->th);
840 GNUNET_CLIENT_notify_transmit_ready (state->connection,
842 GNUNET_TIME_UNIT_FOREVER_REL,
844 &connection_client_transmit_queued,
846 GNUNET_assert (NULL != state->th);
851 connection_client_cancel_impl (struct GNUNET_MQ_Handle *mq,
854 struct ClientConnectionState *state = impl_state;
856 if (NULL != state->th)
858 GNUNET_CLIENT_notify_transmit_ready_cancel (state->th);
861 else if (NULL != mq->continue_task)
863 GNUNET_SCHEDULER_cancel (mq->continue_task);
864 mq->continue_task = NULL;
871 struct GNUNET_MQ_Handle *
872 GNUNET_MQ_queue_for_connection_client (struct GNUNET_CLIENT_Connection *connection,
873 const struct GNUNET_MQ_MessageHandler *handlers,
874 GNUNET_MQ_ErrorHandler error_handler,
875 void *error_handler_cls)
877 struct GNUNET_MQ_Handle *mq;
878 struct ClientConnectionState *state;
881 mq = GNUNET_new (struct GNUNET_MQ_Handle);
882 if (NULL != handlers)
884 for (i=0;NULL != handlers[i].cb; i++) ;
885 mq->handlers = GNUNET_new_array (i + 1,
886 struct GNUNET_MQ_MessageHandler);
887 GNUNET_memcpy (mq->handlers,
889 i * sizeof (struct GNUNET_MQ_MessageHandler));
891 mq->error_handler = error_handler;
892 mq->error_handler_cls = error_handler_cls;
893 state = GNUNET_new (struct ClientConnectionState);
894 state->connection = connection;
895 mq->impl_state = state;
896 mq->send_impl = &connection_client_send_impl;
897 mq->destroy_impl = &connection_client_destroy_impl;
898 mq->cancel_impl = &connection_client_cancel_impl;
899 if (NULL != handlers)
900 state->receive_requested = GNUNET_YES;
907 * Associate the assoc_data in mq with a unique request id.
909 * @param mq message queue, id will be unique for the queue
910 * @param assoc_data to associate
913 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
918 if (NULL == mq->assoc_map)
920 mq->assoc_map = GNUNET_CONTAINER_multihashmap32_create (8);
924 GNUNET_CONTAINER_multihashmap32_put (mq->assoc_map, id, assoc_data,
925 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
931 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
934 if (NULL == mq->assoc_map)
936 return GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map, request_id);
941 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
946 if (NULL == mq->assoc_map)
948 val = GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map,
950 GNUNET_CONTAINER_multihashmap32_remove_all (mq->assoc_map,
957 * Call a callback once the envelope has been sent, that is,
958 * sending it can not be canceled anymore.
959 * There can be only one notify sent callback per envelope.
961 * @param ev message to call the notify callback for
962 * @param cb the notify callback
963 * @param cb_cls closure for the callback
966 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *mqm,
967 GNUNET_MQ_NotifyCallback cb,
971 mqm->sent_cls = cb_cls;
976 * Handle we return for callbacks registered to be
977 * notified when #GNUNET_MQ_destroy() is called on a queue.
979 struct GNUNET_MQ_DestroyNotificationHandle
984 struct GNUNET_MQ_DestroyNotificationHandle *prev;
989 struct GNUNET_MQ_DestroyNotificationHandle *next;
992 * Queue to notify about.
994 struct GNUNET_MQ_Handle *mq;
999 GNUNET_SCHEDULER_TaskCallback cb;
1002 * Closure for @e cb.
1009 * Destroy the message queue.
1011 * @param mq message queue to destroy
1014 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq)
1016 struct GNUNET_MQ_DestroyNotificationHandle *dnh;
1018 if (NULL != mq->destroy_impl)
1020 mq->destroy_impl (mq, mq->impl_state);
1022 if (NULL != mq->continue_task)
1024 GNUNET_SCHEDULER_cancel (mq->continue_task);
1025 mq->continue_task = NULL;
1027 while (NULL != mq->envelope_head)
1029 struct GNUNET_MQ_Envelope *ev;
1031 ev = mq->envelope_head;
1032 ev->parent_queue = NULL;
1033 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
1036 GNUNET_assert (0 < mq->queue_length);
1038 GNUNET_MQ_discard (ev);
1040 if (NULL != mq->current_envelope)
1042 /* we can only discard envelopes that
1043 * are not queued! */
1044 mq->current_envelope->parent_queue = NULL;
1045 GNUNET_MQ_discard (mq->current_envelope);
1046 mq->current_envelope = NULL;
1047 GNUNET_assert (0 < mq->queue_length);
1050 GNUNET_assert (0 == mq->queue_length);
1051 while (NULL != (dnh = mq->dnh_head))
1053 dnh->cb (dnh->cb_cls);
1054 GNUNET_MQ_destroy_notify_cancel (dnh);
1056 if (NULL != mq->assoc_map)
1058 GNUNET_CONTAINER_multihashmap32_destroy (mq->assoc_map);
1059 mq->assoc_map = NULL;
1061 GNUNET_free_non_null (mq->handlers);
1066 const struct GNUNET_MessageHeader *
1067 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
1070 uint16_t whole_size;
1071 uint16_t nested_size;
1072 const struct GNUNET_MessageHeader *nested_msg;
1074 whole_size = ntohs (mh->size);
1075 GNUNET_assert (whole_size >= base_size);
1076 nested_size = whole_size - base_size;
1077 if (0 == nested_size)
1079 if (nested_size < sizeof (struct GNUNET_MessageHeader))
1081 GNUNET_break_op (0);
1084 nested_msg = (const struct GNUNET_MessageHeader *) ((char *) mh + base_size);
1085 if (ntohs (nested_msg->size) != nested_size)
1087 GNUNET_break_op (0);
1095 * Cancel sending the message. Message must have been sent with
1096 * #GNUNET_MQ_send before. May not be called after the notify sent
1097 * callback has been called
1099 * @param ev queued envelope to cancel
1102 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev)
1104 struct GNUNET_MQ_Handle *mq = ev->parent_queue;
1106 GNUNET_assert (NULL != mq);
1107 GNUNET_assert (NULL != mq->cancel_impl);
1109 if (mq->current_envelope == ev)
1111 // complex case, we already started with transmitting
1113 GNUNET_assert (0 < mq->queue_length);
1115 mq->cancel_impl (mq,
1117 // continue sending the next message, if any
1118 if (NULL == mq->envelope_head)
1120 mq->current_envelope = NULL;
1124 mq->current_envelope = mq->envelope_head;
1125 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
1127 mq->current_envelope);
1129 mq->current_envelope->mh,
1135 // simple case, message is still waiting in the queue
1136 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
1139 GNUNET_assert (0 < mq->queue_length);
1143 ev->parent_queue = NULL;
1150 * Function to obtain the current envelope
1151 * from within #GNUNET_MQ_SendImpl implementations.
1153 * @param mq message queue to interrogate
1154 * @return the current envelope
1156 struct GNUNET_MQ_Envelope *
1157 GNUNET_MQ_get_current_envelope (struct GNUNET_MQ_Handle *mq)
1159 return mq->current_envelope;
1164 * Function to obtain the last envelope in the queue.
1166 * @param mq message queue to interrogate
1167 * @return the last envelope in the queue
1169 struct GNUNET_MQ_Envelope *
1170 GNUNET_MQ_get_last_envelope (struct GNUNET_MQ_Handle *mq)
1172 if (NULL != mq->envelope_tail)
1173 return mq->envelope_tail;
1175 return mq->current_envelope;
1180 * Set application-specific options for this envelope.
1181 * Overrides the options set for the queue with
1182 * #GNUNET_MQ_set_options() for this message only.
1184 * @param env message to set options for
1185 * @param flags flags to use (meaning is queue-specific)
1186 * @param extra additional buffer for further data (also queue-specific)
1189 GNUNET_MQ_env_set_options (struct GNUNET_MQ_Envelope *env,
1195 env->have_custom_options = GNUNET_YES;
1200 * Get application-specific options for this envelope.
1202 * @param env message to set options for
1203 * @param[out] flags set to flags to use (meaning is queue-specific)
1204 * @return extra additional buffer for further data (also queue-specific)
1207 GNUNET_MQ_env_get_options (struct GNUNET_MQ_Envelope *env,
1210 struct GNUNET_MQ_Handle *mq = env->parent_queue;
1212 if (GNUNET_YES == env->have_custom_options)
1214 *flags = env->flags;
1222 *flags = mq->default_flags;
1223 return mq->default_extra;
1228 * Set application-specific options for this queue.
1230 * @param mq message queue to set options for
1231 * @param flags flags to use (meaning is queue-specific)
1232 * @param extra additional buffer for further data (also queue-specific)
1235 GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq,
1239 mq->default_flags = flags;
1240 mq->default_extra = extra;
1245 * Register function to be called whenever @a mq is being
1248 * @param mq message queue to watch
1249 * @param cb function to call on @a mq destruction
1250 * @param cb_cls closure for @a cb
1251 * @return handle for #GNUNET_MQ_destroy_notify_cancel().
1253 struct GNUNET_MQ_DestroyNotificationHandle *
1254 GNUNET_MQ_destroy_notify (struct GNUNET_MQ_Handle *mq,
1255 GNUNET_SCHEDULER_TaskCallback cb,
1258 struct GNUNET_MQ_DestroyNotificationHandle *dnh;
1260 dnh = GNUNET_new (struct GNUNET_MQ_DestroyNotificationHandle);
1263 dnh->cb_cls = cb_cls;
1264 GNUNET_CONTAINER_DLL_insert (mq->dnh_head,
1272 * Cancel registration from #GNUNET_MQ_destroy_notify().
1274 * @param dnh handle for registration to cancel
1277 GNUNET_MQ_destroy_notify_cancel (struct GNUNET_MQ_DestroyNotificationHandle *dnh)
1279 struct GNUNET_MQ_Handle *mq = dnh->mq;
1281 GNUNET_CONTAINER_DLL_remove (mq->dnh_head,