fd_event_manager.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**************************************************************************
  2. * UNIT: File Descriptor (FD) Event Manager
  3. *
  4. * OVERVIEW: This unit contains functions to register a FD with the FD
  5. * event manager for callbacks when activity is received on that
  6. * FD. Notification of read, write, and exception activity can
  7. * all be registered for individually. Once a registered FD is
  8. * closed by the user, the FD must be unregistered. To use
  9. * the FD Event manager you need to make calls to
  10. * netsnmp_external_event_info() and
  11. * netsnmp_dispatch_external_events() in your event loop to receive
  12. * callbacks for registered events. See snmpd.c and snmptrapd.c
  13. * for examples.
  14. *
  15. * LIMITATIONS:
  16. **************************************************************************/
  17. #ifndef FD_EVENT_MANAGER_H
  18. #define FD_EVENT_MANAGER_H
  19. #ifdef HAVE_SYS_SELECT_H
  20. #include <sys/select.h>
  21. #endif
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. #define NUM_EXTERNAL_FDS 32
  26. #define FD_REGISTERED_OK 0
  27. #define FD_REGISTRATION_FAILED -2
  28. #define FD_UNREGISTERED_OK 0
  29. #define FD_NO_SUCH_REGISTRATION -1
  30. /* Since the inception of netsnmp_external_event_info and
  31. * netsnmp_dispatch_external_events, there is no longer a need for the data
  32. * below to be globally visible. We will leave it global for now for
  33. * compatibility purposes. */
  34. extern int external_readfd[NUM_EXTERNAL_FDS], external_readfdlen;
  35. extern int external_writefd[NUM_EXTERNAL_FDS], external_writefdlen;
  36. extern int external_exceptfd[NUM_EXTERNAL_FDS], external_exceptfdlen;
  37. extern void (*external_readfdfunc[NUM_EXTERNAL_FDS]) (int, void *);
  38. extern void (*external_writefdfunc[NUM_EXTERNAL_FDS]) (int, void *);
  39. extern void (*external_exceptfdfunc[NUM_EXTERNAL_FDS]) (int, void *);
  40. extern void *external_readfd_data[NUM_EXTERNAL_FDS];
  41. extern void *external_writefd_data[NUM_EXTERNAL_FDS];
  42. extern void *external_exceptfd_data[NUM_EXTERNAL_FDS];
  43. /* Here are the key functions of this unit. Use register_xfd to register
  44. * a callback to be called when there is x activity on the register fd.
  45. * x can be read, write, or except (for exception). When registering,
  46. * you can pass in a pointer to some data that you have allocated that
  47. * you would like to have back when the callback is called. */
  48. int register_readfd(int, void (*func)(int, void *), void *);
  49. int register_writefd(int, void (*func)(int, void *), void *);
  50. int register_exceptfd(int, void (*func)(int, void *), void *);
  51. /* Unregisters a given fd for events */
  52. int unregister_readfd(int);
  53. int unregister_writefd(int);
  54. int unregister_exceptfd(int);
  55. /*
  56. * External Event Info
  57. *
  58. * Description:
  59. * Call this function to add an external event fds to your read, write,
  60. * exception fds that your application already has. When this function
  61. * returns, your fd_sets will be ready for select(). It returns the
  62. * biggest fd in the fd_sets so far.
  63. *
  64. * Input Parameters: None
  65. *
  66. * Output Parameters: None
  67. *
  68. * In/Out Parameters:
  69. * numfds - The biggest fd so far. On exit to this function, numfds
  70. * could of changed since we pass out the new biggest fd.
  71. * readfds - Set of read FDs that we are monitoring. This function
  72. * can modify this set to have more FDs that we are monitoring.
  73. * writefds - Set of write FDs that we are monitoring. This function
  74. * can modify this set to have more FDs that we are monitoring.
  75. * exceptfds - Set of exception FDs that we are monitoring. This function
  76. * can modify this set to have more FDs that we are monitoring.
  77. *
  78. * Return Value: None
  79. *
  80. * Side Effects: None
  81. */
  82. NETSNMP_IMPORT
  83. void netsnmp_external_event_info(int *numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds);
  84. NETSNMP_IMPORT
  85. void netsnmp_external_event_info2(int *numfds,
  86. netsnmp_large_fd_set *readfds,
  87. netsnmp_large_fd_set *writefds,
  88. netsnmp_large_fd_set *exceptfds);
  89. /*
  90. * Dispatch External Events
  91. *
  92. * Description:
  93. * Call this function after select returns with pending events. If any of
  94. * them were NETSNMP external events, the registered callback will be called.
  95. * The corresponding fd_set will have the FD cleared after the event is
  96. * dispatched.
  97. *
  98. * Input Parameters: None
  99. *
  100. * Output Parameters: None
  101. *
  102. * In/Out Parameters:
  103. * count - Number of FDs that have activity. In this function, we decrement
  104. * count as we dispatch an event.
  105. * readfds - Set of read FDs that have activity
  106. * writefds - Set of write FDs that have activity
  107. * exceptfds - Set of exception FDs that have activity
  108. *
  109. * Return Value: None
  110. *
  111. * Side Effects: None
  112. */
  113. NETSNMP_IMPORT
  114. void netsnmp_dispatch_external_events(int *count, fd_set *readfds, fd_set *writefds, fd_set *exceptfds);
  115. NETSNMP_IMPORT
  116. void netsnmp_dispatch_external_events2(int *count,
  117. netsnmp_large_fd_set *readfds,
  118. netsnmp_large_fd_set *writefds,
  119. netsnmp_large_fd_set *exceptfds);
  120. #ifdef __cplusplus
  121. }
  122. #endif
  123. #endif