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