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_DEBUG,
299 "No handler for message of type %d\n",
305 * Call the error handler of a message queue with the given
306 * error code. If there is no error handler, log a warning.
308 * This function is intended to be used by the implementation
311 * @param mq message queue
312 * @param error the error type
315 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
316 enum GNUNET_MQ_Error error)
318 if (NULL == mq->error_handler)
320 LOG (GNUNET_ERROR_TYPE_WARNING,
321 "Got error %d, but no handler installed\n",
325 mq->error_handler (mq->error_handler_cls,
331 * Discard the message queue message, free all
332 * allocated resources. Must be called in the event
333 * that a message is created but should not actually be sent.
335 * @param mqm the message to discard
338 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm)
340 GNUNET_assert (NULL == mqm->parent_queue);
346 * Obtain the current length of the message queue.
348 * @param mq queue to inspect
349 * @return number of queued, non-transmitted messages
352 GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq)
354 return mq->queue_length;
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 ev->parent_queue = mq;
374 /* is the implementation busy? queue it! */
375 if (NULL != mq->current_envelope)
377 GNUNET_CONTAINER_DLL_insert_tail (mq->envelope_head,
382 mq->current_envelope = ev;
390 * Send a copy of a message with the given message queue.
391 * Can be called repeatedly on the same envelope.
393 * @param mq message queue
394 * @param ev the envelope with the message to send.
397 GNUNET_MQ_send_copy (struct GNUNET_MQ_Handle *mq,
398 const struct GNUNET_MQ_Envelope *ev)
400 struct GNUNET_MQ_Envelope *env;
403 msize = ntohs (ev->mh->size);
404 env = GNUNET_malloc (sizeof (struct GNUNET_MQ_Envelope) +
406 env->mh = (struct GNUNET_MessageHeader *) &env[1];
407 env->sent_cb = ev->sent_cb;
408 env->sent_cls = ev->sent_cls;
409 GNUNET_memcpy (&env[1],
419 * Task run to call the send implementation for the next queued
420 * message, if any. Only useful for implementing message queues,
421 * results in undefined behavior if not used carefully.
423 * @param cls message queue to send the next message with
426 impl_send_continue (void *cls)
428 struct GNUNET_MQ_Handle *mq = cls;
429 struct GNUNET_MQ_Envelope *current_envelope;
431 mq->continue_task = NULL;
432 /* call is only valid if we're actually currently sending
434 current_envelope = mq->current_envelope;
435 GNUNET_assert (NULL != current_envelope);
436 current_envelope->parent_queue = NULL;
437 GNUNET_assert (0 < mq->queue_length);
439 if (NULL == mq->envelope_head)
441 mq->current_envelope = NULL;
445 mq->current_envelope = mq->envelope_head;
446 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
448 mq->current_envelope);
450 mq->current_envelope->mh,
453 if (NULL != current_envelope->sent_cb)
454 current_envelope->sent_cb (current_envelope->sent_cls);
455 GNUNET_free (current_envelope);
460 * Call the send implementation for the next queued message, if any.
461 * Only useful for implementing message queues, results in undefined
462 * behavior if not used carefully.
464 * @param mq message queue to send the next message with
467 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq)
469 GNUNET_assert (NULL == mq->continue_task);
470 mq->continue_task = GNUNET_SCHEDULER_add_now (&impl_send_continue,
476 * Create a message queue for the specified handlers.
478 * @param send function the implements sending messages
479 * @param destroy function that implements destroying the queue
480 * @param cancel function that implements canceling a message
481 * @param impl_state for the queue, passed to 'send' and 'destroy'
482 * @param handlers array of message handlers
483 * @param error_handler handler for read and write errors
484 * @param error_handler_cls closure for @a error_handler
485 * @return a new message queue
487 struct GNUNET_MQ_Handle *
488 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
489 GNUNET_MQ_DestroyImpl destroy,
490 GNUNET_MQ_CancelImpl cancel,
492 const struct GNUNET_MQ_MessageHandler *handlers,
493 GNUNET_MQ_ErrorHandler error_handler,
494 void *error_handler_cls)
496 struct GNUNET_MQ_Handle *mq;
499 mq = GNUNET_new (struct GNUNET_MQ_Handle);
500 mq->send_impl = send;
501 mq->destroy_impl = destroy;
502 mq->cancel_impl = cancel;
503 if (NULL != handlers)
505 for (i=0;NULL != handlers[i].cb; i++) ;
506 mq->handlers = GNUNET_new_array (i + 1,
507 struct GNUNET_MQ_MessageHandler);
508 GNUNET_memcpy (mq->handlers,
510 i * sizeof (struct GNUNET_MQ_MessageHandler));
512 mq->error_handler = error_handler;
513 mq->error_handler_cls = error_handler_cls;
514 mq->impl_state = impl_state;
521 * Change the closure argument in all of the `handlers` of the
524 * @param mq to modify
525 * @param handlers_cls new closure to use
528 GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq,
533 if (NULL == mq->handlers)
535 for (i=0;NULL != mq->handlers[i].cb; i++)
536 mq->handlers[i].cls = handlers_cls;
541 * Get the message that should currently be sent.
542 * Fails if there is no current message.
543 * Only useful for implementing message queues,
544 * results in undefined behavior if not used carefully.
546 * @param mq message queue with the current message
547 * @return message to send, never NULL
549 const struct GNUNET_MessageHeader *
550 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq)
552 if (NULL == mq->current_envelope)
554 if (NULL == mq->current_envelope->mh)
556 return mq->current_envelope->mh;
561 * Get the implementation state associated with the
564 * While the GNUNET_MQ_Impl* callbacks receive the
565 * implementation state, continuations that are scheduled
566 * by the implementation function often only have one closure
567 * argument, with this function it is possible to get at the
568 * implementation state when only passing the GNUNET_MQ_Handle
571 * @param mq message queue with the current message
572 * @return message to send, never NULL
575 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq)
577 return mq->impl_state;
581 struct GNUNET_MQ_Envelope *
582 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
586 struct GNUNET_MQ_Envelope *mqm;
588 mqm = GNUNET_malloc (sizeof *mqm + size);
589 mqm->mh = (struct GNUNET_MessageHeader *) &mqm[1];
590 mqm->mh->size = htons (size);
591 mqm->mh->type = htons (type);
599 * Create a new envelope by copying an existing message.
601 * @param hdr header of the message to copy
602 * @return envelope containing @a hdr
604 struct GNUNET_MQ_Envelope *
605 GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr)
607 struct GNUNET_MQ_Envelope *mqm;
608 uint16_t size = ntohs (hdr->size);
610 mqm = GNUNET_malloc (sizeof (*mqm) + size);
611 mqm->mh = (struct GNUNET_MessageHeader *) &mqm[1];
612 GNUNET_memcpy (mqm->mh,
620 * Implementation of the #GNUNET_MQ_msg_nested_mh macro.
622 * @param mhp pointer to the message header pointer that will be changed to allocate at
623 * the newly allocated space for the message.
624 * @param base_size size of the data before the nested message
625 * @param type type of the message in the envelope
626 * @param nested_mh the message to append to the message after base_size
628 struct GNUNET_MQ_Envelope *
629 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
632 const struct GNUNET_MessageHeader *nested_mh)
634 struct GNUNET_MQ_Envelope *mqm;
637 if (NULL == nested_mh)
638 return GNUNET_MQ_msg_ (mhp, base_size, type);
640 size = base_size + ntohs (nested_mh->size);
642 /* check for uint16_t overflow */
643 if (size < base_size)
646 mqm = GNUNET_MQ_msg_ (mhp, size, type);
647 GNUNET_memcpy ((char *) mqm->mh + base_size,
649 ntohs (nested_mh->size));
656 * Transmit a queued message to the session's client.
658 * @param cls consensus session
659 * @param size number of bytes available in @a buf
660 * @param buf where the callee should write the message
661 * @return number of bytes written to @a buf
664 transmit_queued (void *cls,
668 struct GNUNET_MQ_Handle *mq = cls;
669 struct ServerClientSocketState *state = GNUNET_MQ_impl_state (mq);
670 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
673 GNUNET_assert (NULL != buf);
674 msg_size = ntohs (msg->size);
675 GNUNET_assert (size >= msg_size);
676 GNUNET_memcpy (buf, msg, msg_size);
679 GNUNET_MQ_impl_send_continue (mq);
686 server_client_destroy_impl (struct GNUNET_MQ_Handle *mq,
689 struct ServerClientSocketState *state = impl_state;
691 if (NULL != state->th)
693 GNUNET_SERVER_notify_transmit_ready_cancel (state->th);
697 GNUNET_assert (NULL != mq);
698 GNUNET_assert (NULL != state);
699 GNUNET_SERVER_client_drop (state->client);
705 server_client_send_impl (struct GNUNET_MQ_Handle *mq,
706 const struct GNUNET_MessageHeader *msg,
709 struct ServerClientSocketState *state = impl_state;
711 GNUNET_assert (NULL != mq);
712 state->th = GNUNET_SERVER_notify_transmit_ready (state->client,
714 GNUNET_TIME_UNIT_FOREVER_REL,
715 &transmit_queued, mq);
719 struct GNUNET_MQ_Handle *
720 GNUNET_MQ_queue_for_server_client (struct GNUNET_SERVER_Client *client)
722 struct GNUNET_MQ_Handle *mq;
723 struct ServerClientSocketState *scss;
725 mq = GNUNET_new (struct GNUNET_MQ_Handle);
726 scss = GNUNET_new (struct ServerClientSocketState);
727 mq->impl_state = scss;
728 scss->client = client;
729 GNUNET_SERVER_client_keep (client);
730 mq->send_impl = &server_client_send_impl;
731 mq->destroy_impl = &server_client_destroy_impl;
737 * Type of a function to call when we receive a message
741 * @param msg message received, NULL on timeout or fatal error
744 handle_client_message (void *cls,
745 const struct GNUNET_MessageHeader *msg)
747 struct GNUNET_MQ_Handle *mq = cls;
748 struct ClientConnectionState *state;
750 state = mq->impl_state;
753 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_READ);
756 GNUNET_CLIENT_receive (state->connection,
757 &handle_client_message,
759 GNUNET_TIME_UNIT_FOREVER_REL);
760 GNUNET_MQ_inject_message (mq, msg);
765 * Transmit a queued message to the session's client.
767 * @param cls consensus session
768 * @param size number of bytes available in @a buf
769 * @param buf where the callee should write the message
770 * @return number of bytes written to buf
773 connection_client_transmit_queued (void *cls,
777 struct GNUNET_MQ_Handle *mq = cls;
778 const struct GNUNET_MessageHeader *msg;
779 struct ClientConnectionState *state = mq->impl_state;
782 GNUNET_assert (NULL != mq);
784 msg = GNUNET_MQ_impl_current (mq);
788 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_READ);
792 if ( (GNUNET_YES == state->receive_requested) &&
793 (GNUNET_NO == state->receive_active) )
795 state->receive_active = GNUNET_YES;
796 GNUNET_CLIENT_receive (state->connection,
797 &handle_client_message,
799 GNUNET_TIME_UNIT_FOREVER_REL);
802 msg_size = ntohs (msg->size);
803 GNUNET_assert (size >= msg_size);
804 GNUNET_memcpy (buf, msg, msg_size);
807 GNUNET_MQ_impl_send_continue (mq);
814 connection_client_destroy_impl (struct GNUNET_MQ_Handle *mq,
817 struct ClientConnectionState *state = impl_state;
819 if (NULL != state->th)
821 GNUNET_CLIENT_notify_transmit_ready_cancel (state->th);
824 GNUNET_CLIENT_disconnect (state->connection);
825 GNUNET_free (impl_state);
830 connection_client_send_impl (struct GNUNET_MQ_Handle *mq,
831 const struct GNUNET_MessageHeader *msg,
834 struct ClientConnectionState *state = impl_state;
836 GNUNET_assert (NULL != state);
837 GNUNET_assert (NULL == state->th);
839 GNUNET_CLIENT_notify_transmit_ready (state->connection,
841 GNUNET_TIME_UNIT_FOREVER_REL,
843 &connection_client_transmit_queued,
845 GNUNET_assert (NULL != state->th);
850 connection_client_cancel_impl (struct GNUNET_MQ_Handle *mq,
853 struct ClientConnectionState *state = impl_state;
855 if (NULL != state->th)
857 GNUNET_CLIENT_notify_transmit_ready_cancel (state->th);
860 else if (NULL != mq->continue_task)
862 GNUNET_SCHEDULER_cancel (mq->continue_task);
863 mq->continue_task = NULL;
870 struct GNUNET_MQ_Handle *
871 GNUNET_MQ_queue_for_connection_client (struct GNUNET_CLIENT_Connection *connection,
872 const struct GNUNET_MQ_MessageHandler *handlers,
873 GNUNET_MQ_ErrorHandler error_handler,
874 void *error_handler_cls)
876 struct GNUNET_MQ_Handle *mq;
877 struct ClientConnectionState *state;
880 mq = GNUNET_new (struct GNUNET_MQ_Handle);
881 if (NULL != handlers)
883 for (i=0;NULL != handlers[i].cb; i++) ;
884 mq->handlers = GNUNET_new_array (i + 1,
885 struct GNUNET_MQ_MessageHandler);
886 GNUNET_memcpy (mq->handlers,
888 i * sizeof (struct GNUNET_MQ_MessageHandler));
890 mq->error_handler = error_handler;
891 mq->error_handler_cls = error_handler_cls;
892 state = GNUNET_new (struct ClientConnectionState);
893 state->connection = connection;
894 mq->impl_state = state;
895 mq->send_impl = &connection_client_send_impl;
896 mq->destroy_impl = &connection_client_destroy_impl;
897 mq->cancel_impl = &connection_client_cancel_impl;
898 if (NULL != handlers)
899 state->receive_requested = GNUNET_YES;
906 * Associate the assoc_data in mq with a unique request id.
908 * @param mq message queue, id will be unique for the queue
909 * @param assoc_data to associate
912 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
917 if (NULL == mq->assoc_map)
919 mq->assoc_map = GNUNET_CONTAINER_multihashmap32_create (8);
923 GNUNET_CONTAINER_multihashmap32_put (mq->assoc_map, id, assoc_data,
924 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
930 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
933 if (NULL == mq->assoc_map)
935 return GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map, request_id);
940 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
945 if (NULL == mq->assoc_map)
947 val = GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map,
949 GNUNET_CONTAINER_multihashmap32_remove_all (mq->assoc_map,
956 * Call a callback once the envelope has been sent, that is,
957 * sending it can not be canceled anymore.
958 * There can be only one notify sent callback per envelope.
960 * @param ev message to call the notify callback for
961 * @param cb the notify callback
962 * @param cb_cls closure for the callback
965 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *mqm,
966 GNUNET_MQ_NotifyCallback cb,
970 mqm->sent_cls = cb_cls;
975 * Handle we return for callbacks registered to be
976 * notified when #GNUNET_MQ_destroy() is called on a queue.
978 struct GNUNET_MQ_DestroyNotificationHandle
983 struct GNUNET_MQ_DestroyNotificationHandle *prev;
988 struct GNUNET_MQ_DestroyNotificationHandle *next;
991 * Queue to notify about.
993 struct GNUNET_MQ_Handle *mq;
998 GNUNET_SCHEDULER_TaskCallback cb;
1001 * Closure for @e cb.
1008 * Destroy the message queue.
1010 * @param mq message queue to destroy
1013 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq)
1015 struct GNUNET_MQ_DestroyNotificationHandle *dnh;
1017 if (NULL != mq->destroy_impl)
1019 mq->destroy_impl (mq, mq->impl_state);
1021 if (NULL != mq->continue_task)
1023 GNUNET_SCHEDULER_cancel (mq->continue_task);
1024 mq->continue_task = NULL;
1026 while (NULL != mq->envelope_head)
1028 struct GNUNET_MQ_Envelope *ev;
1030 ev = mq->envelope_head;
1031 ev->parent_queue = NULL;
1032 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
1035 GNUNET_assert (0 < mq->queue_length);
1037 GNUNET_MQ_discard (ev);
1039 if (NULL != mq->current_envelope)
1041 /* we can only discard envelopes that
1042 * are not queued! */
1043 mq->current_envelope->parent_queue = NULL;
1044 GNUNET_MQ_discard (mq->current_envelope);
1045 mq->current_envelope = NULL;
1046 GNUNET_assert (0 < mq->queue_length);
1049 GNUNET_assert (0 == mq->queue_length);
1050 while (NULL != (dnh = mq->dnh_head))
1052 dnh->cb (dnh->cb_cls);
1053 GNUNET_MQ_destroy_notify_cancel (dnh);
1055 if (NULL != mq->assoc_map)
1057 GNUNET_CONTAINER_multihashmap32_destroy (mq->assoc_map);
1058 mq->assoc_map = NULL;
1060 GNUNET_free_non_null (mq->handlers);
1065 const struct GNUNET_MessageHeader *
1066 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
1069 uint16_t whole_size;
1070 uint16_t nested_size;
1071 const struct GNUNET_MessageHeader *nested_msg;
1073 whole_size = ntohs (mh->size);
1074 GNUNET_assert (whole_size >= base_size);
1075 nested_size = whole_size - base_size;
1076 if (0 == nested_size)
1078 if (nested_size < sizeof (struct GNUNET_MessageHeader))
1080 GNUNET_break_op (0);
1083 nested_msg = (const struct GNUNET_MessageHeader *) ((char *) mh + base_size);
1084 if (ntohs (nested_msg->size) != nested_size)
1086 GNUNET_break_op (0);
1094 * Cancel sending the message. Message must have been sent with
1095 * #GNUNET_MQ_send before. May not be called after the notify sent
1096 * callback has been called
1098 * @param ev queued envelope to cancel
1101 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev)
1103 struct GNUNET_MQ_Handle *mq = ev->parent_queue;
1105 GNUNET_assert (NULL != mq);
1106 GNUNET_assert (NULL != mq->cancel_impl);
1108 if (mq->current_envelope == ev)
1110 // complex case, we already started with transmitting
1112 GNUNET_assert (0 < mq->queue_length);
1114 mq->cancel_impl (mq,
1116 // continue sending the next message, if any
1117 if (NULL == mq->envelope_head)
1119 mq->current_envelope = NULL;
1123 mq->current_envelope = mq->envelope_head;
1124 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
1126 mq->current_envelope);
1128 mq->current_envelope->mh,
1134 // simple case, message is still waiting in the queue
1135 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
1138 GNUNET_assert (0 < mq->queue_length);
1142 ev->parent_queue = NULL;
1149 * Function to obtain the current envelope
1150 * from within #GNUNET_MQ_SendImpl implementations.
1152 * @param mq message queue to interrogate
1153 * @return the current envelope
1155 struct GNUNET_MQ_Envelope *
1156 GNUNET_MQ_get_current_envelope (struct GNUNET_MQ_Handle *mq)
1158 return mq->current_envelope;
1163 * Function to obtain the last envelope in the queue.
1165 * @param mq message queue to interrogate
1166 * @return the last envelope in the queue
1168 struct GNUNET_MQ_Envelope *
1169 GNUNET_MQ_get_last_envelope (struct GNUNET_MQ_Handle *mq)
1171 if (NULL != mq->envelope_tail)
1172 return mq->envelope_tail;
1174 return mq->current_envelope;
1179 * Set application-specific options for this envelope.
1180 * Overrides the options set for the queue with
1181 * #GNUNET_MQ_set_options() for this message only.
1183 * @param env message to set options for
1184 * @param flags flags to use (meaning is queue-specific)
1185 * @param extra additional buffer for further data (also queue-specific)
1188 GNUNET_MQ_env_set_options (struct GNUNET_MQ_Envelope *env,
1194 env->have_custom_options = GNUNET_YES;
1199 * Get application-specific options for this envelope.
1201 * @param env message to set options for
1202 * @param[out] flags set to flags to use (meaning is queue-specific)
1203 * @return extra additional buffer for further data (also queue-specific)
1206 GNUNET_MQ_env_get_options (struct GNUNET_MQ_Envelope *env,
1209 struct GNUNET_MQ_Handle *mq = env->parent_queue;
1211 if (GNUNET_YES == env->have_custom_options)
1213 *flags = env->flags;
1221 *flags = mq->default_flags;
1222 return mq->default_extra;
1227 * Set application-specific options for this queue.
1229 * @param mq message queue to set options for
1230 * @param flags flags to use (meaning is queue-specific)
1231 * @param extra additional buffer for further data (also queue-specific)
1234 GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq,
1238 mq->default_flags = flags;
1239 mq->default_extra = extra;
1244 * Register function to be called whenever @a mq is being
1247 * @param mq message queue to watch
1248 * @param cb function to call on @a mq destruction
1249 * @param cb_cls closure for @a cb
1250 * @return handle for #GNUNET_MQ_destroy_notify_cancel().
1252 struct GNUNET_MQ_DestroyNotificationHandle *
1253 GNUNET_MQ_destroy_notify (struct GNUNET_MQ_Handle *mq,
1254 GNUNET_SCHEDULER_TaskCallback cb,
1257 struct GNUNET_MQ_DestroyNotificationHandle *dnh;
1259 dnh = GNUNET_new (struct GNUNET_MQ_DestroyNotificationHandle);
1262 dnh->cb_cls = cb_cls;
1263 GNUNET_CONTAINER_DLL_insert (mq->dnh_head,
1271 * Cancel registration from #GNUNET_MQ_destroy_notify().
1273 * @param dnh handle for registration to cancel
1276 GNUNET_MQ_destroy_notify_cancel (struct GNUNET_MQ_DestroyNotificationHandle *dnh)
1278 struct GNUNET_MQ_Handle *mq = dnh->mq;
1280 GNUNET_CONTAINER_DLL_remove (mq->dnh_head,