2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
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 2, 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @author Florian Dold
23 * @file include/gnunet_mq_lib.h
24 * @brief general purpose message queue
29 #include "gnunet_common.h"
33 * Allocate an envelope, with extra space allocated after the space needed
34 * by the message struct.
35 * The allocated message will already have the type and size field set.
37 * @param mvar variable to store the allocated message in;
38 * must have a header field
39 * @param esize extra space to allocate after the message
40 * @param type type of the message
41 * @return the MQ message
43 #define GNUNET_MQ_msg_extra(mvar, esize, type) GNUNET_MQ_msg_((((void)(mvar)->header), (struct GNUNET_MessageHeader**) &(mvar)), (esize) + sizeof *(mvar), (type))
46 * Allocate a GNUNET_MQ_Envelope.
47 * The contained message will already have the type and size field set.
49 * @param mvar variable to store the allocated message in;
50 * must have a header field
51 * @param type type of the message
52 * @return the allocated envelope
54 #define GNUNET_MQ_msg(mvar, type) GNUNET_MQ_msg_extra(mvar, 0, type)
58 * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header.
59 * The allocated message will already have the type and size field set.
61 * @param type type of the message
63 #define GNUNET_MQ_msg_header(type) GNUNET_MQ_msg_ (NULL, sizeof (struct GNUNET_MessageHeader), type)
67 * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header and extra space.
68 * The allocated message will already have the type and size field set.
70 * @param mh pointer that will changed to point at to the allocated message header
71 * @param esize extra space to allocate after the message header
72 * @param type type of the message
74 #define GNUNET_MQ_msg_header_extra(mh, esize, type) GNUNET_MQ_msg_ (&mh, (esize) + sizeof (struct GNUNET_MessageHeader), type)
78 * Allocate a GNUNET_MQ_Envelope, and append a payload message after the given
81 * @param mvar pointer to a message struct, will be changed to point at the newly allocated message,
82 * whose size is 'sizeof(*mvar) + ntohs (mh->size)'
83 * @param type message type of the allocated message, has no effect on the nested message
84 * @param mh message to nest
85 * @return a newly allocated 'struct GNUNET_MQ_Envelope *'
87 #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)
91 * Return a pointer to the message at the end of the given message.
93 * @param var pointer to a message struct, the type of the expression determines the base size,
94 * the space after the base size is the nested message
95 * @return a 'struct GNUNET_MessageHeader *' that points at the nested message of the given message,
96 * or NULL if the given message in 'var' does not have any space after the message struct
98 #define GNUNET_MQ_extract_nested_mh(var) GNUNET_MQ_extract_nested_mh_ ((struct GNUNET_MessageHeader *) (var), sizeof (*(var)))
102 * Implementation of the GNUNET_MQ_extract_nexted_mh macro.
104 * @param mh message header to extract nested message header from
105 * @param base_size size of the message before the nested message's header appears
106 * @return pointer to the nested message, does not copy the message
108 struct GNUNET_MessageHeader *
109 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh, uint16_t base_size);
113 * Implementation of the GNUNET_MQ_msg_nested_mh macro.
115 * @param mhp pointer to the message header pointer that will be changed to allocate at
116 * the newly allocated space for the message.
117 * @param base_size size of the data before the nested message
118 * @param type type of the message in the envelope
119 * @param nested_mh the message to append to the message after base_size
121 struct GNUNET_MQ_Envelope *
122 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp, uint16_t base_size, uint16_t type,
123 const struct GNUNET_MessageHeader *nested_mh);
128 * End-marker for the handlers array
130 #define GNUNET_MQ_HANDLERS_END {NULL, 0, 0}
134 * Opaque handle to a message queue.
136 struct GNUNET_MQ_Handle;
139 * Opaque handle to an envelope.
141 struct GNUNET_MQ_Envelope;
145 GNUNET_MQ_ERROR_READ = 1,
146 GNUNET_MQ_ERROR_WRITE = 2,
147 GNUNET_MQ_ERROR_TIMEOUT = 4
152 * Called when a message has been received.
155 * @param msg the received message
158 (*GNUNET_MQ_MessageCallback) (void *cls,
159 const struct GNUNET_MessageHeader *msg);
163 * Signature of functions implementing the
164 * sending functionality of a message queue.
166 * @param mq the message queue
167 * @param msg the message to send
168 * @param impl_state state of the implementation
171 (*GNUNET_MQ_SendImpl) (struct GNUNET_MQ_Handle *mq,
172 const struct GNUNET_MessageHeader *msg,
177 * Signature of functions implementing the
178 * destruction of a message queue.
179 * Implementations must not free 'mq', but should
180 * take care of 'impl_state'.
182 * @param mq the message queue to destroy
183 * @param impl_state state of the implementation
186 (*GNUNET_MQ_DestroyImpl) (struct GNUNET_MQ_Handle *mq, void *impl_state);
190 * Implementation function that cancels the currently sent message.
192 * @param mq message queue
193 * @param impl_state state specific to the implementation
196 (*GNUNET_MQ_CancelImpl) (struct GNUNET_MQ_Handle *mq, void *impl_state);
200 * Callback used for notifications
205 (*GNUNET_MQ_NotifyCallback) (void *cls);
209 * Generic error handler, called with the appropriate
210 * error code and the same closure specified at the creation of
212 * Not every message queue implementation supports an error handler.
214 * @param cls closure, same closure as for the message handlers
215 * @param error error code
218 (*GNUNET_MQ_ErrorHandler) (void *cls, enum GNUNET_MQ_Error error);
222 * Message handler for a specific message type.
224 struct GNUNET_MQ_MessageHandler
227 * Callback, called every time a new message of
228 * the specified type has been receied.
230 GNUNET_MQ_MessageCallback cb;
234 * Type of the message this handler covers.
239 * Expected size of messages of this type. Use 0 for
240 * variable-size. If non-zero, messages of the given
241 * type will be discarded (and the connection closed)
242 * if they do not have the right size.
244 uint16_t expected_size;
250 * Create a new envelope.
252 * @param mhp message header to store the allocated message header in, can be NULL
253 * @param size size of the message to allocate
254 * @param type type of the message, will be set in the allocated message
255 * @return the allocated MQ message
257 struct GNUNET_MQ_Envelope *
258 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp, uint16_t size, uint16_t type);
262 * Discard the message queue message, free all
263 * allocated resources. Must be called in the event
264 * that a message is created but should not actually be sent.
266 * @param mqm the message to discard
269 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm);
273 * Send a message with the give message queue.
274 * May only be called once per message.
276 * @param mq message queue
277 * @param ev the envelope with the message to send.
280 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev);
284 * Cancel sending the message. Message must have been sent with GNUNET_MQ_send before.
285 * May not be called after the notify sent callback has been called
287 * @param ev queued envelope to cancel
290 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev);
294 * Associate the assoc_data in mq with a unique request id.
296 * @param mq message queue, id will be unique for the queue
297 * @param assoc_data to associate
300 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq, void *assoc_data);
304 * Get the data associated with a request id in a queue
306 * @param mq the message queue with the association
307 * @param request_id the request id we are interested in
308 * @return the associated data
311 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq, uint32_t request_id);
315 * Remove the association for a request id
317 * @param mq the message queue with the association
318 * @param request_id the request id we want to remove
319 * @return the associated data
322 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq, uint32_t request_id);
326 * Create a message queue for a GNUNET_CLIENT_Connection.
327 * If handlers are specfied, receive messages from the connection.
329 * @param connection the client connection
330 * @param handlers handlers for receiving messages
331 * @param error_handler error handler
332 * @param cls closure for the handlers
333 * @return the message queue
335 struct GNUNET_MQ_Handle *
336 GNUNET_MQ_queue_for_connection_client (struct GNUNET_CLIENT_Connection *connection,
337 const struct GNUNET_MQ_MessageHandler *handlers,
338 GNUNET_MQ_ErrorHandler error_handler,
343 * Create a message queue for a GNUNET_STREAM_Socket.
345 * @param client the client
346 * @return the message queue
348 struct GNUNET_MQ_Handle *
349 GNUNET_MQ_queue_for_server_client (struct GNUNET_SERVER_Client *client);
353 * Create a message queue for the specified handlers.
355 * @param send function the implements sending messages
356 * @param destroy function that implements destroying the queue
357 * @param cancel function that implements canceling a message
358 * @param impl_state for the queue, passed to 'send' and 'destroy'
359 * @param handlers array of message handlers
360 * @param error_handler handler for read and write errors
361 * @param cls closure for message handlers and error handler
362 * @return a new message queue
364 struct GNUNET_MQ_Handle *
365 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
366 GNUNET_MQ_DestroyImpl destroy,
367 GNUNET_MQ_CancelImpl cancel,
369 const struct GNUNET_MQ_MessageHandler *handlers,
370 GNUNET_MQ_ErrorHandler error_handler,
376 * Replace the handlers of a message queue with new handlers.
377 * Takes effect immediately, even for messages that already have been received, but for
378 * with the handler has not been called.
380 * If the message queue does not support receiving messages,
381 * this function has no effect.
383 * @param mq message queue
384 * @param new_handlers new handlers
385 * @param cls new closure for the handlers
388 GNUNET_MQ_replace_handlers (struct GNUNET_MQ_Handle *mq,
389 const struct GNUNET_MQ_MessageHandler *new_handlers,
394 * Call a callback once the envelope has been sent, that is,
395 * sending it can not be canceled anymore.
396 * There can be only one notify sent callback per envelope.
398 * @param ev message to call the notify callback for
399 * @param cb the notify callback
400 * @param cls closure for the callback
403 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
404 GNUNET_MQ_NotifyCallback cb,
409 * Destroy the message queue.
411 * @param mq message queue to destroy
414 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq);
418 * Call the message message handler that was registered
419 * for the type of the given message in the given message queue.
421 * This function is indended to be used for the implementation
424 * @param mq message queue with the handlers
425 * @param mh message to dispatch
428 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
429 const struct GNUNET_MessageHeader *mh);
433 * Call the error handler of a message queue with the given
434 * error code. If there is no error handler, log a warning.
436 * This function is intended to be used for the implementation
439 * @param mq message queue
440 * @param error the error type
443 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
444 enum GNUNET_MQ_Error error);
448 * Call the send implementation for the next queued message,
450 * Only useful for implementing message queues,
451 * results in undefined behavior if not used carefully.
453 * @param mq message queue to send the next message with
456 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq);
460 * Get the message that should currently be sent.
461 * Fails if there is no current message.
462 * Only useful for implementing message queues,
463 * results in undefined behavior if not used carefully.
465 * @param mq message queue with the current message
466 * @return message to send, never NULL
468 const struct GNUNET_MessageHeader *
469 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq);
473 * Get the implementation state associated with the
476 * While the GNUNET_MQ_Impl* callbacks receive the
477 * implementation state, continuations that are scheduled
478 * by the implementation function often only have one closure
479 * argument, with this function it is possible to get at the
480 * implementation state when only passing the GNUNET_MQ_Handle
483 * @param mq message queue with the current message
484 * @return message to send, never NULL
487 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq);
491 * Mark the current message as irrevocably sent, but do not
492 * proceed with sending the next message.
493 * Will call the appropriate GNUNET_MQ_NotifyCallback, if any.
495 * @param mq message queue
498 GNUNET_MQ_impl_send_commit (struct GNUNET_MQ_Handle *mq);