Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / include / gnunet_mq_lib.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012-2016 GNUnet e.V.
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @author Florian Dold
23  * @author Christian Grothoff
24  *
25  * @file
26  * General-purpose message queue
27  *
28  * @defgroup mq  MQ library
29  * General-purpose message queue
30  *
31  * @see [Documentation](https://gnunet.org/message-queue-api)
32  *
33  * @{
34  */
35 #ifndef GNUNET_MQ_LIB_H
36 #define GNUNET_MQ_LIB_H
37
38
39 /**
40  * Allocate an envelope, with extra space allocated after the space needed
41  * by the message struct.
42  * The allocated message will already have the type and size field set.
43  *
44  * @param mvar variable to store the allocated message in;
45  *             must have a header field;
46  *             can be NULL
47  * @param esize extra space to allocate after the message
48  * @param type type of the message
49  * @return the MQ message
50  */
51 #define GNUNET_MQ_msg_extra(mvar, esize, type) GNUNET_MQ_msg_(((struct GNUNET_MessageHeader**) &(mvar)), (esize) + sizeof *(mvar), (type))
52
53 /**
54  * Allocate a GNUNET_MQ_Envelope.
55  * The contained message will already have the type and size field set.
56  *
57  * @param mvar variable to store the allocated message in;
58  *             must have a header field;
59  *             can be NULL
60  * @param type type of the message
61  * @return the allocated envelope
62  */
63 #define GNUNET_MQ_msg(mvar, type) GNUNET_MQ_msg_extra(mvar, 0, type)
64
65
66 /**
67  * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header.
68  * The allocated message will already have the type and size field set.
69  *
70  * @param type type of the message
71  */
72 #define GNUNET_MQ_msg_header(type) GNUNET_MQ_msg_ (NULL, sizeof (struct GNUNET_MessageHeader), type)
73
74
75 /**
76  * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header and extra space.
77  * The allocated message will already have the type and size field set.
78  *
79  * @param mh pointer that will changed to point at to the allocated message header
80  * @param esize extra space to allocate after the message header
81  * @param type type of the message
82  */
83 #define GNUNET_MQ_msg_header_extra(mh, esize, type) GNUNET_MQ_msg_ (&mh, (esize) + sizeof (struct GNUNET_MessageHeader), type)
84
85
86 /**
87  * Allocate a GNUNET_MQ_Envelope, and append a payload message after the given
88  * message struct.
89  *
90  * @param mvar pointer to a message struct, will be changed to point at the newly allocated message,
91  *        whose size is 'sizeof(*mvar) + ntohs (mh->size)'
92  * @param type message type of the allocated message, has no effect on the nested message
93  * @param mh message to nest
94  * @return a newly allocated 'struct GNUNET_MQ_Envelope *'
95  */
96 #define GNUNET_MQ_msg_nested_mh(mvar, type, mh) \
97   ({struct GNUNET_MQ_Envelope *_ev;\
98     _ev = GNUNET_MQ_msg_nested_mh_((struct GNUNET_MessageHeader**) &(mvar),\
99                                    sizeof (*(mvar)),\
100                                    (type),\
101                                    (mh));\
102    (void)(mvar)->header; /* type check */\
103    _ev;})
104
105
106 /**
107  * Return a pointer to the message at the end of the given message.
108  *
109  * @param var pointer to a message struct, the type of the expression determines the base size,
110  *        the space after the base size is the nested message
111  * @return a 'struct GNUNET_MessageHeader *' that points at the nested message of the given message,
112  *         or NULL if the given message in @a var does not have any space after the message struct
113  */
114 #define GNUNET_MQ_extract_nested_mh(var) GNUNET_MQ_extract_nested_mh_ ((struct GNUNET_MessageHeader *) (var), sizeof (*(var)))
115
116
117 /**
118  * Implementation of the #GNUNET_MQ_extract_nexted_mh macro.
119  *
120  * @param mh message header to extract nested message header from
121  * @param base_size size of the message before the nested message's header appears
122  * @return pointer to the nested message, does not copy the message
123  *         OR NULL in case of a malformed message.
124  */
125 const struct GNUNET_MessageHeader *
126 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
127                               uint16_t base_size);
128
129
130 /**
131  * Opaque handle to an envelope.
132  */
133 struct GNUNET_MQ_Envelope;
134
135
136 /**
137  * Implementation of the #GNUNET_MQ_msg_nested_mh macro.
138  *
139  * @param mhp pointer to the message header pointer that will be changed to allocate at
140  *        the newly allocated space for the message.
141  * @param base_size size of the data before the nested message
142  * @param type type of the message in the envelope
143  * @param nested_mh the message to append to the message after base_size
144  */
145 struct GNUNET_MQ_Envelope *
146 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
147                           uint16_t base_size,
148                           uint16_t type,
149                           const struct GNUNET_MessageHeader *nested_mh);
150
151
152 /**
153  * Opaque handle to a message queue.
154  */
155 struct GNUNET_MQ_Handle;
156
157
158 /**
159  * Error codes for the queue.
160  */
161 enum GNUNET_MQ_Error
162 {
163   /**
164    * Failed to read message from the network.
165    * FIXME: Likely not properly distinguished
166    * from TIMEOUT case in the code!
167    */
168   GNUNET_MQ_ERROR_READ = 1,
169
170   /**
171    * FIXME: document!
172    */
173   GNUNET_MQ_ERROR_WRITE = 2,
174
175   /**
176    * FIXME: document!
177    */
178   GNUNET_MQ_ERROR_TIMEOUT = 4,
179
180   /**
181    * We received a message that was malformed and thus
182    * could not be passed to its handler.
183    */
184   GNUNET_MQ_ERROR_MALFORMED = 8,
185
186   /**
187    * We received a message for which we have no matching
188    * handler.
189    */
190   GNUNET_MQ_ERROR_NO_MATCH = 16
191 };
192
193
194 /**
195  * Called when a message has been received.
196  *
197  * @param cls closure
198  * @param msg the received message
199  */
200 typedef void
201 (*GNUNET_MQ_MessageCallback) (void *cls,
202                               const struct GNUNET_MessageHeader *msg);
203
204
205 /**
206  * Called when a message needs to be validated.
207  *
208  * @param cls closure
209  * @param msg the received message
210  * @return #GNUNET_OK if the message is well-formed,
211  *         #GNUNET_SYSERR if not
212  */
213 typedef int
214 (*GNUNET_MQ_MessageValidationCallback) (void *cls,
215                                         const struct GNUNET_MessageHeader *msg);
216
217
218 /**
219  * Signature of functions implementing the
220  * sending functionality of a message queue.
221  *
222  * @param mq the message queue
223  * @param msg the message to send
224  * @param impl_state state of the implementation
225  */
226 typedef void
227 (*GNUNET_MQ_SendImpl) (struct GNUNET_MQ_Handle *mq,
228                        const struct GNUNET_MessageHeader *msg,
229                        void *impl_state);
230
231
232 /**
233  * Signature of functions implementing the
234  * destruction of a message queue.
235  * Implementations must not free @a mq, but should
236  * take care of @a impl_state.
237  *
238  * @param mq the message queue to destroy
239  * @param impl_state state of the implementation
240  */
241 typedef void
242 (*GNUNET_MQ_DestroyImpl) (struct GNUNET_MQ_Handle *mq,
243                           void *impl_state);
244
245
246 /**
247  * Implementation function that cancels the currently sent message.
248  *
249  * @param mq message queue
250  * @param impl_state state specific to the implementation
251  */
252 typedef void
253 (*GNUNET_MQ_CancelImpl) (struct GNUNET_MQ_Handle *mq,
254                          void *impl_state);
255
256
257 /**
258  * Generic error handler, called with the appropriate
259  * error code and the same closure specified at the creation of
260  * the message queue.
261  * Not every message queue implementation supports an error handler.
262  *
263  * @param cls closure
264  * @param error error code
265  */
266 typedef void
267 (*GNUNET_MQ_ErrorHandler) (void *cls,
268                            enum GNUNET_MQ_Error error);
269
270
271 /**
272  * Insert @a env into the envelope DLL starting at @a env_head
273  * Note that @a env must not be in any MQ while this function
274  * is used with DLLs defined outside of the MQ module.  This
275  * is just in case some application needs to also manage a
276  * FIFO of envelopes independent of MQ itself and wants to
277  * re-use the pointers internal to @a env.  Use with caution.
278  *
279  * @param[in|out] env_head of envelope DLL
280  * @param[in|out] env_tail tail of envelope DLL
281  * @param[in|out] env element to insert at the tail
282  */
283 void
284 GNUNET_MQ_dll_insert_tail (struct GNUNET_MQ_Envelope **env_head,
285                            struct GNUNET_MQ_Envelope **env_tail,
286                            struct GNUNET_MQ_Envelope *env);
287
288
289 /**
290  * Remove @a env from the envelope DLL starting at @a env_head.
291  * Note that @a env must not be in any MQ while this function
292  * is used with DLLs defined outside of the MQ module. This
293  * is just in case some application needs to also manage a
294  * FIFO of envelopes independent of MQ itself and wants to
295  * re-use the pointers internal to @a env.  Use with caution.
296  *
297  * @param[in|out] env_head of envelope DLL
298  * @param[in|out] env_tail tail of envelope DLL
299  * @param[in|out] env element to remove from the DLL
300  */
301 void
302 GNUNET_MQ_dll_remove (struct GNUNET_MQ_Envelope **env_head,
303                       struct GNUNET_MQ_Envelope **env_tail,
304                       struct GNUNET_MQ_Envelope *env);
305
306
307 /**
308  * Copy an array of handlers.
309  *
310  * Useful if the array has been delared in local memory and needs to be
311  * persisted for future use.
312  *
313  * @param handlers Array of handlers to be copied.
314  * @return A newly allocated array of handlers.
315  *         Needs to be freed with #GNUNET_free.
316  */
317 struct GNUNET_MQ_MessageHandler *
318 GNUNET_MQ_copy_handlers (const struct GNUNET_MQ_MessageHandler *handlers);
319
320
321 /**
322  * Count the handlers in a handler array.
323  *
324  * @param handlers Array of handlers to be counted.
325  * @return The number of handlers in the array.
326  */
327 unsigned int
328 GNUNET_MQ_count_handlers (const struct GNUNET_MQ_MessageHandler *handlers);
329
330
331 /**
332  * Message handler for a specific message type.
333  */
334 struct GNUNET_MQ_MessageHandler
335 {
336   /**
337    * Callback to validate a message of the specified @e type.
338    * The closure given to @e mv will be this struct (not @e ctx).
339    * Using NULL means only size-validation using
340    * @e expected_size.  In this case, @e expected_size must
341    * be non-zero.
342    */
343   GNUNET_MQ_MessageValidationCallback mv;
344
345   /**
346    * Callback, called every time a new message of
347    * the specified @e type has been receied.
348    * The closure given to @e mv will be this struct (not @e ctx).
349    */
350   GNUNET_MQ_MessageCallback cb;
351
352   /**
353    * Closure for @e mv and @e cb.
354    */
355   void *cls;
356
357   /**
358    * Type of the message this handler covers, in host byte order.
359    */
360   uint16_t type;
361
362   /**
363    * Expected size of messages of this type.  Minimum size of the
364    * message if @e mv is non-NULL.  Messages of the given type will be
365    * discarded (and the connection closed with an error reported to
366    * the application) if they do not have the right size.
367    */
368   uint16_t expected_size;
369 };
370
371
372 /**
373  * End-marker for the handlers array
374  */
375 #define GNUNET_MQ_handler_end() { NULL, NULL, NULL, 0, 0 }
376
377
378 /**
379  * Defines a static function @a name which takes as a single argument
380  * a message handler for fixed-sized messages of type @a code and with
381  * a message type argument of @a str.  Given such an argument, the
382  * function @name will return a `struct GNUNET_MQ_MessageHandler`
383  * for the given message type.
384  *
385  * The macro is to be used as follows:
386  * <code>
387  * struct GNUNET_MessageTest { ... }; // must be fixed size
388  * static void
389  * handle_test_message (void *cls,
390  *                      const struct GNUNET_MessageTest *msg)
391  * { ... }
392  *
393  * struct GNUNET_MQ_MessageHandler handlers[] = {
394  *   GNUNET_MQ_hd_fixed_size(test_message,
395  *                           GNUNET_MESSAGE_TYPE_TEST,
396  *                           struct GNUNET_MessageTest,
397  *                           "context"),
398  *   GNUNET_MQ_handler_end()
399  * };
400  *
401  * @param name unique basename for the functions
402  * @param code message type constant
403  * @param str type of the message (a struct)
404  * @param ctx context for the callbacks
405  */
406 #define GNUNET_MQ_hd_fixed_size(name,code,str,ctx)           \
407   ({                                                         \
408     void (*_cb)(void *cls, const str *msg) = &handle_##name; \
409     ((struct GNUNET_MQ_MessageHandler) {                     \
410       NULL, (GNUNET_MQ_MessageCallback) _cb,                 \
411       (ctx), (code), sizeof (str) });                        \
412   })
413
414
415 /**
416  * Defines a static function @a name which takes two arguments and a
417  * context-pointer for validating and handling variable-sized messages
418  * of type @a code and with a message type argument of @a str.  Given
419  * such arguments, the function @name will return a `struct
420  * GNUNET_MQ_MessageHandler` for the given message type.
421  *
422  * The macro is to be used as follows:
423  * <code>
424  * struct GNUNET_MessageTest { ... }; // can be variable size
425  * static int
426  * check_test (void *cls,
427  *             const struct GNUNET_MessageTest *msg)
428  * {
429  *   const char *ctx = cls;
430  *   GNUNET_assert (0 == strcmp ("context", ctx));
431  *   // ...
432  * }
433  * static void
434  * handle_test (void *cls,
435  *              const struct GNUNET_MessageTest *msg)
436  * {
437  *   const char *ctx = cls;
438  *   GNUNET_assert (0 == strcmp ("context", ctx));
439  *   // ...
440  * }
441  *
442  * struct GNUNET_MQ_MessageHandler handlers[] = {
443  *   GNUNET_MQ_hd_var_size(test_message,
444  *                         GNUNET_MESSAGE_TYPE_TEST,
445  *                         struct GNUNET_MessageTest,
446  *                         "context"),
447  *   GNUNET_MQ_handler_end()
448  * };
449  *
450  * @param name unique basename for the functions
451  * @param code message type constant
452  * @param str type of the message (a struct)
453  * @param ctx context for the callbacks
454  */
455 #define GNUNET_MQ_hd_var_size(name,code,str,ctx)             \
456   __extension__ ({                                           \
457     int (*_mv)(void *cls, const str *msg) = &check_##name;   \
458     void (*_cb)(void *cls, const str *msg) = &handle_##name; \
459     ((struct GNUNET_MQ_MessageHandler)                       \
460       { (GNUNET_MQ_MessageValidationCallback) _mv,           \
461         (GNUNET_MQ_MessageCallback) _cb,                     \
462         (ctx), (code), sizeof (str) });                      \
463   })
464
465
466 /**
467  * Create a new envelope.
468  *
469  * @param mhp message header to store the allocated message header in, can be NULL
470  * @param size size of the message to allocate
471  * @param type type of the message, will be set in the allocated message
472  * @return the allocated MQ message
473  */
474 struct GNUNET_MQ_Envelope *
475 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
476                 uint16_t size,
477                 uint16_t type);
478
479
480 /**
481  * Create a new envelope by copying an existing message.
482  *
483  * @param hdr header of the message to copy
484  * @return envelope containing @a hdr
485  */
486 struct GNUNET_MQ_Envelope *
487 GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr);
488
489
490 /**
491  * Discard the message queue message, free all
492  * allocated resources. Must be called in the event
493  * that a message is created but should not actually be sent.
494  *
495  * @param mqm the message to discard
496  */
497 void
498 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm);
499
500
501 /**
502  * Function to obtain the current envelope
503  * from within #GNUNET_MQ_SendImpl implementations.
504  *
505  * @param mq message queue to interrogate
506  * @return the current envelope
507  */
508 struct GNUNET_MQ_Envelope *
509 GNUNET_MQ_get_current_envelope (struct GNUNET_MQ_Handle *mq);
510
511
512 /**
513  * Function to copy an envelope.  The envelope must not yet
514  * be in any queue or have any options or callbacks set.
515  *
516  * @param env envelope to copy
517  * @return copy of @a env
518  */
519 struct GNUNET_MQ_Envelope *
520 GNUNET_MQ_env_copy (struct GNUNET_MQ_Envelope *env);
521
522
523 /**
524  * Function to obtain the last envelope in the queue.
525  *
526  * @param mq message queue to interrogate
527  * @return the last envelope in the queue
528  */
529 struct GNUNET_MQ_Envelope *
530 GNUNET_MQ_get_last_envelope (struct GNUNET_MQ_Handle *mq);
531
532
533 /**
534  * Set application-specific options for this envelope.
535  * Overrides the options set for the queue with
536  * #GNUNET_MQ_set_options() for this message only.
537  *
538  * @param env message to set options for
539  * @param flags flags to use (meaning is queue-specific)
540  * @param extra additional buffer for further data (also queue-specific)
541  */
542 void
543 GNUNET_MQ_env_set_options (struct GNUNET_MQ_Envelope *env,
544                            uint64_t flags,
545                            const void *extra);
546
547
548 /**
549  * Get application-specific options for this envelope.
550  *
551  * @param env message to set options for
552  * @param[out] flags set to flags to use (meaning is queue-specific)
553  * @return extra additional buffer for further data (also queue-specific)
554  */
555 const void *
556 GNUNET_MQ_env_get_options (struct GNUNET_MQ_Envelope *env,
557                            uint64_t *flags);
558
559
560 /**
561  * Remove the first envelope that has not yet been sent from the message
562  * queue and return it.
563  *
564  * @param mq queue to remove envelope from
565  * @return NULL if queue is empty (or has no envelope that is not under transmission)
566  */
567 struct GNUNET_MQ_Envelope *
568 GNUNET_MQ_unsent_head (struct GNUNET_MQ_Handle *mq);
569
570
571 /**
572  * Set application-specific options for this queue.
573  *
574  * @param mq message queue to set options for
575  * @param flags flags to use (meaning is queue-specific)
576  * @param extra additional buffer for further data (also queue-specific)
577  */
578 void
579 GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq,
580                        uint64_t flags,
581                        const void *extra);
582
583
584 /**
585  * Obtain the current length of the message queue.
586  *
587  * @param mq queue to inspect
588  * @return number of queued, non-transmitted messages
589  */
590 unsigned int
591 GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq);
592
593
594 /**
595  * Send a message with the given message queue.
596  * May only be called once per message.
597  *
598  * @param mq message queue
599  * @param ev the envelope with the message to send.
600  */
601 void
602 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
603                 struct GNUNET_MQ_Envelope *ev);
604
605
606 /**
607  * Send a copy of a message with the given message queue.
608  * Can be called repeatedly on the same envelope.
609  *
610  * @param mq message queue
611  * @param ev the envelope with the message to send.
612  */
613 void
614 GNUNET_MQ_send_copy (struct GNUNET_MQ_Handle *mq,
615                      const struct GNUNET_MQ_Envelope *ev);
616
617
618 /**
619  * Cancel sending the message. Message must have been sent with
620  * #GNUNET_MQ_send before.  May not be called after the notify sent
621  * callback has been called
622  *
623  * @param ev queued envelope to cancel
624  */
625 void
626 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev);
627
628
629 /**
630  * Associate the assoc_data in @a mq with a unique request id.
631  *
632  * @param mq message queue, id will be unique for the queue
633  * @param assoc_data to associate
634  */
635 uint32_t
636 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
637                      void *assoc_data);
638
639
640 /**
641  * Get the data associated with a @a request_id in a queue
642  *
643  * @param mq the message queue with the association
644  * @param request_id the request id we are interested in
645  * @return the associated data
646  */
647 void *
648 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
649                      uint32_t request_id);
650
651
652 /**
653  * Remove the association for a @a request_id
654  *
655  * @param mq the message queue with the association
656  * @param request_id the request id we want to remove
657  * @return the associated data
658  */
659 void *
660 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
661                         uint32_t request_id);
662
663
664 /**
665  * Create a message queue for the specified handlers.
666  *
667  * @param send function the implements sending messages
668  * @param destroy function that implements destroying the queue
669  * @param cancel function that implements canceling a message
670  * @param impl_state for the queue, passed to @a send, @a destroy and @a cancel
671  * @param handlers array of message handlers
672  * @param error_handler handler for read and write errors
673  * @param cls closure for message handlers and error handler
674  * @return a new message queue
675  */
676 struct GNUNET_MQ_Handle *
677 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
678                                GNUNET_MQ_DestroyImpl destroy,
679                                GNUNET_MQ_CancelImpl cancel,
680                                void *impl_state,
681                                const struct GNUNET_MQ_MessageHandler *handlers,
682                                GNUNET_MQ_ErrorHandler error_handler,
683                                void *cls);
684
685
686 /**
687  * Change the closure argument in all of the `handlers` of the
688  * @a mq.
689  *
690  * @param mq to modify
691  * @param handlers_cls new closure to use
692  */
693 void
694 GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq,
695                                 void *handlers_cls);
696
697
698 /**
699  * Call a callback once the envelope has been sent, that is,
700  * sending it can not be canceled anymore.
701  * There can be only one notify sent callback per envelope.
702  *
703  * @param ev message to call the notify callback for
704  * @param cb the notify callback
705  * @param cb_cls closure for the callback
706  */
707 void
708 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
709                        GNUNET_SCHEDULER_TaskCallback cb,
710                        void *cb_cls);
711
712
713 /**
714  * Destroy the message queue.
715  *
716  * @param mq message queue to destroy
717  */
718 void
719 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq);
720
721
722 /**
723  * Handle we return for callbacks registered to be
724  * notified when #GNUNET_MQ_destroy() is called on a queue.
725  */
726 struct GNUNET_MQ_DestroyNotificationHandle;
727
728
729 /**
730  * Register function to be called whenever @a mq is being
731  * destroyed.
732  *
733  * @param mq message queue to watch
734  * @param cb function to call on @a mq destruction
735  * @param cb_cls closure for @a cb
736  * @return handle for #GNUNET_MQ_destroy_notify_cancel().
737  */
738 struct GNUNET_MQ_DestroyNotificationHandle *
739 GNUNET_MQ_destroy_notify (struct GNUNET_MQ_Handle *mq,
740                           GNUNET_SCHEDULER_TaskCallback cb,
741                           void *cb_cls);
742
743 /**
744  * Cancel registration from #GNUNET_MQ_destroy_notify().
745  *
746  * @param dnh handle for registration to cancel
747  */
748 void
749 GNUNET_MQ_destroy_notify_cancel (struct GNUNET_MQ_DestroyNotificationHandle *dnh);
750
751
752 /**
753  * Call the message message handler that was registered
754  * for the type of the given message in the given message queue.
755  *
756  * This function is indended to be used for the implementation
757  * of message queues.
758  *
759  * @param mq message queue with the handlers
760  * @param mh message to dispatch
761  */
762 void
763 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
764                           const struct GNUNET_MessageHeader *mh);
765
766
767 /**
768  * Call the error handler of a message queue with the given
769  * error code.  If there is no error handler, log a warning.
770  *
771  * This function is intended to be used for the implementation
772  * of message queues.
773  *
774  * @param mq message queue
775  * @param error the error type
776  */
777 void
778 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
779                         enum GNUNET_MQ_Error error);
780
781
782 /**
783  * Call the send implementation for the next queued message, if any.
784  * Calls the send notification for the current message unless
785  * #GNUNET_MQ_impl_send_in_flight was called for this envelope.
786  *
787  * Only useful for implementing message queues, results in undefined
788  * behavior if not used carefully.
789  *
790  * @param mq message queue to send the next message with
791  */
792 void
793 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq);
794
795
796 /**
797  * Call the send notification for the current message, but do not
798  * try to send the next message until #gnunet_mq_impl_send_continue
799  * is called.
800  *
801  * Only useful for implementing message queues, results in undefined
802  * behavior if not used carefully.
803  *
804  * @param mq message queue to send the next message with
805  */
806 void
807 GNUNET_MQ_impl_send_in_flight (struct GNUNET_MQ_Handle *mq);
808
809
810 /**
811  * Get the implementation state associated with the
812  * message queue.
813  *
814  * While the GNUNET_MQ_Impl* callbacks receive the
815  * implementation state, continuations that are scheduled
816  * by the implementation function often only have one closure
817  * argument, with this function it is possible to get at the
818  * implementation state when only passing the `struct GNUNET_MQ_Handle`
819  * as closure.
820  *
821  * @param mq message queue with the current message
822  * @return message to send, never NULL
823  */
824 void *
825 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq);
826
827
828 /**
829  * Get the message that should currently be sent.
830  * Fails if there is no current message.
831  * Only useful for implementing message queues,
832  * results in undefined behavior if not used carefully.
833  *
834  * @param mq message queue with the current message
835  * @return message to send, never NULL
836  */
837 const struct GNUNET_MessageHeader *
838 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq);
839
840
841 #endif
842
843 /** @} */ /* end of group mq */