PacketPassInactivityMonitor.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * @file PacketPassInactivityMonitor.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 <flow/PacketPassInactivityMonitor.h>
  23. static void input_handler_send (PacketPassInactivityMonitor *o, uint8_t *data, int data_len)
  24. {
  25. DebugObject_Access(&o->d_obj);
  26. // schedule send
  27. PacketPassInterface_Sender_Send(o->output, data, data_len);
  28. // stop timer
  29. BReactor_RemoveTimer(o->reactor, &o->timer);
  30. }
  31. static void input_handler_requestcancel (PacketPassInactivityMonitor *o)
  32. {
  33. DebugObject_Access(&o->d_obj);
  34. // request cancel
  35. PacketPassInterface_Sender_RequestCancel(o->output);
  36. }
  37. static void output_handler_done (PacketPassInactivityMonitor *o)
  38. {
  39. DebugObject_Access(&o->d_obj);
  40. // output no longer busy, restart timer
  41. BReactor_SetTimer(o->reactor, &o->timer);
  42. // call done
  43. PacketPassInterface_Done(&o->input);
  44. }
  45. static void timer_handler (PacketPassInactivityMonitor *o)
  46. {
  47. DebugObject_Access(&o->d_obj);
  48. // restart timer
  49. BReactor_SetTimer(o->reactor, &o->timer);
  50. // call handler
  51. if (o->handler) {
  52. o->handler(o->user);
  53. return;
  54. }
  55. }
  56. void PacketPassInactivityMonitor_Init (PacketPassInactivityMonitor *o, PacketPassInterface *output, BReactor *reactor, btime_t interval, PacketPassInactivityMonitor_handler handler, void *user)
  57. {
  58. // init arguments
  59. o->output = output;
  60. o->reactor = reactor;
  61. o->handler = handler;
  62. o->user = user;
  63. // init input
  64. PacketPassInterface_Init(&o->input, PacketPassInterface_GetMTU(o->output), (PacketPassInterface_handler_send)input_handler_send, o, BReactor_PendingGroup(o->reactor));
  65. if (PacketPassInterface_HasCancel(o->output)) {
  66. PacketPassInterface_EnableCancel(&o->input, (PacketPassInterface_handler_requestcancel)input_handler_requestcancel);
  67. }
  68. // init output
  69. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  70. // init timer
  71. BTimer_Init(&o->timer, interval, (BTimer_handler)timer_handler, o);
  72. BReactor_SetTimer(o->reactor, &o->timer);
  73. DebugObject_Init(&o->d_obj);
  74. }
  75. void PacketPassInactivityMonitor_Free (PacketPassInactivityMonitor *o)
  76. {
  77. DebugObject_Free(&o->d_obj);
  78. // free timer
  79. BReactor_RemoveTimer(o->reactor, &o->timer);
  80. // free input
  81. PacketPassInterface_Free(&o->input);
  82. }
  83. PacketPassInterface * PacketPassInactivityMonitor_GetInput (PacketPassInactivityMonitor *o)
  84. {
  85. DebugObject_Access(&o->d_obj);
  86. return &o->input;
  87. }
  88. void PacketPassInactivityMonitor_SetHandler (PacketPassInactivityMonitor *o, PacketPassInactivityMonitor_handler handler, void *user)
  89. {
  90. DebugObject_Access(&o->d_obj);
  91. o->handler = handler;
  92. o->user = user;
  93. }