BPRFileDesc.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * @file BPRFileDesc.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. * @section DESCRIPTION
  23. *
  24. * Object used for obtaining notifications for available I/O operations
  25. * on NSPR file descriptors (PRFileDesc) with supported bottom layers.
  26. * Currently only the {@link BSocketPRFileDesc} bottom layer is supported.
  27. */
  28. #ifndef BADVPN_NSPRSUPPORT_BPRFILEDESC_H
  29. #define BADVPN_NSPRSUPPORT_BPRFILEDESC_H
  30. #include <system/BPending.h>
  31. #include <system/DebugObject.h>
  32. #include <nspr_support/BSocketPRFileDesc.h>
  33. #define BPRFILEDESC_BOTTOM_BSOCKET 1
  34. /**
  35. * Handler function called when an event occurs on the NSPR file descriptor.
  36. * It is guaranteed that the event had a handler and was enabled.
  37. * The event is disabled before the handler is called.
  38. *
  39. * It is guaranteed that the handler returns control to the reactor immediately.
  40. *
  41. * @param user as in {@link BPRFileDesc_AddEventHandler}
  42. * @param event event being reported
  43. */
  44. typedef void (*BPRFileDesc_handler) (void *user, PRInt16 event);
  45. /**
  46. * Object used for obtaining notifications for available I/O operations
  47. * on NSPR file descriptors (PRFileDesc) with supported bottom layers.
  48. * Currently only the {@link BSocketPRFileDesc} bottom layer is supported.
  49. */
  50. typedef struct {
  51. DebugObject d_obj;
  52. BReactor *reactor;
  53. PRFileDesc *prfd;
  54. BPRFileDesc_handler handlers[2];
  55. void *handlers_user[2];
  56. PRInt16 waitEvents;
  57. // bottom
  58. int bottom_type;
  59. PRFileDesc *bottom;
  60. // event dispatching
  61. int dispatching;
  62. PRInt16 ready_events;
  63. int current_event_index;
  64. BPending job;
  65. } BPRFileDesc;
  66. /**
  67. * Initializes the object.
  68. *
  69. * @param obj the object
  70. * @param prfd NSPR file descriptor for which notifications are needed.
  71. * Its bottom layer must be a {@link BSocketPRFileDesc}.
  72. * The bottom {@link BSocket} must not have any event handlers
  73. * registered (socket-global or event-specific).
  74. * This object registers a socket-global event handler for
  75. * the bottom {@link BSocket}.
  76. */
  77. void BPRFileDesc_Init (BPRFileDesc *obj, PRFileDesc *prfd);
  78. /**
  79. * Frees the object.
  80. * @param obj the object
  81. */
  82. void BPRFileDesc_Free (BPRFileDesc *obj);
  83. /**
  84. * Registers a handler for an event.
  85. * The event must not already have a handler.
  86. *
  87. * @param obj the object
  88. * @param event NSPR event to register the handler for. Must be
  89. * PR_POLL_READ or PR_POLL_WRITE.
  90. * @param user value to pass to handler
  91. */
  92. void BPRFileDesc_AddEventHandler (BPRFileDesc *obj, PRInt16 event, BPRFileDesc_handler handler, void *user);
  93. /**
  94. * Unregisters a handler for an event.
  95. * The event must have a handler.
  96. *
  97. * @param obj the object
  98. * @param event NSPR event to unregister the handler for
  99. */
  100. void BPRFileDesc_RemoveEventHandler (BPRFileDesc *obj, PRInt16 event);
  101. /**
  102. * Enables monitoring of an event.
  103. * The event must have a handler.
  104. * The event must not be enabled.
  105. * If the operation associated with the event can already be performed,
  106. * the handler for the event may never be called.
  107. *
  108. * @param obj the object
  109. * @param event NSPR event to enable monitoring for
  110. */
  111. void BPRFileDesc_EnableEvent (BPRFileDesc *obj, PRInt16 event);
  112. /**
  113. * Disables monitoring of an event.
  114. * The event must have a handler.
  115. * The event must be enabled.
  116. *
  117. * @param obj the object
  118. * @param event NSPR event to disable monitoring for
  119. */
  120. void BPRFileDesc_DisableEvent (BPRFileDesc *obj, PRInt16 event);
  121. /**
  122. * Returns the {@link BReactor} used by this object.
  123. *
  124. * @param obj the object
  125. * @return {@link BReactor} used by this object
  126. */
  127. BReactor * BPRFileDesc_Reactor (BPRFileDesc *obj);
  128. #endif