StreamRecvConnector.c 3.1 KB

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