PacketPassInactivityMonitor.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 int input_handler_send (PacketPassInactivityMonitor *o, uint8_t *data, int data_len)
  24. {
  25. // set send called
  26. o->send_called = 1;
  27. // call send
  28. DEAD_ENTER(o->dead)
  29. int res = PacketPassInterface_Sender_Send(o->output, data, data_len);
  30. if (DEAD_LEAVE(o->dead)) {
  31. return -1;
  32. }
  33. ASSERT(res == 0 || res == 1)
  34. if (res == 0) {
  35. // output busy, stop timer
  36. BReactor_RemoveTimer(o->reactor, &o->timer);
  37. } else {
  38. // output accepted packet, restart timer
  39. BReactor_SetTimer(o->reactor, &o->timer);
  40. }
  41. return res;
  42. }
  43. static void input_handler_cancel (PacketPassInactivityMonitor *o)
  44. {
  45. // output no longer busy, restart timer
  46. BReactor_SetTimer(o->reactor, &o->timer);
  47. // call cancel
  48. PacketPassInterface_Sender_Cancel(o->output);
  49. return;
  50. }
  51. static void output_handler_done (PacketPassInactivityMonitor *o)
  52. {
  53. // output no longer busy, restart timer
  54. BReactor_SetTimer(o->reactor, &o->timer);
  55. // call done
  56. PacketPassInterface_Done(&o->input);
  57. return;
  58. }
  59. static void timer_handler (PacketPassInactivityMonitor *o)
  60. {
  61. // restart timer
  62. BReactor_SetTimer(o->reactor, &o->timer);
  63. // call handler
  64. o->handler(o->user);
  65. return;
  66. }
  67. void PacketPassInactivityMonitor_Init (PacketPassInactivityMonitor *o, PacketPassInterface *output, BReactor *reactor, btime_t interval, PacketPassInactivityMonitor_handler handler, void *user)
  68. {
  69. ASSERT(interval > 0)
  70. // init arguments
  71. o->output = output;
  72. o->reactor = reactor;
  73. o->handler = handler;
  74. o->user = user;
  75. // init dead var
  76. DEAD_INIT(o->dead);
  77. // init input
  78. PacketPassInterface_Init(&o->input, PacketPassInterface_GetMTU(o->output), (PacketPassInterface_handler_send)input_handler_send, o);
  79. if (PacketPassInterface_HasCancel(o->output)) {
  80. PacketPassInterface_EnableCancel(&o->input, (PacketPassInterface_handler_cancel)input_handler_cancel);
  81. }
  82. // init output
  83. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  84. // init timer and start it
  85. BTimer_Init(&o->timer, interval, (BTimer_handler)timer_handler, o);
  86. BReactor_SetTimer(o->reactor, &o->timer);
  87. // set send not called
  88. o->send_called = 0;
  89. // init debug object
  90. DebugObject_Init(&o->d_obj);
  91. }
  92. void PacketPassInactivityMonitor_Free (PacketPassInactivityMonitor *o)
  93. {
  94. // free debug object
  95. DebugObject_Free(&o->d_obj);
  96. // free timer
  97. BReactor_RemoveTimer(o->reactor, &o->timer);
  98. // free input
  99. PacketPassInterface_Free(&o->input);
  100. // free dead var
  101. DEAD_KILL(o->dead);
  102. }
  103. PacketPassInterface * PacketPassInactivityMonitor_GetInput (PacketPassInactivityMonitor *o)
  104. {
  105. return &o->input;
  106. }