StreamPassInterface.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /**
  2. * @file StreamPassInterface.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Interface allowing a stream sender to pass stream data to a stream receiver.
  25. *
  26. * Note that this interface behaves exactly the same and has the same code as
  27. * {@link StreamRecvInterface} if names and its external semantics are disregarded.
  28. * If you modify this file, you should probably modify {@link StreamRecvInterface}
  29. * too.
  30. */
  31. #ifndef BADVPN_FLOW_STREAMPASSINTERFACE_H
  32. #define BADVPN_FLOW_STREAMPASSINTERFACE_H
  33. #include <stdint.h>
  34. #include <stddef.h>
  35. #include <misc/dead.h>
  36. #include <misc/debug.h>
  37. #include <system/DebugObject.h>
  38. /**
  39. * Handler called at the receiver when {@link StreamPassInterface_Sender_Send} is called
  40. * from the sender.
  41. * It is guaranteed that the interface is in not sending state.
  42. * It is guaranteed that the handler is not being called from within the Send handler.
  43. *
  44. * @param user value supplied to {@link StreamPassInterface_Init}
  45. * @param data pointer to data being sent
  46. * @param data_len amount of data being sent. Will be >0.
  47. * @return - >0 if the receiver accepts some data immediately, indicating how much of the
  48. * data was accepted. The interface remains in not sending state. The receiver
  49. * may not use the provided data after the handler returns.
  50. * - 0 if the receiver cannot accept any data immediately. The interface enters
  51. * sending state as the handler returns. The receiver may use the provided data
  52. * as long as it needs to. When it's done processing some data and doesn't need
  53. * the data any more, it must call {@link StreamPassInterface_Done}.
  54. */
  55. typedef int (*StreamPassInterface_handler_send) (void *user, uint8_t *data, int data_len);
  56. /**
  57. * Handler called at the sender when {@link StreamPassInterface_Done} is called from the receiver.
  58. * The receiver will no longer use the data it was provided with.
  59. * It is guaranteed that the interface was in sending state.
  60. * The interface enters not sending state before the handler is called.
  61. * It is guaranteed that the handler is not being called from within Send or Done handlers.
  62. *
  63. * @param user value supplied to {@link StreamPassInterface_Sender_Init}
  64. * @param data_len amount of data the receiver processed. Will be >0 and not exceed the amount
  65. * of data submitted in {@link StreamPassInterface_Sender_Send}.
  66. */
  67. typedef void (*StreamPassInterface_handler_done) (void *user, int data_len);
  68. /**
  69. * Interface allowing a stream sender to pass stream data to a stream receiver.
  70. * The sender passes some data by providing the receiver with a pointer
  71. * to the data. The receiver may then either accept some of the data immediately,
  72. * or tell the sender to wait for some data to be processed and inform it
  73. * when it's done.
  74. */
  75. typedef struct
  76. {
  77. DebugObject d_obj;
  78. // receiver data
  79. StreamPassInterface_handler_send handler_send;
  80. void *user_receiver;
  81. // sender data
  82. StreamPassInterface_handler_done handler_done;
  83. void *user_sender;
  84. // debug vars
  85. #ifndef NDEBUG
  86. dead_t debug_dead;
  87. int debug_busy_len;
  88. int debug_in_send;
  89. int debug_in_done;
  90. #endif
  91. } StreamPassInterface;
  92. /**
  93. * Initializes the interface. The sender portion must also be initialized
  94. * with {@link StreamPassInterface_Sender_Init} before I/O can start.
  95. * The interface is initialized in not sending state.
  96. *
  97. * @param i the object
  98. * @param handler_send handler called when the sender wants to send some data
  99. * @param user arbitrary value that will be passed to receiver callback functions
  100. */
  101. static void StreamPassInterface_Init (StreamPassInterface *i, StreamPassInterface_handler_send handler_send, void *user);
  102. /**
  103. * Frees the interface.
  104. *
  105. * @param i the object
  106. */
  107. static void StreamPassInterface_Free (StreamPassInterface *i);
  108. /**
  109. * Notifies the sender that the receiver has finished processing some of the data being sent.
  110. * The receiver must not use the data it was provided any more.
  111. * The interface must be in sending state.
  112. * The interface enters not sending state before notifying the sender.
  113. * Must not be called from within Send or Done handlers.
  114. *
  115. * Be aware that the sender may attempt to send data from within this function.
  116. *
  117. * @param i the object
  118. * @param data_len amount of data processed. Must be >0 and not exceed the amount of data
  119. * the receiver was provided with in {@link StreamPassInterface_handler_send}.
  120. */
  121. static void StreamPassInterface_Done (StreamPassInterface *i, int data_len);
  122. /**
  123. * Initializes the sender portion of the interface.
  124. *
  125. * @param i the object
  126. * @param handler_done handler called when the receiver has finished processing some data
  127. * @param user arbitrary value that will be passed to sender callback functions
  128. */
  129. static void StreamPassInterface_Sender_Init (StreamPassInterface *i, StreamPassInterface_handler_done handler_done, void *user);
  130. /**
  131. * Attempts to send some data.
  132. * The interface must be in not sending state.
  133. * Must not be called from within the Send handler.
  134. *
  135. * @param i the object
  136. * @param data pointer to data to send
  137. * @param data_len amount of data to send. Must be >0.
  138. * @return - >0 if some data was accepted by the receiver immediately, indicating how much of
  139. * the data was accepted. The data is no longer needed.
  140. * The interface remains in not sending state.
  141. * - 0 if no data could not be accepted immediately and is being processed.
  142. * The interface enters sending state, and the data must stay accessible while the
  143. * receiver is processing it. When the receiver is done processing it, the
  144. * {@link StreamPassInterface_handler_done} handler will be called.
  145. */
  146. static int StreamPassInterface_Sender_Send (StreamPassInterface *i, uint8_t *data, int data_len);
  147. #ifndef NDEBUG
  148. /**
  149. * Determines if we are in a Send call.
  150. * Only available if NDEBUG is not defined.
  151. *
  152. * @param i the object
  153. * @return 1 if in a Send call, 0 if not
  154. */
  155. static int StreamPassInterface_InClient (StreamPassInterface *i);
  156. /**
  157. * Determines if we are in a Done call.
  158. * Only available if NDEBUG is not defined.
  159. *
  160. * @param i the object
  161. * @return 1 if in a Done call, 0 if not
  162. */
  163. static int StreamPassInterface_InDone (StreamPassInterface *i);
  164. #endif
  165. void StreamPassInterface_Init (StreamPassInterface *i, StreamPassInterface_handler_send handler_send, void *user)
  166. {
  167. i->handler_send = handler_send;
  168. i->user_receiver = user;
  169. i->handler_done = NULL;
  170. i->user_sender = NULL;
  171. // init debugging
  172. #ifndef NDEBUG
  173. DEAD_INIT(i->debug_dead);
  174. i->debug_busy_len = -1;
  175. i->debug_in_send = 0;
  176. i->debug_in_done = 0;
  177. #endif
  178. // init debug object
  179. DebugObject_Init(&i->d_obj);
  180. }
  181. void StreamPassInterface_Free (StreamPassInterface *i)
  182. {
  183. // free debug object
  184. DebugObject_Free(&i->d_obj);
  185. // free debugging
  186. #ifndef NDEBUG
  187. DEAD_KILL(i->debug_dead);
  188. #endif
  189. }
  190. void StreamPassInterface_Done (StreamPassInterface *i, int data_len)
  191. {
  192. ASSERT(i->debug_busy_len > 0)
  193. ASSERT(!i->debug_in_send)
  194. ASSERT(!i->debug_in_done)
  195. ASSERT(data_len > 0)
  196. ASSERT(data_len <= i->debug_busy_len)
  197. #ifndef NDEBUG
  198. i->debug_busy_len = -1;
  199. i->debug_in_done = 1;
  200. DEAD_ENTER(i->debug_dead)
  201. #endif
  202. i->handler_done(i->user_sender, data_len);
  203. #ifndef NDEBUG
  204. if (DEAD_LEAVE(i->debug_dead)) {
  205. return;
  206. }
  207. i->debug_in_done = 0;
  208. #endif
  209. }
  210. void StreamPassInterface_Sender_Init (StreamPassInterface *i, StreamPassInterface_handler_done handler_done, void *user)
  211. {
  212. i->handler_done = handler_done;
  213. i->user_sender = user;
  214. }
  215. int StreamPassInterface_Sender_Send (StreamPassInterface *i, uint8_t *data, int data_len)
  216. {
  217. ASSERT(i->debug_busy_len == -1)
  218. ASSERT(!i->debug_in_send)
  219. ASSERT(data_len > 0)
  220. ASSERT(data)
  221. #ifndef NDEBUG
  222. i->debug_in_send = 1;
  223. DEAD_ENTER(i->debug_dead)
  224. #endif
  225. int res = i->handler_send(i->user_receiver, data, data_len);
  226. #ifndef NDEBUG
  227. if (DEAD_LEAVE(i->debug_dead)) {
  228. return -1;
  229. }
  230. i->debug_in_send = 0;
  231. ASSERT(res >= 0)
  232. ASSERT(res <= data_len)
  233. if (res == 0) {
  234. i->debug_busy_len = data_len;
  235. }
  236. #endif
  237. return res;
  238. }
  239. #ifndef NDEBUG
  240. int StreamPassInterface_InClient (StreamPassInterface *i)
  241. {
  242. return i->debug_in_send;
  243. }
  244. int StreamPassInterface_InDone (StreamPassInterface *i)
  245. {
  246. return i->debug_in_done;
  247. }
  248. #endif
  249. #endif