TimerPacketSink.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * @file TimerPacketSink.h
  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. #ifndef _TIMERPACKETSINK_H
  23. #define _TIMERPACKETSINK_H
  24. #include <stdio.h>
  25. #include <system/BReactor.h>
  26. #include <flow/PacketPassInterface.h>
  27. typedef struct {
  28. BReactor *reactor;
  29. PacketPassInterface input;
  30. BTimer timer;
  31. } TimerPacketSink;
  32. static void _TimerPacketSink_input_handler_send (TimerPacketSink *s, uint8_t *data, int data_len)
  33. {
  34. printf("sink: send '");
  35. fwrite(data, data_len, 1, stdout);
  36. printf("'\n");
  37. BReactor_SetTimer(s->reactor, &s->timer);
  38. }
  39. static void _TimerPacketSink_input_handler_cancel (TimerPacketSink *s)
  40. {
  41. printf("sink: cancelled\n");
  42. BReactor_RemoveTimer(s->reactor, &s->timer);
  43. }
  44. static void _TimerPacketSink_timer_handler (TimerPacketSink *s)
  45. {
  46. printf("sink: done\n");
  47. PacketPassInterface_Done(&s->input);
  48. }
  49. static void TimerPacketSink_Init (TimerPacketSink *s, BReactor *reactor, int mtu, int ms)
  50. {
  51. // init arguments
  52. s->reactor = reactor;
  53. // init input
  54. PacketPassInterface_Init(&s->input, mtu, (PacketPassInterface_handler_send)_TimerPacketSink_input_handler_send, s, BReactor_PendingGroup(s->reactor));
  55. PacketPassInterface_EnableCancel(&s->input, (PacketPassInterface_handler_cancel)_TimerPacketSink_input_handler_cancel);
  56. // init timer
  57. BTimer_Init(&s->timer, ms, (BTimer_handler)_TimerPacketSink_timer_handler, s);
  58. }
  59. static void TimerPacketSink_Free (TimerPacketSink *s)
  60. {
  61. // free timer
  62. BReactor_RemoveTimer(s->reactor, &s->timer);
  63. // free input
  64. PacketPassInterface_Free(&s->input);
  65. }
  66. static PacketPassInterface * TimerPacketSink_GetInput (TimerPacketSink *s)
  67. {
  68. return &s->input;
  69. }
  70. #endif