BPRFileDesc.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // event dispatching
  58. int dispatching;
  59. PRInt16 ready_events;
  60. int current_event_index;
  61. BPending job;
  62. int bottom_type;
  63. PRFileDesc *bottom;
  64. } BPRFileDesc;
  65. /**
  66. * Initializes the object.
  67. *
  68. * @param obj the object
  69. * @param prfd NSPR file descriptor for which notifications are needed.
  70. * Its bottom layer must be a {@link BSocketPRFileDesc}.
  71. * The bottom {@link BSocket} must not have any event handlers
  72. * registered (socket-global or event-specific).
  73. * This object registers a socket-global event handler for
  74. * the bottom {@link BSocket}.
  75. */
  76. void BPRFileDesc_Init (BPRFileDesc *obj, PRFileDesc *prfd);
  77. /**
  78. * Frees the object.
  79. * @param obj the object
  80. */
  81. void BPRFileDesc_Free (BPRFileDesc *obj);
  82. /**
  83. * Registers a handler for an event.
  84. * The event must not already have a handler.
  85. *
  86. * @param obj the object
  87. * @param event NSPR event to register the handler for. Must be
  88. * PR_POLL_READ or PR_POLL_WRITE.
  89. * @param user value to pass to handler
  90. */
  91. void BPRFileDesc_AddEventHandler (BPRFileDesc *obj, PRInt16 event, BPRFileDesc_handler handler, void *user);
  92. /**
  93. * Unregisters a handler for an event.
  94. * The event must have a handler.
  95. *
  96. * @param obj the object
  97. * @param event NSPR event to unregister the handler for
  98. */
  99. void BPRFileDesc_RemoveEventHandler (BPRFileDesc *obj, PRInt16 event);
  100. /**
  101. * Enables monitoring of an event.
  102. * The event must have a handler.
  103. * The event must not be enabled.
  104. * If the operation associated with the event can already be performed,
  105. * the handler for the event may never be called.
  106. *
  107. * @param obj the object
  108. * @param event NSPR event to enable monitoring for
  109. */
  110. void BPRFileDesc_EnableEvent (BPRFileDesc *obj, PRInt16 event);
  111. /**
  112. * Disables monitoring of an event.
  113. * The event must have a handler.
  114. * The event must be enabled.
  115. *
  116. * @param obj the object
  117. * @param event NSPR event to disable monitoring for
  118. */
  119. void BPRFileDesc_DisableEvent (BPRFileDesc *obj, PRInt16 event);
  120. #endif