2 This file is part of GNUnet.
3 Copyright (C) 2012-2013 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @author Florian Dold
25 * General-purpose message queue
27 * @defgroup mq MQ library
28 * General-purpose message queue
30 * @see [Documentation](https://gnunet.org/message-queue-api)
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.
43 * @param mvar variable to store the allocated message in;
44 * must have a header field
45 * @param esize extra space to allocate after the message
46 * @param type type of the message
47 * @return the MQ message
49 #define GNUNET_MQ_msg_extra(mvar, esize, type) GNUNET_MQ_msg_(((struct GNUNET_MessageHeader**) &(mvar)), (esize) + sizeof *(mvar), (type))
52 * Allocate a GNUNET_MQ_Envelope.
53 * The contained message will already have the type and size field set.
55 * @param mvar variable to store the allocated message in;
56 * must have a header field
57 * @param type type of the message
58 * @return the allocated envelope
60 #define GNUNET_MQ_msg(mvar, type) GNUNET_MQ_msg_extra(mvar, 0, type)
64 * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header.
65 * The allocated message will already have the type and size field set.
67 * @param type type of the message
69 #define GNUNET_MQ_msg_header(type) GNUNET_MQ_msg_ (NULL, sizeof (struct GNUNET_MessageHeader), type)
73 * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header and extra space.
74 * The allocated message will already have the type and size field set.
76 * @param mh pointer that will changed to point at to the allocated message header
77 * @param esize extra space to allocate after the message header
78 * @param type type of the message
80 #define GNUNET_MQ_msg_header_extra(mh, esize, type) GNUNET_MQ_msg_ (&mh, (esize) + sizeof (struct GNUNET_MessageHeader), type)
84 * Allocate a GNUNET_MQ_Envelope, and append a payload message after the given
87 * @param mvar pointer to a message struct, will be changed to point at the newly allocated message,
88 * whose size is 'sizeof(*mvar) + ntohs (mh->size)'
89 * @param type message type of the allocated message, has no effect on the nested message
90 * @param mh message to nest
91 * @return a newly allocated 'struct GNUNET_MQ_Envelope *'
93 #define GNUNET_MQ_msg_nested_mh(mvar, type, mh) GNUNET_MQ_msg_nested_mh_((((void)(mvar)->header), (struct GNUNET_MessageHeader**) &(mvar)), sizeof (*(mvar)), (type), mh)
97 * Return a pointer to the message at the end of the given message.
99 * @param var pointer to a message struct, the type of the expression determines the base size,
100 * the space after the base size is the nested message
101 * @return a 'struct GNUNET_MessageHeader *' that points at the nested message of the given message,
102 * or NULL if the given message in @a var does not have any space after the message struct
104 #define GNUNET_MQ_extract_nested_mh(var) GNUNET_MQ_extract_nested_mh_ ((struct GNUNET_MessageHeader *) (var), sizeof (*(var)))
108 * Implementation of the GNUNET_MQ_extract_nexted_mh macro.
110 * @param mh message header to extract nested message header from
111 * @param base_size size of the message before the nested message's header appears
112 * @return pointer to the nested message, does not copy the message
114 const struct GNUNET_MessageHeader *
115 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
120 * Implementation of the GNUNET_MQ_msg_nested_mh macro.
122 * @param mhp pointer to the message header pointer that will be changed to allocate at
123 * the newly allocated space for the message.
124 * @param base_size size of the data before the nested message
125 * @param type type of the message in the envelope
126 * @param nested_mh the message to append to the message after base_size
128 struct GNUNET_MQ_Envelope *
129 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
132 const struct GNUNET_MessageHeader *nested_mh);
137 * End-marker for the handlers array
139 #define GNUNET_MQ_HANDLERS_END {NULL, 0, 0}
143 * Opaque handle to a message queue.
145 struct GNUNET_MQ_Handle;
148 * Opaque handle to an envelope.
150 struct GNUNET_MQ_Envelope;
154 * Error codes for the queue.
161 GNUNET_MQ_ERROR_READ = 1,
166 GNUNET_MQ_ERROR_WRITE = 2,
171 GNUNET_MQ_ERROR_TIMEOUT = 4
176 * Called when a message has been received.
179 * @param msg the received message
182 (*GNUNET_MQ_MessageCallback) (void *cls,
183 const struct GNUNET_MessageHeader *msg);
187 * Signature of functions implementing the
188 * sending functionality of a message queue.
190 * @param mq the message queue
191 * @param msg the message to send
192 * @param impl_state state of the implementation
195 (*GNUNET_MQ_SendImpl) (struct GNUNET_MQ_Handle *mq,
196 const struct GNUNET_MessageHeader *msg,
201 * Signature of functions implementing the
202 * destruction of a message queue.
203 * Implementations must not free @a mq, but should
204 * take care of @a impl_state.
206 * @param mq the message queue to destroy
207 * @param impl_state state of the implementation
210 (*GNUNET_MQ_DestroyImpl) (struct GNUNET_MQ_Handle *mq,
215 * Implementation function that cancels the currently sent message.
217 * @param mq message queue
218 * @param impl_state state specific to the implementation
221 (*GNUNET_MQ_CancelImpl) (struct GNUNET_MQ_Handle *mq,
226 * Callback used for notifications
231 (*GNUNET_MQ_NotifyCallback) (void *cls);
235 * Generic error handler, called with the appropriate
236 * error code and the same closure specified at the creation of
238 * Not every message queue implementation supports an error handler.
240 * @param cls closure, same closure as for the message handlers
241 * @param error error code
244 (*GNUNET_MQ_ErrorHandler) (void *cls,
245 enum GNUNET_MQ_Error error);
249 * Message handler for a specific message type.
251 struct GNUNET_MQ_MessageHandler
254 * Callback, called every time a new message of
255 * the specified type has been receied.
257 GNUNET_MQ_MessageCallback cb;
260 * Type of the message this handler covers.
265 * Expected size of messages of this type. Use 0 for
266 * variable-size. If non-zero, messages of the given
267 * type will be discarded (and the connection closed)
268 * if they do not have the right size.
270 uint16_t expected_size;
276 * Create a new envelope.
278 * @param mhp message header to store the allocated message header in, can be NULL
279 * @param size size of the message to allocate
280 * @param type type of the message, will be set in the allocated message
281 * @return the allocated MQ message
283 struct GNUNET_MQ_Envelope *
284 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
290 * Discard the message queue message, free all
291 * allocated resources. Must be called in the event
292 * that a message is created but should not actually be sent.
294 * @param mqm the message to discard
297 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm);
301 * Send a message with the give message queue.
302 * May only be called once per message.
304 * @param mq message queue
305 * @param ev the envelope with the message to send.
308 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
309 struct GNUNET_MQ_Envelope *ev);
313 * Cancel sending the message. Message must have been sent with
314 * #GNUNET_MQ_send before. May not be called after the notify sent
315 * callback has been called
317 * @param ev queued envelope to cancel
320 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev);
324 * Associate the assoc_data in mq with a unique request id.
326 * @param mq message queue, id will be unique for the queue
327 * @param assoc_data to associate
330 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq, void *assoc_data);
334 * Get the data associated with a request id in a queue
336 * @param mq the message queue with the association
337 * @param request_id the request id we are interested in
338 * @return the associated data
341 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
342 uint32_t request_id);
346 * Remove the association for a request id
348 * @param mq the message queue with the association
349 * @param request_id the request id we want to remove
350 * @return the associated data
353 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
354 uint32_t request_id);
358 * Create a message queue for a GNUNET_CLIENT_Connection.
359 * If handlers are specfied, receive messages from the connection.
361 * @param connection the client connection
362 * @param handlers handlers for receiving messages
363 * @param error_handler error handler
364 * @param cls closure for the handlers
365 * @return the message queue
367 struct GNUNET_MQ_Handle *
368 GNUNET_MQ_queue_for_connection_client (struct GNUNET_CLIENT_Connection *connection,
369 const struct GNUNET_MQ_MessageHandler *handlers,
370 GNUNET_MQ_ErrorHandler error_handler,
375 * Create a message queue for a GNUNET_SERVER_Client.
377 * @param client the client
378 * @return the message queue
380 struct GNUNET_MQ_Handle *
381 GNUNET_MQ_queue_for_server_client (struct GNUNET_SERVER_Client *client);
385 * Create a message queue for the specified handlers.
387 * @param send function the implements sending messages
388 * @param destroy function that implements destroying the queue
389 * @param cancel function that implements canceling a message
390 * @param impl_state for the queue, passed to @a send, @a destroy and @a cancel
391 * @param handlers array of message handlers
392 * @param error_handler handler for read and write errors
393 * @param cls closure for message handlers and error handler
394 * @return a new message queue
396 struct GNUNET_MQ_Handle *
397 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
398 GNUNET_MQ_DestroyImpl destroy,
399 GNUNET_MQ_CancelImpl cancel,
401 const struct GNUNET_MQ_MessageHandler *handlers,
402 GNUNET_MQ_ErrorHandler error_handler,
407 * Replace the handlers of a message queue with new handlers. Takes
408 * effect immediately, even for messages that already have been
409 * received, but for with the handler has not been called.
411 * If the message queue does not support receiving messages,
412 * this function has no effect.
414 * @param mq message queue
415 * @param new_handlers new handlers
416 * @param cls new closure for the handlers
419 GNUNET_MQ_replace_handlers (struct GNUNET_MQ_Handle *mq,
420 const struct GNUNET_MQ_MessageHandler *new_handlers,
425 * Call a callback once the envelope has been sent, that is,
426 * sending it can not be canceled anymore.
427 * There can be only one notify sent callback per envelope.
429 * @param ev message to call the notify callback for
430 * @param cb the notify callback
431 * @param cls closure for the callback
434 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
435 GNUNET_MQ_NotifyCallback cb,
440 * Destroy the message queue.
442 * @param mq message queue to destroy
445 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq);
449 * Call the message message handler that was registered
450 * for the type of the given message in the given message queue.
452 * This function is indended to be used for the implementation
455 * @param mq message queue with the handlers
456 * @param mh message to dispatch
459 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
460 const struct GNUNET_MessageHeader *mh);
464 * Call the error handler of a message queue with the given
465 * error code. If there is no error handler, log a warning.
467 * This function is intended to be used for the implementation
470 * @param mq message queue
471 * @param error the error type
474 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
475 enum GNUNET_MQ_Error error);
479 * Call the send implementation for the next queued message,
481 * Only useful for implementing message queues,
482 * results in undefined behavior if not used carefully.
484 * @param mq message queue to send the next message with
487 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq);
491 * Get the message that should currently be sent.
492 * The returned message is only valid until #GNUNET_MQ_impl_send_continue
494 * Fails if there is no current message.
495 * Only useful for implementing message queues,
496 * results in undefined behavior if not used carefully.
498 * @param mq message queue with the current message, only valid
499 * until #GNUNET_MQ_impl_send_continue is called
500 * @return message to send, never NULL
502 const struct GNUNET_MessageHeader *
503 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq);
507 * Get the implementation state associated with the
510 * While the GNUNET_MQ_Impl* callbacks receive the
511 * implementation state, continuations that are scheduled
512 * by the implementation function often only have one closure
513 * argument, with this function it is possible to get at the
514 * implementation state when only passing the GNUNET_MQ_Handle
517 * @param mq message queue with the current message
518 * @return message to send, never NULL
521 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq);
526 /** @} */ /* end of group mq */