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 * 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 * GNUNET_YES if the sent notification was called
149 * for the current envelope.
151 int send_notification_called;
154 * Map of associations, lazily allocated
156 struct GNUNET_CONTAINER_MultiHashMap32 *assoc_map;
159 * Task scheduled during #GNUNET_MQ_impl_send_continue
160 * or #GNUNET_MQ_impl_send_in_flight
162 struct GNUNET_SCHEDULER_Task *send_task;
165 * Functions to call on queue destruction; kept in a DLL.
167 struct GNUNET_MQ_DestroyNotificationHandle *dnh_head;
170 * Functions to call on queue destruction; kept in a DLL.
172 struct GNUNET_MQ_DestroyNotificationHandle *dnh_tail;
175 * Additional options buffer set for this queue by
176 * #GNUNET_MQ_set_options(). Default is 0.
178 const void *default_extra;
181 * Flags that were set for this queue by
182 * #GNUNET_MQ_set_options(). Default is 0.
184 uint64_t default_flags;
187 * Next id that should be used for the @e assoc_map,
188 * initialized lazily to a random value together with
194 * Number of entries we have in the envelope-DLL.
196 unsigned int queue_length;
199 * GNUNET_YES if GNUNET_MQ_impl_evacuate was called.
206 * Implementation-specific state for connection to
207 * client (MQ for server).
209 struct ServerClientSocketState
212 * Handle of the client that connected to the server.
214 struct GNUNET_SERVER_Client *client;
217 * Active transmission request to the client.
219 struct GNUNET_SERVER_TransmitHandle *th;
224 * Implementation-specific state for connection to
225 * service (MQ for clients).
227 struct ClientConnectionState
230 * Did we call receive alread alreadyy?
235 * Do we also want to receive?
237 int receive_requested;
240 * Connection to the service.
242 struct GNUNET_CLIENT_Connection *connection;
245 * Active transmission request (or NULL).
247 struct GNUNET_CLIENT_TransmitHandle *th;
252 * Call the message message handler that was registered
253 * for the type of the given message in the given message queue.
255 * This function is indended to be used for the implementation
258 * @param mq message queue with the handlers
259 * @param mh message to dispatch
262 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
263 const struct GNUNET_MessageHeader *mh)
265 const struct GNUNET_MQ_MessageHandler *handler;
266 int handled = GNUNET_NO;
267 uint16_t ms = ntohs (mh->size);
269 if (NULL == mq->handlers)
271 for (handler = mq->handlers; NULL != handler->cb; handler++)
273 if (handler->type == ntohs (mh->type))
275 handled = GNUNET_YES;
276 if ( (handler->expected_size > ms) ||
277 ( (handler->expected_size != ms) &&
278 (NULL == handler->mv) ) )
280 /* Too small, or not an exact size and
281 no 'mv' handler to check rest */
282 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
283 "Received malformed message of type %u\n",
284 (unsigned int) handler->type);
285 GNUNET_MQ_inject_error (mq,
286 GNUNET_MQ_ERROR_MALFORMED);
289 if ( (NULL == handler->mv) ||
291 handler->mv (handler->cls, mh)) )
293 /* message well-formed, pass to handler */
294 handler->cb (handler->cls, mh);
298 /* Message rejected by check routine */
299 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
300 "Received malformed message of type %u\n",
301 (unsigned int) handler->type);
302 GNUNET_MQ_inject_error (mq,
303 GNUNET_MQ_ERROR_MALFORMED);
309 if (GNUNET_NO == handled)
310 LOG (GNUNET_ERROR_TYPE_INFO,
311 "No handler for message of type %d and size %d\n",
318 * Call the error handler of a message queue with the given
319 * error code. If there is no error handler, log a warning.
321 * This function is intended to be used by the implementation
324 * @param mq message queue
325 * @param error the error type
328 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
329 enum GNUNET_MQ_Error error)
331 if (NULL == mq->error_handler)
333 LOG (GNUNET_ERROR_TYPE_WARNING,
334 "Got error %d, but no handler installed\n",
338 mq->error_handler (mq->error_handler_cls,
344 * Discard the message queue message, free all
345 * allocated resources. Must be called in the event
346 * that a message is created but should not actually be sent.
348 * @param mqm the message to discard
351 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *ev)
353 GNUNET_assert (NULL == ev->parent_queue);
359 * Obtain the current length of the message queue.
361 * @param mq queue to inspect
362 * @return number of queued, non-transmitted messages
365 GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq)
367 return mq->queue_length;
372 * Send a message with the given message queue.
373 * May only be called once per message.
375 * @param mq message queue
376 * @param ev the envelope with the message to send.
379 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
380 struct GNUNET_MQ_Envelope *ev)
382 GNUNET_assert (NULL != mq);
383 GNUNET_assert (NULL == ev->parent_queue);
386 ev->parent_queue = mq;
387 /* is the implementation busy? queue it! */
388 if (NULL != mq->current_envelope)
390 GNUNET_CONTAINER_DLL_insert_tail (mq->envelope_head,
395 mq->current_envelope = ev;
403 * Send a copy of a message with the given message queue.
404 * Can be called repeatedly on the same envelope.
406 * @param mq message queue
407 * @param ev the envelope with the message to send.
410 GNUNET_MQ_send_copy (struct GNUNET_MQ_Handle *mq,
411 const struct GNUNET_MQ_Envelope *ev)
413 struct GNUNET_MQ_Envelope *env;
416 msize = ntohs (ev->mh->size);
417 env = GNUNET_malloc (sizeof (struct GNUNET_MQ_Envelope) +
419 env->mh = (struct GNUNET_MessageHeader *) &env[1];
420 env->sent_cb = ev->sent_cb;
421 env->sent_cls = ev->sent_cls;
422 GNUNET_memcpy (&env[1],
431 * Task run to call the send notification for the next queued
432 * message, if any. Only useful for implementing message queues,
433 * results in undefined behavior if not used carefully.
435 * @param cls message queue to send the next message with
438 impl_send_in_flight (void *cls)
440 struct GNUNET_MQ_Handle *mq = cls;
441 struct GNUNET_MQ_Envelope *current_envelope;
443 mq->send_task = NULL;
444 /* call is only valid if we're actually currently sending
446 current_envelope = mq->current_envelope;
447 GNUNET_assert (NULL != current_envelope);
448 /* can't call cancel from now on anymore */
449 current_envelope->parent_queue = NULL;
450 if ( (GNUNET_NO == mq->send_notification_called) &&
451 (NULL != current_envelope->sent_cb) )
453 current_envelope->sent_cb (current_envelope->sent_cls);
455 mq->send_notification_called = GNUNET_YES;
460 * Task run to call the send implementation for the next queued
461 * message, if any. Only useful for implementing message queues,
462 * results in undefined behavior if not used carefully.
464 * @param cls message queue to send the next message with
467 impl_send_continue (void *cls)
469 struct GNUNET_MQ_Handle *mq = cls;
470 struct GNUNET_MQ_Envelope *current_envelope;
472 mq->send_task = NULL;
473 /* call is only valid if we're actually currently sending
475 current_envelope = mq->current_envelope;
476 GNUNET_assert (NULL != current_envelope);
477 impl_send_in_flight (mq);
478 GNUNET_assert (0 < mq->queue_length);
480 if (NULL == mq->envelope_head)
482 mq->current_envelope = NULL;
486 mq->current_envelope = mq->envelope_head;
487 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
489 mq->current_envelope);
490 mq->send_notification_called = GNUNET_NO;
492 mq->current_envelope->mh,
495 GNUNET_free (current_envelope);
500 * Call the send implementation for the next queued message, if any.
501 * Only useful for implementing message queues, results in undefined
502 * behavior if not used carefully.
504 * @param mq message queue to send the next message with
507 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq)
509 /* maybe #GNUNET_MQ_impl_send_in_flight was called? */
510 if (NULL != mq->send_task)
512 GNUNET_SCHEDULER_cancel (mq->send_task);
514 mq->send_task = GNUNET_SCHEDULER_add_now (&impl_send_continue,
520 * Call the send notification for the current message, but do not
521 * try to send the next message until #gnunet_mq_impl_send_continue
524 * only useful for implementing message queues, results in undefined
525 * behavior if not used carefully.
527 * @param mq message queue to send the next message with
530 GNUNET_MQ_impl_send_in_flight (struct GNUNET_MQ_Handle *mq)
532 GNUNET_assert (NULL == mq->send_task);
533 mq->send_task = GNUNET_SCHEDULER_add_now (&impl_send_in_flight,
539 * Create a message queue for the specified handlers.
541 * @param send function the implements sending messages
542 * @param destroy function that implements destroying the queue
543 * @param cancel function that implements canceling a message
544 * @param impl_state for the queue, passed to 'send' and 'destroy'
545 * @param handlers array of message handlers
546 * @param error_handler handler for read and write errors
547 * @param error_handler_cls closure for @a error_handler
548 * @return a new message queue
550 struct GNUNET_MQ_Handle *
551 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
552 GNUNET_MQ_DestroyImpl destroy,
553 GNUNET_MQ_CancelImpl cancel,
555 const struct GNUNET_MQ_MessageHandler *handlers,
556 GNUNET_MQ_ErrorHandler error_handler,
557 void *error_handler_cls)
559 struct GNUNET_MQ_Handle *mq;
562 mq = GNUNET_new (struct GNUNET_MQ_Handle);
563 mq->send_impl = send;
564 mq->destroy_impl = destroy;
565 mq->cancel_impl = cancel;
566 if (NULL != handlers)
568 for (i=0;NULL != handlers[i].cb; i++) ;
569 mq->handlers = GNUNET_new_array (i + 1,
570 struct GNUNET_MQ_MessageHandler);
571 GNUNET_memcpy (mq->handlers,
573 i * sizeof (struct GNUNET_MQ_MessageHandler));
575 mq->error_handler = error_handler;
576 mq->error_handler_cls = error_handler_cls;
577 mq->impl_state = impl_state;
584 * Change the closure argument in all of the `handlers` of the
587 * @param mq to modify
588 * @param handlers_cls new closure to use
591 GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq,
596 if (NULL == mq->handlers)
598 for (i=0;NULL != mq->handlers[i].cb; i++)
599 mq->handlers[i].cls = handlers_cls;
604 * Get the message that should currently be sent.
605 * Fails if there is no current message.
606 * Only useful for implementing message queues,
607 * results in undefined behavior if not used carefully.
609 * @param mq message queue with the current message
610 * @return message to send, never NULL
612 const struct GNUNET_MessageHeader *
613 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq)
615 GNUNET_assert (NULL != mq->current_envelope);
616 GNUNET_assert (NULL == mq->current_envelope->mh);
617 return mq->current_envelope->mh;
622 * Get the implementation state associated with the
625 * While the GNUNET_MQ_Impl* callbacks receive the
626 * implementation state, continuations that are scheduled
627 * by the implementation function often only have one closure
628 * argument, with this function it is possible to get at the
629 * implementation state when only passing the GNUNET_MQ_Handle
632 * @param mq message queue with the current message
633 * @return message to send, never NULL
636 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq)
638 return mq->impl_state;
642 struct GNUNET_MQ_Envelope *
643 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
647 struct GNUNET_MQ_Envelope *ev;
649 ev = GNUNET_malloc (size + sizeof (struct GNUNET_MQ_Envelope));
650 ev->mh = (struct GNUNET_MessageHeader *) &ev[1];
651 ev->mh->size = htons (size);
652 ev->mh->type = htons (type);
660 * Create a new envelope by copying an existing message.
662 * @param hdr header of the message to copy
663 * @return envelope containing @a hdr
665 struct GNUNET_MQ_Envelope *
666 GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr)
668 struct GNUNET_MQ_Envelope *mqm;
669 uint16_t size = ntohs (hdr->size);
671 mqm = GNUNET_malloc (sizeof (*mqm) + size);
672 mqm->mh = (struct GNUNET_MessageHeader *) &mqm[1];
673 GNUNET_memcpy (mqm->mh,
681 * Implementation of the #GNUNET_MQ_msg_nested_mh macro.
683 * @param mhp pointer to the message header pointer that will be changed to allocate at
684 * the newly allocated space for the message.
685 * @param base_size size of the data before the nested message
686 * @param type type of the message in the envelope
687 * @param nested_mh the message to append to the message after base_size
689 struct GNUNET_MQ_Envelope *
690 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
693 const struct GNUNET_MessageHeader *nested_mh)
695 struct GNUNET_MQ_Envelope *mqm;
698 if (NULL == nested_mh)
699 return GNUNET_MQ_msg_ (mhp, base_size, type);
701 size = base_size + ntohs (nested_mh->size);
703 /* check for uint16_t overflow */
704 if (size < base_size)
707 mqm = GNUNET_MQ_msg_ (mhp, size, type);
708 GNUNET_memcpy ((char *) mqm->mh + base_size,
710 ntohs (nested_mh->size));
717 * Transmit a queued message to the session's client.
719 * @param cls consensus session
720 * @param size number of bytes available in @a buf
721 * @param buf where the callee should write the message
722 * @return number of bytes written to @a buf
725 transmit_queued (void *cls,
729 struct GNUNET_MQ_Handle *mq = cls;
730 struct ServerClientSocketState *state = GNUNET_MQ_impl_state (mq);
731 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
734 GNUNET_assert (NULL != buf);
735 msg_size = ntohs (msg->size);
736 GNUNET_assert (size >= msg_size);
737 GNUNET_memcpy (buf, msg, msg_size);
740 GNUNET_MQ_impl_send_continue (mq);
747 server_client_destroy_impl (struct GNUNET_MQ_Handle *mq,
750 struct ServerClientSocketState *state = impl_state;
752 if (NULL != state->th)
754 GNUNET_SERVER_notify_transmit_ready_cancel (state->th);
758 GNUNET_assert (NULL != mq);
759 GNUNET_assert (NULL != state);
760 GNUNET_SERVER_client_drop (state->client);
766 server_client_send_impl (struct GNUNET_MQ_Handle *mq,
767 const struct GNUNET_MessageHeader *msg,
770 struct ServerClientSocketState *state = impl_state;
772 GNUNET_assert (NULL != mq);
773 state->th = GNUNET_SERVER_notify_transmit_ready (state->client,
775 GNUNET_TIME_UNIT_FOREVER_REL,
776 &transmit_queued, mq);
780 struct GNUNET_MQ_Handle *
781 GNUNET_MQ_queue_for_server_client (struct GNUNET_SERVER_Client *client)
783 struct GNUNET_MQ_Handle *mq;
784 struct ServerClientSocketState *scss;
786 mq = GNUNET_new (struct GNUNET_MQ_Handle);
787 scss = GNUNET_new (struct ServerClientSocketState);
788 mq->impl_state = scss;
789 scss->client = client;
790 GNUNET_SERVER_client_keep (client);
791 mq->send_impl = &server_client_send_impl;
792 mq->destroy_impl = &server_client_destroy_impl;
798 * Type of a function to call when we receive a message
802 * @param msg message received, NULL on timeout or fatal error
805 handle_client_message (void *cls,
806 const struct GNUNET_MessageHeader *msg)
808 struct GNUNET_MQ_Handle *mq = cls;
809 struct ClientConnectionState *state;
811 state = mq->impl_state;
814 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_READ);
817 GNUNET_CLIENT_receive (state->connection,
818 &handle_client_message,
820 GNUNET_TIME_UNIT_FOREVER_REL);
821 GNUNET_MQ_inject_message (mq, msg);
826 * Transmit a queued message to the session's client.
828 * @param cls consensus session
829 * @param size number of bytes available in @a buf
830 * @param buf where the callee should write the message
831 * @return number of bytes written to buf
834 connection_client_transmit_queued (void *cls,
838 struct GNUNET_MQ_Handle *mq = cls;
839 const struct GNUNET_MessageHeader *msg;
840 struct ClientConnectionState *state = mq->impl_state;
843 GNUNET_assert (NULL != mq);
845 msg = GNUNET_MQ_impl_current (mq);
849 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_READ);
853 if ( (GNUNET_YES == state->receive_requested) &&
854 (GNUNET_NO == state->receive_active) )
856 state->receive_active = GNUNET_YES;
857 GNUNET_CLIENT_receive (state->connection,
858 &handle_client_message,
860 GNUNET_TIME_UNIT_FOREVER_REL);
863 msg_size = ntohs (msg->size);
864 GNUNET_assert (size >= msg_size);
865 GNUNET_memcpy (buf, msg, msg_size);
868 GNUNET_MQ_impl_send_continue (mq);
875 connection_client_destroy_impl (struct GNUNET_MQ_Handle *mq,
878 struct ClientConnectionState *state = impl_state;
880 if (NULL != state->th)
882 GNUNET_CLIENT_notify_transmit_ready_cancel (state->th);
885 GNUNET_CLIENT_disconnect (state->connection);
886 GNUNET_free (impl_state);
891 connection_client_send_impl (struct GNUNET_MQ_Handle *mq,
892 const struct GNUNET_MessageHeader *msg,
895 struct ClientConnectionState *state = impl_state;
897 GNUNET_assert (NULL != state);
898 GNUNET_assert (NULL == state->th);
900 GNUNET_CLIENT_notify_transmit_ready (state->connection,
902 GNUNET_TIME_UNIT_FOREVER_REL,
904 &connection_client_transmit_queued,
906 GNUNET_assert (NULL != state->th);
911 connection_client_cancel_impl (struct GNUNET_MQ_Handle *mq,
914 struct ClientConnectionState *state = impl_state;
916 if (NULL != state->th)
918 GNUNET_CLIENT_notify_transmit_ready_cancel (state->th);
921 else if (NULL != mq->send_task)
923 GNUNET_SCHEDULER_cancel (mq->send_task);
924 mq->send_task = NULL;
931 struct GNUNET_MQ_Handle *
932 GNUNET_MQ_queue_for_connection_client (struct GNUNET_CLIENT_Connection *connection,
933 const struct GNUNET_MQ_MessageHandler *handlers,
934 GNUNET_MQ_ErrorHandler error_handler,
935 void *error_handler_cls)
937 struct GNUNET_MQ_Handle *mq;
938 struct ClientConnectionState *state;
941 mq = GNUNET_new (struct GNUNET_MQ_Handle);
942 if (NULL != handlers)
944 for (i=0;NULL != handlers[i].cb; i++) ;
945 mq->handlers = GNUNET_new_array (i + 1,
946 struct GNUNET_MQ_MessageHandler);
947 GNUNET_memcpy (mq->handlers,
949 i * sizeof (struct GNUNET_MQ_MessageHandler));
951 mq->error_handler = error_handler;
952 mq->error_handler_cls = error_handler_cls;
953 state = GNUNET_new (struct ClientConnectionState);
954 state->connection = connection;
955 mq->impl_state = state;
956 mq->send_impl = &connection_client_send_impl;
957 mq->destroy_impl = &connection_client_destroy_impl;
958 mq->cancel_impl = &connection_client_cancel_impl;
959 if (NULL != handlers)
960 state->receive_requested = GNUNET_YES;
967 * Associate the assoc_data in mq with a unique request id.
969 * @param mq message queue, id will be unique for the queue
970 * @param assoc_data to associate
973 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
978 if (NULL == mq->assoc_map)
980 mq->assoc_map = GNUNET_CONTAINER_multihashmap32_create (8);
984 GNUNET_CONTAINER_multihashmap32_put (mq->assoc_map, id, assoc_data,
985 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
991 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
994 if (NULL == mq->assoc_map)
996 return GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map, request_id);
1001 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
1002 uint32_t request_id)
1006 if (NULL == mq->assoc_map)
1008 val = GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map,
1010 GNUNET_CONTAINER_multihashmap32_remove_all (mq->assoc_map,
1017 * Call a callback once the envelope has been sent, that is,
1018 * sending it can not be canceled anymore.
1019 * There can be only one notify sent callback per envelope.
1021 * @param ev message to call the notify callback for
1022 * @param cb the notify callback
1023 * @param cb_cls closure for the callback
1026 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *mqm,
1027 GNUNET_MQ_NotifyCallback cb,
1031 mqm->sent_cls = cb_cls;
1036 * Handle we return for callbacks registered to be
1037 * notified when #GNUNET_MQ_destroy() is called on a queue.
1039 struct GNUNET_MQ_DestroyNotificationHandle
1044 struct GNUNET_MQ_DestroyNotificationHandle *prev;
1049 struct GNUNET_MQ_DestroyNotificationHandle *next;
1052 * Queue to notify about.
1054 struct GNUNET_MQ_Handle *mq;
1059 GNUNET_SCHEDULER_TaskCallback cb;
1062 * Closure for @e cb.
1069 * Destroy the message queue.
1071 * @param mq message queue to destroy
1074 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq)
1076 struct GNUNET_MQ_DestroyNotificationHandle *dnh;
1078 if (NULL != mq->destroy_impl)
1080 mq->destroy_impl (mq, mq->impl_state);
1082 if (NULL != mq->send_task)
1084 GNUNET_SCHEDULER_cancel (mq->send_task);
1085 mq->send_task = NULL;
1087 while (NULL != mq->envelope_head)
1089 struct GNUNET_MQ_Envelope *ev;
1091 ev = mq->envelope_head;
1092 ev->parent_queue = NULL;
1093 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
1096 GNUNET_assert (0 < mq->queue_length);
1098 GNUNET_MQ_discard (ev);
1100 if (NULL != mq->current_envelope)
1102 /* we can only discard envelopes that
1103 * are not queued! */
1104 mq->current_envelope->parent_queue = NULL;
1105 GNUNET_MQ_discard (mq->current_envelope);
1106 mq->current_envelope = NULL;
1107 GNUNET_assert (0 < mq->queue_length);
1110 GNUNET_assert (0 == mq->queue_length);
1111 while (NULL != (dnh = mq->dnh_head))
1113 dnh->cb (dnh->cb_cls);
1114 GNUNET_MQ_destroy_notify_cancel (dnh);
1116 if (NULL != mq->assoc_map)
1118 GNUNET_CONTAINER_multihashmap32_destroy (mq->assoc_map);
1119 mq->assoc_map = NULL;
1121 GNUNET_free_non_null (mq->handlers);
1126 const struct GNUNET_MessageHeader *
1127 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
1130 uint16_t whole_size;
1131 uint16_t nested_size;
1132 const struct GNUNET_MessageHeader *nested_msg;
1134 whole_size = ntohs (mh->size);
1135 GNUNET_assert (whole_size >= base_size);
1136 nested_size = whole_size - base_size;
1137 if (0 == nested_size)
1139 if (nested_size < sizeof (struct GNUNET_MessageHeader))
1141 GNUNET_break_op (0);
1144 nested_msg = (const struct GNUNET_MessageHeader *) ((char *) mh + base_size);
1145 if (ntohs (nested_msg->size) != nested_size)
1147 GNUNET_break_op (0);
1155 * Cancel sending the message. Message must have been sent with
1156 * #GNUNET_MQ_send before. May not be called after the notify sent
1157 * callback has been called
1159 * @param ev queued envelope to cancel
1162 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev)
1164 struct GNUNET_MQ_Handle *mq = ev->parent_queue;
1166 GNUNET_assert (NULL != mq);
1167 GNUNET_assert (NULL != mq->cancel_impl);
1169 mq->evacuate_called = GNUNET_NO;
1171 if (mq->current_envelope == ev)
1173 // complex case, we already started with transmitting
1175 GNUNET_assert (0 < mq->queue_length);
1177 mq->cancel_impl (mq,
1179 // continue sending the next message, if any
1180 if (NULL == mq->envelope_head)
1182 mq->current_envelope = NULL;
1186 mq->current_envelope = mq->envelope_head;
1187 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
1189 mq->current_envelope);
1190 mq->send_notification_called = GNUNET_NO;
1192 mq->current_envelope->mh,
1198 // simple case, message is still waiting in the queue
1199 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
1202 GNUNET_assert (0 < mq->queue_length);
1206 if (GNUNET_YES != mq->evacuate_called)
1208 ev->parent_queue = NULL;
1217 * Function to obtain the current envelope
1218 * from within #GNUNET_MQ_SendImpl implementations.
1220 * @param mq message queue to interrogate
1221 * @return the current envelope
1223 struct GNUNET_MQ_Envelope *
1224 GNUNET_MQ_get_current_envelope (struct GNUNET_MQ_Handle *mq)
1226 return mq->current_envelope;
1231 * Function to obtain the last envelope in the queue.
1233 * @param mq message queue to interrogate
1234 * @return the last envelope in the queue
1236 struct GNUNET_MQ_Envelope *
1237 GNUNET_MQ_get_last_envelope (struct GNUNET_MQ_Handle *mq)
1239 if (NULL != mq->envelope_tail)
1240 return mq->envelope_tail;
1242 return mq->current_envelope;
1247 * Set application-specific options for this envelope.
1248 * Overrides the options set for the queue with
1249 * #GNUNET_MQ_set_options() for this message only.
1251 * @param env message to set options for
1252 * @param flags flags to use (meaning is queue-specific)
1253 * @param extra additional buffer for further data (also queue-specific)
1256 GNUNET_MQ_env_set_options (struct GNUNET_MQ_Envelope *env,
1262 env->have_custom_options = GNUNET_YES;
1267 * Get application-specific options for this envelope.
1269 * @param env message to set options for
1270 * @param[out] flags set to flags to use (meaning is queue-specific)
1271 * @return extra additional buffer for further data (also queue-specific)
1274 GNUNET_MQ_env_get_options (struct GNUNET_MQ_Envelope *env,
1277 struct GNUNET_MQ_Handle *mq = env->parent_queue;
1279 if (GNUNET_YES == env->have_custom_options)
1281 *flags = env->flags;
1289 *flags = mq->default_flags;
1290 return mq->default_extra;
1295 * Set application-specific options for this queue.
1297 * @param mq message queue to set options for
1298 * @param flags flags to use (meaning is queue-specific)
1299 * @param extra additional buffer for further data (also queue-specific)
1302 GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq,
1306 mq->default_flags = flags;
1307 mq->default_extra = extra;
1312 * Register function to be called whenever @a mq is being
1315 * @param mq message queue to watch
1316 * @param cb function to call on @a mq destruction
1317 * @param cb_cls closure for @a cb
1318 * @return handle for #GNUNET_MQ_destroy_notify_cancel().
1320 struct GNUNET_MQ_DestroyNotificationHandle *
1321 GNUNET_MQ_destroy_notify (struct GNUNET_MQ_Handle *mq,
1322 GNUNET_SCHEDULER_TaskCallback cb,
1325 struct GNUNET_MQ_DestroyNotificationHandle *dnh;
1327 dnh = GNUNET_new (struct GNUNET_MQ_DestroyNotificationHandle);
1330 dnh->cb_cls = cb_cls;
1331 GNUNET_CONTAINER_DLL_insert (mq->dnh_head,
1339 * Cancel registration from #GNUNET_MQ_destroy_notify().
1341 * @param dnh handle for registration to cancel
1344 GNUNET_MQ_destroy_notify_cancel (struct GNUNET_MQ_DestroyNotificationHandle *dnh)
1346 struct GNUNET_MQ_Handle *mq = dnh->mq;
1348 GNUNET_CONTAINER_DLL_remove (mq->dnh_head,