SingleStreamSender.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * @file SingleStreamSender.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 "SingleStreamSender.h"
  24. static void output_handler_done (SingleStreamSender *o, int data_len)
  25. {
  26. DebugObject_Access(&o->d_obj);
  27. ASSERT(data_len > 0)
  28. ASSERT(data_len <= o->packet_len - o->pos)
  29. // update position
  30. o->pos += data_len;
  31. // if everything was sent, notify user
  32. if (o->pos == o->packet_len) {
  33. DEBUGERROR(&o->d_err, o->handler(o->user));
  34. return;
  35. }
  36. // send more
  37. StreamPassInterface_Sender_Send(o->output, o->packet + o->pos, o->packet_len - o->pos);
  38. }
  39. void SingleStreamSender_Init (SingleStreamSender *o, uint8_t *packet, int packet_len, StreamPassInterface *output, BPendingGroup *pg, void *user, SingleStreamSender_handler handler)
  40. {
  41. ASSERT(packet_len > 0)
  42. ASSERT(handler)
  43. // init arguments
  44. o->packet = packet;
  45. o->packet_len = packet_len;
  46. o->output = output;
  47. o->user = user;
  48. o->handler = handler;
  49. // set position zero
  50. o->pos = 0;
  51. // init output
  52. StreamPassInterface_Sender_Init(o->output, (StreamPassInterface_handler_done)output_handler_done, o);
  53. // start sending
  54. StreamPassInterface_Sender_Send(o->output, o->packet + o->pos, o->packet_len - o->pos);
  55. DebugError_Init(&o->d_err, pg);
  56. DebugObject_Init(&o->d_obj);
  57. }
  58. void SingleStreamSender_Free (SingleStreamSender *o)
  59. {
  60. DebugObject_Free(&o->d_obj);
  61. DebugError_Free(&o->d_err);
  62. }