PacketPassInactivityMonitor.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * @file PacketPassInactivityMonitor.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * A {@link PacketPassInterface} layer for detecting inactivity.
  32. */
  33. #ifndef BADVPN_PACKETPASSINACTIVITYMONITOR_H
  34. #define BADVPN_PACKETPASSINACTIVITYMONITOR_H
  35. #include <base/DebugObject.h>
  36. #include <system/BReactor.h>
  37. #include <flow/PacketPassInterface.h>
  38. /**
  39. * Handler function invoked when inactivity is detected.
  40. * It is guaranteed that the interfaces are in not sending state.
  41. *
  42. * @param user value given to {@link PacketPassInactivityMonitor_Init}
  43. */
  44. typedef void (*PacketPassInactivityMonitor_handler) (void *user);
  45. /**
  46. * A {@link PacketPassInterface} layer for detecting inactivity.
  47. * It reports inactivity to a user provided handler function.
  48. *
  49. * The object behaves like that:
  50. * ("timer set" means started with the given timeout whether if was running or not,
  51. * "timer unset" means stopped if it was running)
  52. * - There is a timer.
  53. * - The timer is set when the object is initialized.
  54. * - When the input calls Send, the call is passed on to the output.
  55. * If the output accepted the packet, the timer is set. If the output
  56. * blocked the packet, the timer is unset.
  57. * - When the output calls Done, the timer is set, and the call is
  58. * passed on to the input.
  59. * - When the input calls Cancel, the timer is set, and the call is
  60. * passed on to the output.
  61. * - When the timer expires, the timer is set, ant the user's handler
  62. * function is invoked.
  63. */
  64. typedef struct {
  65. DebugObject d_obj;
  66. PacketPassInterface *output;
  67. BReactor *reactor;
  68. PacketPassInactivityMonitor_handler handler;
  69. void *user;
  70. PacketPassInterface input;
  71. BTimer timer;
  72. } PacketPassInactivityMonitor;
  73. /**
  74. * Initializes the object.
  75. * See {@link PacketPassInactivityMonitor} for details.
  76. *
  77. * @param o the object
  78. * @param output output interface
  79. * @param reactor reactor we live in
  80. * @param interval timer value in milliseconds
  81. * @param handler handler function for reporting inactivity, or NULL to disable
  82. * @param user value passed to handler functions
  83. */
  84. void PacketPassInactivityMonitor_Init (PacketPassInactivityMonitor *o, PacketPassInterface *output, BReactor *reactor, btime_t interval, PacketPassInactivityMonitor_handler handler, void *user);
  85. /**
  86. * Frees the object.
  87. *
  88. * @param o the object
  89. */
  90. void PacketPassInactivityMonitor_Free (PacketPassInactivityMonitor *o);
  91. /**
  92. * Returns the input interface.
  93. * The MTU of the interface will be the same as of the output interface.
  94. * The interface supports cancel functionality if the output interface supports it.
  95. *
  96. * @param o the object
  97. * @return input interface
  98. */
  99. PacketPassInterface * PacketPassInactivityMonitor_GetInput (PacketPassInactivityMonitor *o);
  100. /**
  101. * Sets or removes the inactivity handler.
  102. *
  103. * @param o the object
  104. * @param handler handler function for reporting inactivity, or NULL to disable
  105. * @param user value passed to handler functions
  106. */
  107. void PacketPassInactivityMonitor_SetHandler (PacketPassInactivityMonitor *o, PacketPassInactivityMonitor_handler handler, void *user);
  108. /**
  109. * Sets the timer to expire immediately in order to force an inactivity report.
  110. *
  111. * @param o the object
  112. */
  113. void PacketPassInactivityMonitor_Force (PacketPassInactivityMonitor *o);
  114. #endif