gnunet_mq_lib.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2012-2013 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. /**
  18. * @author Florian Dold
  19. *
  20. * @file
  21. * General-purpose message queue
  22. *
  23. * @defgroup mq MQ library
  24. * General-purpose message queue
  25. * @{
  26. */
  27. #ifndef GNUNET_MQ_H
  28. #define GNUNET_MQ_H
  29. /**
  30. * Allocate an envelope, with extra space allocated after the space needed
  31. * by the message struct.
  32. * The allocated message will already have the type and size field set.
  33. *
  34. * @param mvar variable to store the allocated message in;
  35. * must have a header field
  36. * @param esize extra space to allocate after the message
  37. * @param type type of the message
  38. * @return the MQ message
  39. */
  40. #define GNUNET_MQ_msg_extra(mvar, esize, type) GNUNET_MQ_msg_(((struct GNUNET_MessageHeader**) &(mvar)), (esize) + sizeof *(mvar), (type))
  41. /**
  42. * Allocate a GNUNET_MQ_Envelope.
  43. * The contained 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. * @param type type of the message
  48. * @return the allocated envelope
  49. */
  50. #define GNUNET_MQ_msg(mvar, type) GNUNET_MQ_msg_extra(mvar, 0, type)
  51. /**
  52. * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header.
  53. * The allocated message will already have the type and size field set.
  54. *
  55. * @param type type of the message
  56. */
  57. #define GNUNET_MQ_msg_header(type) GNUNET_MQ_msg_ (NULL, sizeof (struct GNUNET_MessageHeader), type)
  58. /**
  59. * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header and extra space.
  60. * The allocated message will already have the type and size field set.
  61. *
  62. * @param mh pointer that will changed to point at to the allocated message header
  63. * @param esize extra space to allocate after the message header
  64. * @param type type of the message
  65. */
  66. #define GNUNET_MQ_msg_header_extra(mh, esize, type) GNUNET_MQ_msg_ (&mh, (esize) + sizeof (struct GNUNET_MessageHeader), type)
  67. /**
  68. * Allocate a GNUNET_MQ_Envelope, and append a payload message after the given
  69. * message struct.
  70. *
  71. * @param mvar pointer to a message struct, will be changed to point at the newly allocated message,
  72. * whose size is 'sizeof(*mvar) + ntohs (mh->size)'
  73. * @param type message type of the allocated message, has no effect on the nested message
  74. * @param mh message to nest
  75. * @return a newly allocated 'struct GNUNET_MQ_Envelope *'
  76. */
  77. #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)
  78. /**
  79. * Return a pointer to the message at the end of the given message.
  80. *
  81. * @param var pointer to a message struct, the type of the expression determines the base size,
  82. * the space after the base size is the nested message
  83. * @return a 'struct GNUNET_MessageHeader *' that points at the nested message of the given message,
  84. * or NULL if the given message in @a var does not have any space after the message struct
  85. */
  86. #define GNUNET_MQ_extract_nested_mh(var) GNUNET_MQ_extract_nested_mh_ ((struct GNUNET_MessageHeader *) (var), sizeof (*(var)))
  87. /**
  88. * Implementation of the GNUNET_MQ_extract_nexted_mh macro.
  89. *
  90. * @param mh message header to extract nested message header from
  91. * @param base_size size of the message before the nested message's header appears
  92. * @return pointer to the nested message, does not copy the message
  93. */
  94. const struct GNUNET_MessageHeader *
  95. GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
  96. uint16_t base_size);
  97. /**
  98. * Implementation of the GNUNET_MQ_msg_nested_mh macro.
  99. *
  100. * @param mhp pointer to the message header pointer that will be changed to allocate at
  101. * the newly allocated space for the message.
  102. * @param base_size size of the data before the nested message
  103. * @param type type of the message in the envelope
  104. * @param nested_mh the message to append to the message after base_size
  105. */
  106. struct GNUNET_MQ_Envelope *
  107. GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
  108. uint16_t base_size,
  109. uint16_t type,
  110. const struct GNUNET_MessageHeader *nested_mh);
  111. /**
  112. * End-marker for the handlers array
  113. */
  114. #define GNUNET_MQ_HANDLERS_END {NULL, 0, 0}
  115. /**
  116. * Opaque handle to a message queue.
  117. */
  118. struct GNUNET_MQ_Handle;
  119. /**
  120. * Opaque handle to an envelope.
  121. */
  122. struct GNUNET_MQ_Envelope;
  123. /**
  124. * Error codes for the queue.
  125. */
  126. enum GNUNET_MQ_Error
  127. {
  128. /**
  129. * FIXME: document!
  130. */
  131. GNUNET_MQ_ERROR_READ = 1,
  132. /**
  133. * FIXME: document!
  134. */
  135. GNUNET_MQ_ERROR_WRITE = 2,
  136. /**
  137. * FIXME: document!
  138. */
  139. GNUNET_MQ_ERROR_TIMEOUT = 4
  140. };
  141. /**
  142. * Called when a message has been received.
  143. *
  144. * @param cls closure
  145. * @param msg the received message
  146. */
  147. typedef void
  148. (*GNUNET_MQ_MessageCallback) (void *cls,
  149. const struct GNUNET_MessageHeader *msg);
  150. /**
  151. * Signature of functions implementing the
  152. * sending functionality of a message queue.
  153. *
  154. * @param mq the message queue
  155. * @param msg the message to send
  156. * @param impl_state state of the implementation
  157. */
  158. typedef void
  159. (*GNUNET_MQ_SendImpl) (struct GNUNET_MQ_Handle *mq,
  160. const struct GNUNET_MessageHeader *msg,
  161. void *impl_state);
  162. /**
  163. * Signature of functions implementing the
  164. * destruction of a message queue.
  165. * Implementations must not free @a mq, but should
  166. * take care of @a impl_state.
  167. *
  168. * @param mq the message queue to destroy
  169. * @param impl_state state of the implementation
  170. */
  171. typedef void
  172. (*GNUNET_MQ_DestroyImpl) (struct GNUNET_MQ_Handle *mq,
  173. void *impl_state);
  174. /**
  175. * Implementation function that cancels the currently sent message.
  176. *
  177. * @param mq message queue
  178. * @param impl_state state specific to the implementation
  179. */
  180. typedef void
  181. (*GNUNET_MQ_CancelImpl) (struct GNUNET_MQ_Handle *mq,
  182. void *impl_state);
  183. /**
  184. * Callback used for notifications
  185. *
  186. * @param cls closure
  187. */
  188. typedef void
  189. (*GNUNET_MQ_NotifyCallback) (void *cls);
  190. /**
  191. * Generic error handler, called with the appropriate
  192. * error code and the same closure specified at the creation of
  193. * the message queue.
  194. * Not every message queue implementation supports an error handler.
  195. *
  196. * @param cls closure, same closure as for the message handlers
  197. * @param error error code
  198. */
  199. typedef void
  200. (*GNUNET_MQ_ErrorHandler) (void *cls,
  201. enum GNUNET_MQ_Error error);
  202. /**
  203. * Message handler for a specific message type.
  204. */
  205. struct GNUNET_MQ_MessageHandler
  206. {
  207. /**
  208. * Callback, called every time a new message of
  209. * the specified type has been receied.
  210. */
  211. GNUNET_MQ_MessageCallback cb;
  212. /**
  213. * Type of the message this handler covers.
  214. */
  215. uint16_t type;
  216. /**
  217. * Expected size of messages of this type. Use 0 for
  218. * variable-size. If non-zero, messages of the given
  219. * type will be discarded (and the connection closed)
  220. * if they do not have the right size.
  221. */
  222. uint16_t expected_size;
  223. };
  224. /**
  225. * Create a new envelope.
  226. *
  227. * @param mhp message header to store the allocated message header in, can be NULL
  228. * @param size size of the message to allocate
  229. * @param type type of the message, will be set in the allocated message
  230. * @return the allocated MQ message
  231. */
  232. struct GNUNET_MQ_Envelope *
  233. GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
  234. uint16_t size,
  235. uint16_t type);
  236. /**
  237. * Discard the message queue message, free all
  238. * allocated resources. Must be called in the event
  239. * that a message is created but should not actually be sent.
  240. *
  241. * @param mqm the message to discard
  242. */
  243. void
  244. GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm);
  245. /**
  246. * Send a message with the give message queue.
  247. * May only be called once per message.
  248. *
  249. * @param mq message queue
  250. * @param ev the envelope with the message to send.
  251. */
  252. void
  253. GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
  254. struct GNUNET_MQ_Envelope *ev);
  255. /**
  256. * Cancel sending the message. Message must have been sent with
  257. * #GNUNET_MQ_send before. May not be called after the notify sent
  258. * callback has been called
  259. *
  260. * @param ev queued envelope to cancel
  261. */
  262. void
  263. GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev);
  264. /**
  265. * Associate the assoc_data in mq with a unique request id.
  266. *
  267. * @param mq message queue, id will be unique for the queue
  268. * @param assoc_data to associate
  269. */
  270. uint32_t
  271. GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq, void *assoc_data);
  272. /**
  273. * Get the data associated with a request id in a queue
  274. *
  275. * @param mq the message queue with the association
  276. * @param request_id the request id we are interested in
  277. * @return the associated data
  278. */
  279. void *
  280. GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
  281. uint32_t request_id);
  282. /**
  283. * Remove the association for a request id
  284. *
  285. * @param mq the message queue with the association
  286. * @param request_id the request id we want to remove
  287. * @return the associated data
  288. */
  289. void *
  290. GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
  291. uint32_t request_id);
  292. /**
  293. * Create a message queue for a GNUNET_CLIENT_Connection.
  294. * If handlers are specfied, receive messages from the connection.
  295. *
  296. * @param connection the client connection
  297. * @param handlers handlers for receiving messages
  298. * @param error_handler error handler
  299. * @param cls closure for the handlers
  300. * @return the message queue
  301. */
  302. struct GNUNET_MQ_Handle *
  303. GNUNET_MQ_queue_for_connection_client (struct GNUNET_CLIENT_Connection *connection,
  304. const struct GNUNET_MQ_MessageHandler *handlers,
  305. GNUNET_MQ_ErrorHandler error_handler,
  306. void *cls);
  307. /**
  308. * Create a message queue for a GNUNET_SERVER_Client.
  309. *
  310. * @param client the client
  311. * @return the message queue
  312. */
  313. struct GNUNET_MQ_Handle *
  314. GNUNET_MQ_queue_for_server_client (struct GNUNET_SERVER_Client *client);
  315. /**
  316. * Create a message queue for the specified handlers.
  317. *
  318. * @param send function the implements sending messages
  319. * @param destroy function that implements destroying the queue
  320. * @param cancel function that implements canceling a message
  321. * @param impl_state for the queue, passed to @a send, @a destroy and @a cancel
  322. * @param handlers array of message handlers
  323. * @param error_handler handler for read and write errors
  324. * @param cls closure for message handlers and error handler
  325. * @return a new message queue
  326. */
  327. struct GNUNET_MQ_Handle *
  328. GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
  329. GNUNET_MQ_DestroyImpl destroy,
  330. GNUNET_MQ_CancelImpl cancel,
  331. void *impl_state,
  332. const struct GNUNET_MQ_MessageHandler *handlers,
  333. GNUNET_MQ_ErrorHandler error_handler,
  334. void *cls);
  335. /**
  336. * Replace the handlers of a message queue with new handlers. Takes
  337. * effect immediately, even for messages that already have been
  338. * received, but for with the handler has not been called.
  339. *
  340. * If the message queue does not support receiving messages,
  341. * this function has no effect.
  342. *
  343. * @param mq message queue
  344. * @param new_handlers new handlers
  345. * @param cls new closure for the handlers
  346. */
  347. void
  348. GNUNET_MQ_replace_handlers (struct GNUNET_MQ_Handle *mq,
  349. const struct GNUNET_MQ_MessageHandler *new_handlers,
  350. void *cls);
  351. /**
  352. * Call a callback once the envelope has been sent, that is,
  353. * sending it can not be canceled anymore.
  354. * There can be only one notify sent callback per envelope.
  355. *
  356. * @param ev message to call the notify callback for
  357. * @param cb the notify callback
  358. * @param cls closure for the callback
  359. */
  360. void
  361. GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
  362. GNUNET_MQ_NotifyCallback cb,
  363. void *cls);
  364. /**
  365. * Destroy the message queue.
  366. *
  367. * @param mq message queue to destroy
  368. */
  369. void
  370. GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq);
  371. /**
  372. * Call the message message handler that was registered
  373. * for the type of the given message in the given message queue.
  374. *
  375. * This function is indended to be used for the implementation
  376. * of message queues.
  377. *
  378. * @param mq message queue with the handlers
  379. * @param mh message to dispatch
  380. */
  381. void
  382. GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
  383. const struct GNUNET_MessageHeader *mh);
  384. /**
  385. * Call the error handler of a message queue with the given
  386. * error code. If there is no error handler, log a warning.
  387. *
  388. * This function is intended to be used for the implementation
  389. * of message queues.
  390. *
  391. * @param mq message queue
  392. * @param error the error type
  393. */
  394. void
  395. GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
  396. enum GNUNET_MQ_Error error);
  397. /**
  398. * Call the send implementation for the next queued message,
  399. * if any.
  400. * Only useful for implementing message queues,
  401. * results in undefined behavior if not used carefully.
  402. *
  403. * @param mq message queue to send the next message with
  404. */
  405. void
  406. GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq);
  407. /**
  408. * Get the message that should currently be sent.
  409. * The returned message is only valid until #GNUNET_MQ_impl_send_continue
  410. * is called.
  411. * Fails if there is no current message.
  412. * Only useful for implementing message queues,
  413. * results in undefined behavior if not used carefully.
  414. *
  415. * @param mq message queue with the current message, only valid
  416. * until #GNUNET_MQ_impl_send_continue is called
  417. * @return message to send, never NULL
  418. */
  419. const struct GNUNET_MessageHeader *
  420. GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq);
  421. /**
  422. * Get the implementation state associated with the
  423. * message queue.
  424. *
  425. * While the GNUNET_MQ_Impl* callbacks receive the
  426. * implementation state, continuations that are scheduled
  427. * by the implementation function often only have one closure
  428. * argument, with this function it is possible to get at the
  429. * implementation state when only passing the GNUNET_MQ_Handle
  430. * as closure.
  431. *
  432. * @param mq message queue with the current message
  433. * @return message to send, never NULL
  434. */
  435. void *
  436. GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq);
  437. #endif
  438. /** @} */ /* end of group mq */