src: for every AGPL3.0 file, add SPDX identifier.
[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      SPDX-License-Identifier: AGPL3.0-or-later
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  * Copy an array of handlers, appending AGPL handler.
344  *
345  * Useful if the array has been delared in local memory and needs to be
346  * persisted for future use.
347  *
348  * @param handlers Array of handlers to be copied. Can be NULL (nothing done).
349  * @param agpl_handler function to call for AGPL handling
350  * @param agpl_cls closure for @a agpl_handler
351  * @return A newly allocated array of handlers.
352  *         Needs to be freed with #GNUNET_free.
353  */
354 struct GNUNET_MQ_MessageHandler *
355 GNUNET_MQ_copy_handlers2 (const struct GNUNET_MQ_MessageHandler *handlers,
356                           GNUNET_MQ_MessageCallback agpl_handler,
357                           void *agpl_cls);
358
359
360 /**
361  * Count the handlers in a handler array.
362  *
363  * @param handlers Array of handlers to be counted.
364  * @return The number of handlers in the array.
365  */
366 unsigned int
367 GNUNET_MQ_count_handlers (const struct GNUNET_MQ_MessageHandler *handlers);
368
369
370 /**
371  * Message handler for a specific message type.
372  */
373 struct GNUNET_MQ_MessageHandler
374 {
375   /**
376    * Callback to validate a message of the specified @e type.
377    * The closure given to @e mv will be this struct (not @e ctx).
378    * Using NULL means only size-validation using
379    * @e expected_size.  In this case, @e expected_size must
380    * be non-zero.
381    */
382   GNUNET_MQ_MessageValidationCallback mv;
383
384   /**
385    * Callback, called every time a new message of
386    * the specified @e type has been receied.
387    * The closure given to @e mv will be this struct (not @e ctx).
388    */
389   GNUNET_MQ_MessageCallback cb;
390
391   /**
392    * Closure for @e mv and @e cb.
393    */
394   void *cls;
395
396   /**
397    * Type of the message this handler covers, in host byte order.
398    */
399   uint16_t type;
400
401   /**
402    * Expected size of messages of this type.  Minimum size of the
403    * message if @e mv is non-NULL.  Messages of the given type will be
404    * discarded (and the connection closed with an error reported to
405    * the application) if they do not have the right size.
406    */
407   uint16_t expected_size;
408 };
409
410
411 /**
412  * End-marker for the handlers array
413  */
414 #define GNUNET_MQ_handler_end() { NULL, NULL, NULL, 0, 0 }
415
416
417 /**
418  * Defines a static function @a name which takes as a single argument
419  * a message handler for fixed-sized messages of type @a code and with
420  * a message type argument of @a str.  Given such an argument, the
421  * function @name will return a `struct GNUNET_MQ_MessageHandler`
422  * for the given message type.
423  *
424  * The macro is to be used as follows:
425  * <code>
426  * struct GNUNET_MessageTest { ... }; // must be fixed size
427  * static void
428  * handle_test_message (void *cls,
429  *                      const struct GNUNET_MessageTest *msg)
430  * { ... }
431  *
432  * struct GNUNET_MQ_MessageHandler handlers[] = {
433  *   GNUNET_MQ_hd_fixed_size(test_message,
434  *                           GNUNET_MESSAGE_TYPE_TEST,
435  *                           struct GNUNET_MessageTest,
436  *                           "context"),
437  *   GNUNET_MQ_handler_end()
438  * };
439  *
440  * @param name unique basename for the functions
441  * @param code message type constant
442  * @param str type of the message (a struct)
443  * @param ctx context for the callbacks
444  */
445 #define GNUNET_MQ_hd_fixed_size(name,code,str,ctx)           \
446   ({                                                         \
447     void (*_cb)(void *cls, const str *msg) = &handle_##name; \
448     ((struct GNUNET_MQ_MessageHandler) {                     \
449       NULL, (GNUNET_MQ_MessageCallback) _cb,                 \
450       (ctx), (code), sizeof (str) });                        \
451   })
452
453
454 /**
455  * Defines a static function @a name which takes two arguments and a
456  * context-pointer for validating and handling variable-sized messages
457  * of type @a code and with a message type argument of @a str.  Given
458  * such arguments, the function @name will return a `struct
459  * GNUNET_MQ_MessageHandler` for the given message type.
460  *
461  * The macro is to be used as follows:
462  * <code>
463  * struct GNUNET_MessageTest { ... }; // can be variable size
464  * static int
465  * check_test (void *cls,
466  *             const struct GNUNET_MessageTest *msg)
467  * {
468  *   const char *ctx = cls;
469  *   GNUNET_assert (0 == strcmp ("context", ctx));
470  *   // ...
471  * }
472  * static void
473  * handle_test (void *cls,
474  *              const struct GNUNET_MessageTest *msg)
475  * {
476  *   const char *ctx = cls;
477  *   GNUNET_assert (0 == strcmp ("context", ctx));
478  *   // ...
479  * }
480  *
481  * struct GNUNET_MQ_MessageHandler handlers[] = {
482  *   GNUNET_MQ_hd_var_size(test_message,
483  *                         GNUNET_MESSAGE_TYPE_TEST,
484  *                         struct GNUNET_MessageTest,
485  *                         "context"),
486  *   GNUNET_MQ_handler_end()
487  * };
488  *
489  * @param name unique basename for the functions
490  * @param code message type constant
491  * @param str type of the message (a struct)
492  * @param ctx context for the callbacks
493  */
494 #define GNUNET_MQ_hd_var_size(name,code,str,ctx)             \
495   __extension__ ({                                           \
496     int (*_mv)(void *cls, const str *msg) = &check_##name;   \
497     void (*_cb)(void *cls, const str *msg) = &handle_##name; \
498     ((struct GNUNET_MQ_MessageHandler)                       \
499       { (GNUNET_MQ_MessageValidationCallback) _mv,           \
500         (GNUNET_MQ_MessageCallback) _cb,                     \
501         (ctx), (code), sizeof (str) });                      \
502   })
503
504
505 /**
506  * Insert code for a "check_" function that verifies that
507  * a given variable-length message received over the network
508  * is followed by a 0-terminated string.  If the message @a m
509  * is not followed by a 0-terminated string, an error is logged
510  * and the function is returned with #GNUNET_NO.
511  *
512  * @param an IPC message with proper type to determine
513  *  the size, starting with a `struct GNUNET_MessageHeader`
514  */
515 #define GNUNET_MQ_check_zero_termination(m)           \
516   {                                                   \
517     const char *str = (const char *) &m[1];           \
518     const struct GNUNET_MessageHeader *hdr =          \
519       (const struct GNUNET_MessageHeader *) m;        \
520     uint16_t slen = ntohs (hdr->size) - sizeof (*m);  \
521     if ( (0 == slen) ||                               \
522          (memchr (str, 0, slen) != &str[slen - 1]) )  \
523     {                                                 \
524       GNUNET_break (0);                               \
525       return GNUNET_NO;                               \
526     }                                                 \
527   }
528
529
530 /**
531  * Create a new envelope.
532  *
533  * @param mhp message header to store the allocated message header in, can be NULL
534  * @param size size of the message to allocate
535  * @param type type of the message, will be set in the allocated message
536  * @return the allocated MQ message
537  */
538 struct GNUNET_MQ_Envelope *
539 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
540                 uint16_t size,
541                 uint16_t type);
542
543
544 /**
545  * Create a new envelope by copying an existing message.
546  *
547  * @param hdr header of the message to copy
548  * @return envelope containing @a hdr
549  */
550 struct GNUNET_MQ_Envelope *
551 GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr);
552
553
554 /**
555  * Discard the message queue message, free all
556  * allocated resources. Must be called in the event
557  * that a message is created but should not actually be sent.
558  *
559  * @param mqm the message to discard
560  */
561 void
562 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm);
563
564
565 /**
566  * Function to obtain the current envelope
567  * from within #GNUNET_MQ_SendImpl implementations.
568  *
569  * @param mq message queue to interrogate
570  * @return the current envelope
571  */
572 struct GNUNET_MQ_Envelope *
573 GNUNET_MQ_get_current_envelope (struct GNUNET_MQ_Handle *mq);
574
575
576 /**
577  * Function to copy an envelope.  The envelope must not yet
578  * be in any queue or have any options or callbacks set.
579  *
580  * @param env envelope to copy
581  * @return copy of @a env
582  */
583 struct GNUNET_MQ_Envelope *
584 GNUNET_MQ_env_copy (struct GNUNET_MQ_Envelope *env);
585
586
587 /**
588  * Function to obtain the last envelope in the queue.
589  *
590  * @param mq message queue to interrogate
591  * @return the last envelope in the queue
592  */
593 struct GNUNET_MQ_Envelope *
594 GNUNET_MQ_get_last_envelope (struct GNUNET_MQ_Handle *mq);
595
596
597 /**
598  * Set application-specific options for this envelope.
599  * Overrides the options set for the queue with
600  * #GNUNET_MQ_set_options() for this message only.
601  *
602  * @param env message to set options for
603  * @param flags flags to use (meaning is queue-specific)
604  * @param extra additional buffer for further data (also queue-specific)
605  */
606 void
607 GNUNET_MQ_env_set_options (struct GNUNET_MQ_Envelope *env,
608                            uint64_t flags,
609                            const void *extra);
610
611
612 /**
613  * Get application-specific options for this envelope.
614  *
615  * @param env message to set options for
616  * @param[out] flags set to flags to use (meaning is queue-specific)
617  * @return extra additional buffer for further data (also queue-specific)
618  */
619 const void *
620 GNUNET_MQ_env_get_options (struct GNUNET_MQ_Envelope *env,
621                            uint64_t *flags);
622
623
624 /**
625  * Remove the first envelope that has not yet been sent from the message
626  * queue and return it.
627  *
628  * @param mq queue to remove envelope from
629  * @return NULL if queue is empty (or has no envelope that is not under transmission)
630  */
631 struct GNUNET_MQ_Envelope *
632 GNUNET_MQ_unsent_head (struct GNUNET_MQ_Handle *mq);
633
634
635 /**
636  * Set application-specific options for this queue.
637  *
638  * @param mq message queue to set options for
639  * @param flags flags to use (meaning is queue-specific)
640  * @param extra additional buffer for further data (also queue-specific)
641  */
642 void
643 GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq,
644                        uint64_t flags,
645                        const void *extra);
646
647
648 /**
649  * Obtain the current length of the message queue.
650  *
651  * @param mq queue to inspect
652  * @return number of queued, non-transmitted messages
653  */
654 unsigned int
655 GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq);
656
657
658 /**
659  * Send a message with the given message queue.
660  * May only be called once per message.
661  *
662  * @param mq message queue
663  * @param ev the envelope with the message to send.
664  */
665 void
666 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
667                 struct GNUNET_MQ_Envelope *ev);
668
669
670 /**
671  * Send a copy of a message with the given message queue.
672  * Can be called repeatedly on the same envelope.
673  *
674  * @param mq message queue
675  * @param ev the envelope with the message to send.
676  */
677 void
678 GNUNET_MQ_send_copy (struct GNUNET_MQ_Handle *mq,
679                      const struct GNUNET_MQ_Envelope *ev);
680
681
682 /**
683  * Cancel sending the message. Message must have been sent with
684  * #GNUNET_MQ_send before.  May not be called after the notify sent
685  * callback has been called
686  *
687  * @param ev queued envelope to cancel
688  */
689 void
690 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev);
691
692
693 /**
694  * Associate the assoc_data in @a mq with a unique request id.
695  *
696  * @param mq message queue, id will be unique for the queue
697  * @param assoc_data to associate
698  */
699 uint32_t
700 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
701                      void *assoc_data);
702
703
704 /**
705  * Get the data associated with a @a request_id in a queue
706  *
707  * @param mq the message queue with the association
708  * @param request_id the request id we are interested in
709  * @return the associated data
710  */
711 void *
712 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
713                      uint32_t request_id);
714
715
716 /**
717  * Remove the association for a @a request_id
718  *
719  * @param mq the message queue with the association
720  * @param request_id the request id we want to remove
721  * @return the associated data
722  */
723 void *
724 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
725                         uint32_t request_id);
726
727
728 /**
729  * Create a message queue for the specified handlers.
730  *
731  * @param send function the implements sending messages
732  * @param destroy function that implements destroying the queue
733  * @param cancel function that implements canceling a message
734  * @param impl_state for the queue, passed to @a send, @a destroy and @a cancel
735  * @param handlers array of message handlers
736  * @param error_handler handler for read and write errors
737  * @param cls closure for message handlers and error handler
738  * @return a new message queue
739  */
740 struct GNUNET_MQ_Handle *
741 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
742                                GNUNET_MQ_DestroyImpl destroy,
743                                GNUNET_MQ_CancelImpl cancel,
744                                void *impl_state,
745                                const struct GNUNET_MQ_MessageHandler *handlers,
746                                GNUNET_MQ_ErrorHandler error_handler,
747                                void *cls);
748
749
750 /**
751  * Change the closure argument in all of the `handlers` of the
752  * @a mq.
753  *
754  * @param mq to modify
755  * @param handlers_cls new closure to use
756  */
757 void
758 GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq,
759                                 void *handlers_cls);
760
761
762 /**
763  * Call a callback once the envelope has been sent, that is,
764  * sending it can not be canceled anymore.
765  * There can be only one notify sent callback per envelope.
766  *
767  * @param ev message to call the notify callback for
768  * @param cb the notify callback
769  * @param cb_cls closure for the callback
770  */
771 void
772 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
773                        GNUNET_SCHEDULER_TaskCallback cb,
774                        void *cb_cls);
775
776
777 /**
778  * Destroy the message queue.
779  *
780  * @param mq message queue to destroy
781  */
782 void
783 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq);
784
785
786 /**
787  * Handle we return for callbacks registered to be
788  * notified when #GNUNET_MQ_destroy() is called on a queue.
789  */
790 struct GNUNET_MQ_DestroyNotificationHandle;
791
792
793 /**
794  * Register function to be called whenever @a mq is being
795  * destroyed.
796  *
797  * @param mq message queue to watch
798  * @param cb function to call on @a mq destruction
799  * @param cb_cls closure for @a cb
800  * @return handle for #GNUNET_MQ_destroy_notify_cancel().
801  */
802 struct GNUNET_MQ_DestroyNotificationHandle *
803 GNUNET_MQ_destroy_notify (struct GNUNET_MQ_Handle *mq,
804                           GNUNET_SCHEDULER_TaskCallback cb,
805                           void *cb_cls);
806
807 /**
808  * Cancel registration from #GNUNET_MQ_destroy_notify().
809  *
810  * @param dnh handle for registration to cancel
811  */
812 void
813 GNUNET_MQ_destroy_notify_cancel (struct GNUNET_MQ_DestroyNotificationHandle *dnh);
814
815
816 /**
817  * Call the message message handler that was registered
818  * for the type of the given message in the given message queue.
819  *
820  * This function is indended to be used for the implementation
821  * of message queues.
822  *
823  * @param mq message queue with the handlers
824  * @param mh message to dispatch
825  */
826 void
827 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
828                           const struct GNUNET_MessageHeader *mh);
829
830
831 /**
832  * Call the error handler of a message queue with the given
833  * error code.  If there is no error handler, log a warning.
834  *
835  * This function is intended to be used for the implementation
836  * of message queues.
837  *
838  * @param mq message queue
839  * @param error the error type
840  */
841 void
842 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
843                         enum GNUNET_MQ_Error error);
844
845
846 /**
847  * Call the send implementation for the next queued message, if any.
848  * Calls the send notification for the current message unless
849  * #GNUNET_MQ_impl_send_in_flight was called for this envelope.
850  *
851  * Only useful for implementing message queues, results in undefined
852  * behavior if not used carefully.
853  *
854  * @param mq message queue to send the next message with
855  */
856 void
857 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq);
858
859
860 /**
861  * Call the send notification for the current message, but do not
862  * try to send the next message until #gnunet_mq_impl_send_continue
863  * is called.
864  *
865  * Only useful for implementing message queues, results in undefined
866  * behavior if not used carefully.
867  *
868  * @param mq message queue to send the next message with
869  */
870 void
871 GNUNET_MQ_impl_send_in_flight (struct GNUNET_MQ_Handle *mq);
872
873
874 /**
875  * Get the implementation state associated with the
876  * message queue.
877  *
878  * While the GNUNET_MQ_Impl* callbacks receive the
879  * implementation state, continuations that are scheduled
880  * by the implementation function often only have one closure
881  * argument, with this function it is possible to get at the
882  * implementation state when only passing the `struct GNUNET_MQ_Handle`
883  * as closure.
884  *
885  * @param mq message queue with the current message
886  * @return message to send, never NULL
887  */
888 void *
889 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq);
890
891
892 /**
893  * Get the message that should currently be sent.
894  * Fails if there is no current message.
895  * Only useful for implementing message queues,
896  * results in undefined behavior if not used carefully.
897  *
898  * @param mq message queue with the current message
899  * @return message to send, never NULL
900  */
901 const struct GNUNET_MessageHeader *
902 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq);
903
904
905
906 /**
907  * Enum defining all known preference categories.
908  */
909 enum GNUNET_MQ_PreferenceKind
910 {
911
912   /**
913    * No preference was expressed.
914    */
915   GNUNET_MQ_PREFERENCE_NONE = 0,
916
917   /**
918    * The preferred transmission for this envelope focuses on
919    * maximizing bandwidth.
920    */
921   GNUNET_MQ_PREFERENCE_BANDWIDTH = 1,
922
923   /**
924    * The preferred transmission for this envelope foces on
925    * minimizing latency.
926    */
927   GNUNET_MQ_PREFERENCE_LATENCY = 2,
928
929   /**
930    * The preferred transmission for this envelope foces on
931    * reliability.
932    */
933   GNUNET_MQ_PREFERENCE_RELIABILITY = 3
934
935   /**
936    * Number of preference values allowed.
937    */
938 #define GNUNET_MQ_PREFERENCE_COUNT 4
939
940 };
941
942
943 /**
944  * Convert an `enum GNUNET_MQ_PreferenceType` to a string
945  *
946  * @param type the preference type
947  * @return a string or NULL if invalid
948  */
949 const char *
950 GNUNET_MQ_preference_to_string (enum GNUNET_MQ_PreferenceKind type);
951
952
953
954
955 #endif
956
957 /** @} */ /* end of group mq */