StreamRecvConnector.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 void output_handler_recv (StreamRecvConnector *o, uint8_t *data, int data_avail)
  26. {
  27. ASSERT(data_avail > 0)
  28. ASSERT(o->out_avail == -1)
  29. if (o->input) {
  30. ASSERT(!o->in_blocking)
  31. }
  32. // remember output packet
  33. o->out_avail = data_avail;
  34. o->out = data;
  35. if (o->input) {
  36. // schedule receive
  37. StreamRecvInterface_Receiver_Recv(o->input, o->out, o->out_avail);
  38. o->in_blocking = 1;
  39. }
  40. }
  41. static void input_handler_done (StreamRecvConnector *o, int data_len)
  42. {
  43. ASSERT(data_len > 0)
  44. ASSERT(data_len <= o->out_avail)
  45. ASSERT(o->out_avail > 0)
  46. ASSERT(o->input)
  47. ASSERT(o->in_blocking)
  48. // input not blocking any more
  49. o->in_blocking = 0;
  50. // have no output packet
  51. o->out_avail = -1;
  52. // allow output to receive more packets
  53. StreamRecvInterface_Done(&o->output, data_len);
  54. }
  55. void StreamRecvConnector_Init (StreamRecvConnector *o, BPendingGroup *pg)
  56. {
  57. // init output
  58. StreamRecvInterface_Init(&o->output, (StreamRecvInterface_handler_recv)output_handler_recv, o, pg);
  59. // have no output packet
  60. o->out_avail = -1;
  61. // have no input
  62. o->input = NULL;
  63. DebugObject_Init(&o->d_obj);
  64. }
  65. void StreamRecvConnector_Free (StreamRecvConnector *o)
  66. {
  67. DebugObject_Free(&o->d_obj);
  68. // free output
  69. StreamRecvInterface_Free(&o->output);
  70. }
  71. StreamRecvInterface * StreamRecvConnector_GetOutput (StreamRecvConnector *o)
  72. {
  73. DebugObject_Access(&o->d_obj);
  74. return &o->output;
  75. }
  76. void StreamRecvConnector_ConnectInput (StreamRecvConnector *o, StreamRecvInterface *input)
  77. {
  78. ASSERT(!o->input)
  79. DebugObject_Access(&o->d_obj);
  80. // set input
  81. o->input = input;
  82. // init input
  83. StreamRecvInterface_Receiver_Init(o->input, (StreamRecvInterface_handler_done)input_handler_done, o);
  84. // set input not blocking
  85. o->in_blocking = 0;
  86. // if we have an output packet, schedule receive
  87. if (o->out_avail > 0) {
  88. StreamRecvInterface_Receiver_Recv(o->input, o->out, o->out_avail);
  89. o->in_blocking = 1;
  90. }
  91. }
  92. void StreamRecvConnector_DisconnectInput (StreamRecvConnector *o)
  93. {
  94. ASSERT(o->input)
  95. DebugObject_Access(&o->d_obj);
  96. // set no input
  97. o->input = NULL;
  98. }