BPRFileDesc.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 <misc/dead.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. * @param user as in {@link BPRFileDesc_AddEventHandler}
  40. * @param event event being reported
  41. */
  42. typedef void (*BPRFileDesc_handler) (void *user, PRInt16 event);
  43. /**
  44. * Object used for obtaining notifications for available I/O operations
  45. * on NSPR file descriptors (PRFileDesc) with supported bottom layers.
  46. * Currently only the {@link BSocketPRFileDesc} bottom layer is supported.
  47. */
  48. typedef struct {
  49. DebugObject d_obj;
  50. dead_t dead;
  51. BReactor *reactor;
  52. PRFileDesc *prfd;
  53. BPRFileDesc_handler handlers[2];
  54. void *handlers_user[2];
  55. PRInt16 waitEvents;
  56. int bottom_type;
  57. PRFileDesc *bottom;
  58. int in_handler;
  59. } BPRFileDesc;
  60. /**
  61. * Initializes the object.
  62. *
  63. * @param obj the object
  64. * @param prfd NSPR file descriptor for which notifications are needed.
  65. * Its bottom layer must be a {@link BSocketPRFileDesc}.
  66. * The bottom {@link BSocket} must not have any event handlers
  67. * registered (socket-global or event-specific).
  68. * This object registers a socket-global event handler for
  69. * the bottom {@link BSocket}.
  70. */
  71. void BPRFileDesc_Init (BPRFileDesc *obj, PRFileDesc *prfd);
  72. /**
  73. * Frees the object.
  74. * @param obj the object
  75. */
  76. void BPRFileDesc_Free (BPRFileDesc *obj);
  77. /**
  78. * Registers a handler for an event.
  79. * The event must not already have a handler.
  80. *
  81. * @param obj the object
  82. * @param event NSPR event to register the handler for. Must be
  83. * PR_POLL_READ or PR_POLL_WRITE.
  84. * @param user value to pass to handler
  85. */
  86. void BPRFileDesc_AddEventHandler (BPRFileDesc *obj, PRInt16 event, BPRFileDesc_handler handler, void *user);
  87. /**
  88. * Unregisters a handler for an event.
  89. * The event must have a handler.
  90. *
  91. * @param obj the object
  92. * @param event NSPR event to unregister the handler for
  93. */
  94. void BPRFileDesc_RemoveEventHandler (BPRFileDesc *obj, PRInt16 event);
  95. /**
  96. * Enables monitoring of an event.
  97. * The event must have a handler.
  98. * The event must not be enabled.
  99. * If the operation associated with the event can already be performed,
  100. * the handler for the event may never be called.
  101. *
  102. * @param obj the object
  103. * @param event NSPR event to enable monitoring for
  104. */
  105. void BPRFileDesc_EnableEvent (BPRFileDesc *obj, PRInt16 event);
  106. /**
  107. * Disables monitoring of an event.
  108. * The event must have a handler.
  109. * The event must be enabled.
  110. *
  111. * @param obj the object
  112. * @param event NSPR event to disable monitoring for
  113. */
  114. void BPRFileDesc_DisableEvent (BPRFileDesc *obj, PRInt16 event);
  115. #endif