StreamPacketSender.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @file StreamPacketSender.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 <misc/debug.h>
  23. #include "StreamPacketSender.h"
  24. static void input_handler_send (StreamPacketSender *o, uint8_t *data, int data_len)
  25. {
  26. DebugObject_Access(&o->d_obj);
  27. ASSERT(data_len > 0)
  28. // limit length to MTU and remember
  29. if (data_len > o->output_mtu) {
  30. o->sending_len = o->output_mtu;
  31. } else {
  32. o->sending_len = data_len;
  33. }
  34. // send
  35. PacketPassInterface_Sender_Send(o->output, data, o->sending_len);
  36. }
  37. static void output_handler_done (StreamPacketSender *o)
  38. {
  39. DebugObject_Access(&o->d_obj);
  40. // done
  41. StreamPassInterface_Done(&o->input, o->sending_len);
  42. }
  43. void StreamPacketSender_Init (StreamPacketSender *o, PacketPassInterface *output, BPendingGroup *pg)
  44. {
  45. ASSERT(PacketPassInterface_GetMTU(output) > 0)
  46. // init arguments
  47. o->output = output;
  48. // remember output MTU
  49. o->output_mtu = PacketPassInterface_GetMTU(output);
  50. // init input
  51. StreamPassInterface_Init(&o->input, (StreamPassInterface_handler_send)input_handler_send, o, pg);
  52. // init output
  53. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  54. DebugObject_Init(&o->d_obj);
  55. }
  56. void StreamPacketSender_Free (StreamPacketSender *o)
  57. {
  58. DebugObject_Free(&o->d_obj);
  59. // free input
  60. StreamPassInterface_Free(&o->input);
  61. }
  62. StreamPassInterface * StreamPacketSender_GetInput (StreamPacketSender *o)
  63. {
  64. DebugObject_Access(&o->d_obj);
  65. return &o->input;
  66. }