StreamRecvInterface.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /**
  2. * @file StreamRecvInterface.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 receiver to receive stream data from a stream sender.
  25. *
  26. * Note that this interface behaves exactly the same and has the same code as
  27. * {@link StreamPassInterface} if names and its external semantics are disregarded.
  28. * If you modify this file, you should probably modify {@link StreamPassInterface}
  29. * too.
  30. */
  31. #ifndef BADVPN_FLOW_STREAMRECVINTERFACE_H
  32. #define BADVPN_FLOW_STREAMRECVINTERFACE_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 sender when {@link StreamRecvInterface_Receiver_Recv} is called
  40. * from the receiver.
  41. * It is guaranteed that the interface is in not receiving state.
  42. * It is guaranteed that the handler is not being called from within Recv or Cancel handlers.
  43. *
  44. * @param user value supplied to {@link StreamRecvInterface_Init}
  45. * @param data pointer to the buffer where data is to be written
  46. * @param data_avail size of the buffer. Will be >0.
  47. * @return - >0 if the sender provides some data immediately, indicating how much data was
  48. * written to the buffer. The interface remains in not receiving state.
  49. * The sender may not use the provided buffer after the handler returns.
  50. * - 0 if the sender cannot provide any data immediately. The interface enters
  51. * receiving state as the handler returns. The sender must write some data to the
  52. * provided buffer and call {@link StreamRecvInterface_Done} when it's done.
  53. */
  54. typedef int (*StreamRecvInterface_handler_recv) (void *user, uint8_t *data, int data_avail);
  55. /**
  56. * Handler called at the receiver when {@link StreamRecvInterface_Done} is called from the sender.
  57. * The sender will no longer use the buffer it was provided with.
  58. * It is guaranteed that the interface was in receiving state.
  59. * The interface enters not receiving state before the handler is called.
  60. * It is guaranteed that the handler is not being called from within Recv or Done handlers.
  61. *
  62. * @param user value supplied to {@link StreamRecvInterface_Receiver_Init}
  63. * @param data_len number of bytes written. Will be >0 and <= the size of the buffer
  64. * that was provided in the previous {@link StreamRecvInterface_Receiver_Recv}
  65. * call.
  66. */
  67. typedef void (*StreamRecvInterface_handler_done) (void *user, int data_len);
  68. /**
  69. * Interface allowing a stream receiver to receive stream data from a stream sender.
  70. * The receiver receives data by providing the sender with a buffer. The sender
  71. * may then either provide some data immediately, or tell the receiver to wait for
  72. * some data to be available and inform it when it's done.
  73. */
  74. typedef struct {
  75. DebugObject d_obj;
  76. // sender data
  77. StreamRecvInterface_handler_recv handler_recv;
  78. void *user_sender;
  79. // receiver data
  80. StreamRecvInterface_handler_done handler_done;
  81. void *user_receiver;
  82. // debug vars
  83. #ifndef NDEBUG
  84. dead_t debug_dead;
  85. int debug_busy_len;
  86. int debug_in_recv;
  87. int debug_in_done;
  88. #endif
  89. } StreamRecvInterface;
  90. /**
  91. * Initializes the interface. The receiver portion must also be initialized
  92. * with {@link StreamRecvInterface_Receiver_Init} before I/O can start.
  93. * The interface is initialized in not receiving state.
  94. *
  95. * @param i the object
  96. * @param handler_recv handler called when the receiver wants to receive data
  97. * @param user arbitrary value that will be passed to sender callback functions
  98. */
  99. static void StreamRecvInterface_Init (StreamRecvInterface *i, StreamRecvInterface_handler_recv handler_recv, void *user);
  100. /**
  101. * Frees the interface.
  102. *
  103. * @param i the object
  104. */
  105. static void StreamRecvInterface_Free (StreamRecvInterface *i);
  106. /**
  107. * Notifies the receiver that the sender has finished providing some data.
  108. * The sender must not use the buffer it was provided any more.
  109. * The interface must be in receiving state.
  110. * The interface enters not receiving state before notifying the receiver.
  111. * Must not be called from within Recv or Done handlers.
  112. *
  113. * Be aware that the receiver may attempt to receive data from within this function.
  114. *
  115. * @param i the object
  116. * @param data_len number of bytes written. Must be >0 and <= the size of the buffer
  117. * that was provided in the previous {@link StreamRecvInterface_handler_recv}
  118. * call.
  119. */
  120. static void StreamRecvInterface_Done (StreamRecvInterface *i, int data_len);
  121. /**
  122. * Initializes the receiver portion of the interface.
  123. *
  124. * @param i the object
  125. * @param handler_done handler called when the sender has finished providing a packet
  126. * @param user arbitrary value that will be passed to receiver callback functions
  127. */
  128. static void StreamRecvInterface_Receiver_Init (StreamRecvInterface *i, StreamRecvInterface_handler_done handler_done, void *user);
  129. /**
  130. * Attempts to receive some data.
  131. * The interface must be in not receiving state.
  132. * Must not be called from within the Recv handler.
  133. *
  134. * @param i the object
  135. * @param data pointer to the buffer where data is to be written
  136. * @param data_avail size of the buffer. Must be >0.
  137. * @return - >0 if some data was provided by the sender imediately, indicating how much data was
  138. * provided. The buffer is no longer needed.
  139. * The interface remains in not receiving state.
  140. * - 0 if no data could not be provided immediately.
  141. * The interface enters receiving state, and the buffer must stay accessible while the
  142. * sender is providing the data. When the sender is done providing it, the
  143. * {@link StreamRecvInterface_handler_done} handler will be called.
  144. */
  145. static int StreamRecvInterface_Receiver_Recv (StreamRecvInterface *i, uint8_t *data, int data_avail);
  146. #ifndef NDEBUG
  147. /**
  148. * Determines if we are in a Recv call.
  149. * Only available if NDEBUG is not defined.
  150. *
  151. * @param i the object
  152. * @return 1 if in a Recv call, 0 if not
  153. */
  154. static int StreamRecvInterface_InClient (StreamRecvInterface *i);
  155. /**
  156. * Determines if we are in a Done call.
  157. * Only available if NDEBUG is not defined.
  158. *
  159. * @param i the object
  160. * @return 1 if in a Done call, 0 if not
  161. */
  162. static int StreamRecvInterface_InDone (StreamRecvInterface *i);
  163. #endif
  164. void StreamRecvInterface_Init (StreamRecvInterface *i, StreamRecvInterface_handler_recv handler_recv, void *user)
  165. {
  166. i->handler_recv = handler_recv;
  167. i->user_sender = user;
  168. i->handler_done = NULL;
  169. i->user_receiver = NULL;
  170. // init debugging
  171. #ifndef NDEBUG
  172. DEAD_INIT(i->debug_dead);
  173. i->debug_busy_len = -1;
  174. i->debug_in_recv = 0;
  175. i->debug_in_done = 0;
  176. #endif
  177. // init debug object
  178. DebugObject_Init(&i->d_obj);
  179. }
  180. void StreamRecvInterface_Free (StreamRecvInterface *i)
  181. {
  182. // free debug object
  183. DebugObject_Free(&i->d_obj);
  184. // free debugging
  185. #ifndef NDEBUG
  186. DEAD_KILL(i->debug_dead);
  187. #endif
  188. }
  189. void StreamRecvInterface_Done (StreamRecvInterface *i, int data_len)
  190. {
  191. ASSERT(i->debug_busy_len > 0)
  192. ASSERT(!i->debug_in_recv)
  193. ASSERT(!i->debug_in_done)
  194. ASSERT(data_len > 0)
  195. ASSERT(data_len <= i->debug_busy_len)
  196. #ifndef NDEBUG
  197. i->debug_busy_len = -1;
  198. i->debug_in_done = 1;
  199. DEAD_ENTER(i->debug_dead)
  200. #endif
  201. i->handler_done(i->user_receiver, data_len);
  202. #ifndef NDEBUG
  203. if (DEAD_LEAVE(i->debug_dead)) {
  204. return;
  205. }
  206. i->debug_in_done = 0;
  207. #endif
  208. }
  209. void StreamRecvInterface_Receiver_Init (StreamRecvInterface *i, StreamRecvInterface_handler_done handler_done, void *user)
  210. {
  211. i->handler_done = handler_done;
  212. i->user_receiver = user;
  213. }
  214. int StreamRecvInterface_Receiver_Recv (StreamRecvInterface *i, uint8_t *data, int data_avail)
  215. {
  216. ASSERT(i->debug_busy_len == -1)
  217. ASSERT(!i->debug_in_recv)
  218. ASSERT(data_avail > 0)
  219. ASSERT(data)
  220. #ifndef NDEBUG
  221. i->debug_in_recv = 1;
  222. DEAD_ENTER(i->debug_dead)
  223. #endif
  224. int res = i->handler_recv(i->user_sender, data, data_avail);
  225. #ifndef NDEBUG
  226. if (DEAD_LEAVE(i->debug_dead)) {
  227. return -1;
  228. }
  229. i->debug_in_recv = 0;
  230. ASSERT(res >= 0)
  231. ASSERT(res <= data_avail)
  232. if (res == 0) {
  233. i->debug_busy_len = data_avail;
  234. }
  235. #endif
  236. return res;
  237. }
  238. #ifndef NDEBUG
  239. int StreamRecvInterface_InClient (StreamRecvInterface *i)
  240. {
  241. return i->debug_in_recv;
  242. }
  243. int StreamRecvInterface_InDone (StreamRecvInterface *i)
  244. {
  245. return i->debug_in_done;
  246. }
  247. #endif
  248. #endif