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