TimerPacketSink.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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_requestcancel (TimerPacketSink *s)
  40. {
  41. printf("sink: cancelled\n");
  42. BReactor_RemoveTimer(s->reactor, &s->timer);
  43. PacketPassInterface_Done(&s->input);
  44. }
  45. static void _TimerPacketSink_timer_handler (TimerPacketSink *s)
  46. {
  47. printf("sink: done\n");
  48. PacketPassInterface_Done(&s->input);
  49. }
  50. static void TimerPacketSink_Init (TimerPacketSink *s, BReactor *reactor, int mtu, int ms)
  51. {
  52. // init arguments
  53. s->reactor = reactor;
  54. // init input
  55. PacketPassInterface_Init(&s->input, mtu, (PacketPassInterface_handler_send)_TimerPacketSink_input_handler_send, s, BReactor_PendingGroup(s->reactor));
  56. PacketPassInterface_EnableCancel(&s->input, (PacketPassInterface_handler_requestcancel)_TimerPacketSink_input_handler_requestcancel);
  57. // init timer
  58. BTimer_Init(&s->timer, ms, (BTimer_handler)_TimerPacketSink_timer_handler, s);
  59. }
  60. static void TimerPacketSink_Free (TimerPacketSink *s)
  61. {
  62. // free timer
  63. BReactor_RemoveTimer(s->reactor, &s->timer);
  64. // free input
  65. PacketPassInterface_Free(&s->input);
  66. }
  67. static PacketPassInterface * TimerPacketSink_GetInput (TimerPacketSink *s)
  68. {
  69. return &s->input;
  70. }
  71. #endif