StreamPassConnector.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * @file StreamPassConnector.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/StreamPassConnector.h>
  25. static void input_handler_send (StreamPassConnector *o, uint8_t *data, int data_len)
  26. {
  27. ASSERT(data_len > 0)
  28. ASSERT(o->in_len == -1)
  29. DebugObject_Access(&o->d_obj);
  30. // remember input packet
  31. o->in_len = data_len;
  32. o->in = data;
  33. if (o->output) {
  34. // schedule send
  35. StreamPassInterface_Sender_Send(o->output, o->in, o->in_len);
  36. }
  37. }
  38. static void output_handler_done (StreamPassConnector *o, int data_len)
  39. {
  40. ASSERT(data_len > 0)
  41. ASSERT(data_len <= o->in_len)
  42. ASSERT(o->in_len > 0)
  43. ASSERT(o->output)
  44. DebugObject_Access(&o->d_obj);
  45. // have no input packet
  46. o->in_len = -1;
  47. // allow input to send more packets
  48. StreamPassInterface_Done(&o->input, data_len);
  49. }
  50. void StreamPassConnector_Init (StreamPassConnector *o, BPendingGroup *pg)
  51. {
  52. // init output
  53. StreamPassInterface_Init(&o->input, (StreamPassInterface_handler_send)input_handler_send, o, pg);
  54. // have no input packet
  55. o->in_len = -1;
  56. // have no output
  57. o->output = NULL;
  58. DebugObject_Init(&o->d_obj);
  59. }
  60. void StreamPassConnector_Free (StreamPassConnector *o)
  61. {
  62. DebugObject_Free(&o->d_obj);
  63. // free output
  64. StreamPassInterface_Free(&o->input);
  65. }
  66. StreamPassInterface * StreamPassConnector_GetInput (StreamPassConnector *o)
  67. {
  68. DebugObject_Access(&o->d_obj);
  69. return &o->input;
  70. }
  71. void StreamPassConnector_ConnectOutput (StreamPassConnector *o, StreamPassInterface *output)
  72. {
  73. ASSERT(!o->output)
  74. DebugObject_Access(&o->d_obj);
  75. // set output
  76. o->output = output;
  77. // init output
  78. StreamPassInterface_Sender_Init(o->output, (StreamPassInterface_handler_done)output_handler_done, o);
  79. // if we have an input packet, schedule send
  80. if (o->in_len > 0) {
  81. StreamPassInterface_Sender_Send(o->output, o->in, o->in_len);
  82. }
  83. }
  84. void StreamPassConnector_DisconnectOutput (StreamPassConnector *o)
  85. {
  86. ASSERT(o->output)
  87. DebugObject_Access(&o->d_obj);
  88. // set no output
  89. o->output = NULL;
  90. }