StreamRecvConnector.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * @file StreamRecvConnector.c
  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. #include <stddef.h>
  23. #include <misc/debug.h>
  24. #include <flow/StreamRecvConnector.h>
  25. static int output_handler_recv (StreamRecvConnector *o, uint8_t *data, int data_avail)
  26. {
  27. ASSERT(o->out_avail == -1)
  28. ASSERT(!o->input || !o->in_blocking)
  29. ASSERT(data_avail > 0)
  30. // if we have no input, remember output data
  31. if (!o->input) {
  32. o->out_avail = data_avail;
  33. o->out = data;
  34. return 0;
  35. }
  36. // try to receive data
  37. int res;
  38. while (1) {
  39. DEAD_ENTER_N(obj, o->dead)
  40. DEAD_ENTER_N(inp, o->input_dead)
  41. res = StreamRecvInterface_Receiver_Recv(o->input, data, data_avail);
  42. DEAD_LEAVE_N(obj, o->dead);
  43. DEAD_LEAVE_N(inp, o->input_dead);
  44. if (DEAD_KILLED_N(obj)) {
  45. return -1;
  46. }
  47. if (DEAD_KILLED_N(inp)) {
  48. if (!o->input) {
  49. // lost input
  50. o->out_avail = data_avail;
  51. o->out = data;
  52. return 0;
  53. }
  54. // got a new input, retry
  55. continue;
  56. }
  57. break;
  58. };
  59. ASSERT(res >= 0)
  60. ASSERT(res <= data_avail)
  61. if (res == 0) {
  62. // input blocking
  63. o->out_avail = data_avail;
  64. o->out = data;
  65. o->in_blocking = 1;
  66. return 0;
  67. }
  68. return res;
  69. }
  70. static void input_handler_done (StreamRecvConnector *o, int data_len)
  71. {
  72. ASSERT(o->out_avail > 0)
  73. ASSERT(o->input)
  74. ASSERT(o->in_blocking)
  75. ASSERT(data_len > 0)
  76. ASSERT(data_len <= o->out_avail)
  77. // have no output packet
  78. o->out_avail = -1;
  79. // input not blocking any more
  80. o->in_blocking = 0;
  81. // allow output to receive more packets
  82. StreamRecvInterface_Done(&o->output, data_len);
  83. return;
  84. }
  85. static void job_handler (StreamRecvConnector *o)
  86. {
  87. ASSERT(o->input)
  88. ASSERT(!o->in_blocking)
  89. ASSERT(o->out_avail > 0)
  90. // try to receive data
  91. DEAD_ENTER_N(obj, o->dead)
  92. DEAD_ENTER_N(inp, o->input_dead)
  93. int res = StreamRecvInterface_Receiver_Recv(o->input, o->out, o->out_avail);
  94. DEAD_LEAVE_N(obj, o->dead);
  95. DEAD_LEAVE_N(inp, o->input_dead);
  96. if (DEAD_KILLED_N(obj)) {
  97. return;
  98. }
  99. if (DEAD_KILLED_N(inp)) {
  100. // lost current input. Do nothing here.
  101. // If we gained a new one, its own job is responsible for it.
  102. return;
  103. }
  104. ASSERT(res >= 0)
  105. ASSERT(res <= o->out_avail)
  106. if (res == 0) {
  107. // input blocking
  108. o->in_blocking = 1;
  109. return;
  110. }
  111. // have no output packet
  112. o->out_avail = -1;
  113. // allow output to receive more data
  114. StreamRecvInterface_Done(&o->output, res);
  115. return;
  116. }
  117. void StreamRecvConnector_Init (StreamRecvConnector *o, BPendingGroup *pg)
  118. {
  119. // init dead var
  120. DEAD_INIT(o->dead);
  121. // init output
  122. StreamRecvInterface_Init(&o->output, (StreamRecvInterface_handler_recv)output_handler_recv, o);
  123. // have no output packet
  124. o->out_avail = -1;
  125. // have no input
  126. o->input = NULL;
  127. // init continue job
  128. BPending_Init(&o->continue_job, pg, (BPending_handler)job_handler, o);
  129. // init debug object
  130. DebugObject_Init(&o->d_obj);
  131. }
  132. void StreamRecvConnector_Free (StreamRecvConnector *o)
  133. {
  134. // free debug object
  135. DebugObject_Free(&o->d_obj);
  136. // free continue job
  137. BPending_Free(&o->continue_job);
  138. // free input dead var
  139. if (o->input) {
  140. DEAD_KILL(o->input_dead);
  141. }
  142. // free output
  143. StreamRecvInterface_Free(&o->output);
  144. // free dead var
  145. DEAD_KILL(o->dead);
  146. }
  147. StreamRecvInterface * StreamRecvConnector_GetOutput (StreamRecvConnector *o)
  148. {
  149. return &o->output;
  150. }
  151. void StreamRecvConnector_ConnectInput (StreamRecvConnector *o, StreamRecvInterface *input)
  152. {
  153. ASSERT(!o->input)
  154. // set input
  155. o->input = input;
  156. // init input
  157. StreamRecvInterface_Receiver_Init(o->input, (StreamRecvInterface_handler_done)input_handler_done, o);
  158. // init input dead var
  159. DEAD_INIT(o->input_dead);
  160. // set input not blocking
  161. o->in_blocking = 0;
  162. // if we have an input packet, set continue job
  163. if (o->out_avail > 0) {
  164. BPending_Set(&o->continue_job);
  165. }
  166. }
  167. void StreamRecvConnector_DisconnectInput (StreamRecvConnector *o)
  168. {
  169. ASSERT(o->input)
  170. // unset continue job (in case it wasn't called yet)
  171. BPending_Unset(&o->continue_job);
  172. // free dead var
  173. DEAD_KILL(o->input_dead);
  174. // set no input
  175. o->input = NULL;
  176. }