gnunet_mst_lib.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009-2013, 2016 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your 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. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @author Christian Grothoff
  18. *
  19. * @file
  20. * Library for tokenizing a message stream
  21. * @defgroup server Server library
  22. * Library for tokenizing a message stream
  23. *
  24. * @see [Documentation](https://gnunet.org/mst)
  25. *
  26. * @{
  27. */
  28. #ifndef GNUNET_MST_LIB_H
  29. #define GNUNET_MST_LIB_H
  30. #ifdef __cplusplus
  31. extern "C"
  32. {
  33. #if 0 /* keep Emacsens' auto-indent happy */
  34. }
  35. #endif
  36. #endif
  37. #include "gnunet_common.h"
  38. /**
  39. * Handle to a message stream tokenizer.
  40. */
  41. struct GNUNET_MessageStreamTokenizer;
  42. /**
  43. * Functions with this signature are called whenever a
  44. * complete message is received by the tokenizer.
  45. *
  46. * Do not call #GNUNET_mst_destroy from within
  47. * the scope of this callback.
  48. *
  49. * @param cls closure
  50. * @param message the actual message
  51. * @return #GNUNET_OK on success,
  52. * #GNUNET_NO to stop further processing due to disconnect (no error)
  53. * #GNUNET_SYSERR to stop further processing due to error
  54. */
  55. typedef int
  56. (*GNUNET_MessageTokenizerCallback) (void *cls,
  57. const struct GNUNET_MessageHeader *message);
  58. /**
  59. * Create a message stream tokenizer.
  60. *
  61. * @param cb function to call on completed messages
  62. * @param cb_cls closure for @a cb
  63. * @return handle to tokenizer
  64. */
  65. struct GNUNET_MessageStreamTokenizer *
  66. GNUNET_MST_create (GNUNET_MessageTokenizerCallback cb,
  67. void *cb_cls);
  68. /**
  69. * Add incoming data to the receive buffer and call the
  70. * callback for all complete messages.
  71. *
  72. * @param mst tokenizer to use
  73. * @param buf input data to add
  74. * @param size number of bytes in @a buf
  75. * @param purge should any excess bytes in the buffer be discarded
  76. * (i.e. for packet-based services like UDP)
  77. * @param one_shot only call callback once, keep rest of message in buffer
  78. * @return #GNUNET_OK if we are done processing (need more data)
  79. * #GNUNET_NO if one_shot was set and we have another message ready
  80. * #GNUNET_SYSERR if the data stream is corrupt
  81. */
  82. int
  83. GNUNET_MST_from_buffer (struct GNUNET_MessageStreamTokenizer *mst,
  84. const char *buf,
  85. size_t size,
  86. int purge,
  87. int one_shot);
  88. /**
  89. * Add incoming data to the receive buffer and call the
  90. * callback for all complete messages.
  91. *
  92. * @param mst tokenizer to use
  93. * @param buf input data to add
  94. * @param size number of bytes in @a buf
  95. * @param purge should any excess bytes in the buffer be discarded
  96. * (i.e. for packet-based services like UDP)
  97. * @param one_shot only call callback once, keep rest of message in buffer
  98. * @return #GNUNET_OK if we are done processing (need more data)
  99. * #GNUNET_NO if one_shot was set and we have another message ready
  100. * #GNUNET_SYSERR if the data stream is corrupt
  101. */
  102. int
  103. GNUNET_MST_read (struct GNUNET_MessageStreamTokenizer *mst,
  104. struct GNUNET_NETWORK_Handle *sock,
  105. int purge,
  106. int one_shot);
  107. /**
  108. * Obtain the next message from the @a mst, assuming that
  109. * there are more unprocessed messages in the internal buffer
  110. * of the @a mst.
  111. *
  112. * @param mst tokenizer to use
  113. * @param one_shot only call callback once, keep rest of message in buffer
  114. * @return #GNUNET_OK if we are done processing (need more data)
  115. * #GNUNET_NO if one_shot was set and we have another message ready
  116. * #GNUNET_SYSERR if the data stream is corrupt
  117. */
  118. int
  119. GNUNET_MST_next (struct GNUNET_MessageStreamTokenizer *mst,
  120. int one_shot);
  121. /**
  122. * Destroys a tokenizer.
  123. *
  124. * @param mst tokenizer to destroy
  125. */
  126. void
  127. GNUNET_MST_destroy (struct GNUNET_MessageStreamTokenizer *mst);
  128. #if 0 /* keep Emacsens' auto-indent happy */
  129. {
  130. #endif
  131. #ifdef __cplusplus
  132. }
  133. #endif
  134. #endif
  135. /** @} */ /* end of group server */
  136. /* end of gnunet_mst_lib.h */