watcher.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (C) 2013 Martin Willi
  3. * Copyright (C) 2013 revosec AG
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. /**
  16. * @defgroup watcher watcher
  17. * @{ @ingroup processor
  18. */
  19. #ifndef WATCHER_H_
  20. #define WATCHER_H_
  21. typedef struct watcher_t watcher_t;
  22. typedef enum watcher_event_t watcher_event_t;
  23. typedef enum watcher_state_t watcher_state_t;
  24. #include <library.h>
  25. /**
  26. * Callback function to register for file descriptor events.
  27. *
  28. * The callback is executed asynchronously using a thread from the pool.
  29. * Monitoring of fd is temporarily suspended to avoid additional events while
  30. * it is processed asynchronously. To allow concurrent events, one can quickly
  31. * process it (using a read/write) and return from the callback. This will
  32. * re-enable the event, while the data read can be processed in another
  33. * asynchronous job.
  34. *
  35. * On Linux, even if select() marks an FD as "ready", a subsequent read/write
  36. * can block. It is therefore highly recommended to use non-blocking I/O
  37. * and handle EAGAIN/EWOULDBLOCK gracefully.
  38. *
  39. * @param data user data passed during registration
  40. * @param fd file descriptor the event occurred on
  41. * @param event type of event
  42. * @return TRUE to keep watching event, FALSE to unregister fd for event
  43. */
  44. typedef bool (*watcher_cb_t)(void *data, int fd, watcher_event_t event);
  45. /**
  46. * What events to watch for a file descriptor.
  47. */
  48. enum watcher_event_t {
  49. WATCHER_READ = (1<<0),
  50. WATCHER_WRITE = (1<<1),
  51. WATCHER_EXCEPT = (1<<2),
  52. };
  53. /**
  54. * State the watcher currently is in
  55. */
  56. enum watcher_state_t {
  57. /** no watcher thread running or queued */
  58. WATCHER_STOPPED = 0,
  59. /** a job has been queued for watching, but not yet started */
  60. WATCHER_QUEUED,
  61. /** a watcher thread is active, dispatching socket events */
  62. WATCHER_RUNNING,
  63. };
  64. /**
  65. * Watch multiple file descriptors using select().
  66. */
  67. struct watcher_t {
  68. /**
  69. * Start watching a new file descriptor.
  70. *
  71. * Multiple callbacks can be registered for the same file descriptor, and
  72. * all of them get notified. Such callbacks are executed concurrently.
  73. *
  74. * @param fd file descriptor to start watching
  75. * @param events ORed set of events to watch
  76. * @param cb callback function to invoke on events
  77. * @param data data to pass to cb()
  78. */
  79. void (*add)(watcher_t *this, int fd, watcher_event_t events,
  80. watcher_cb_t cb, void *data);
  81. /**
  82. * Stop watching a previously registered file descriptor.
  83. *
  84. * This call blocks until any active callback for this FD returns. All
  85. * callbacks registered for that FD get unregistered.
  86. *
  87. * @param fd file descriptor to stop watching
  88. */
  89. void (*remove)(watcher_t *this, int fd);
  90. /**
  91. * Get the current watcher state
  92. *
  93. * @return currently active watcher state
  94. */
  95. watcher_state_t (*get_state)(watcher_t *this);
  96. /**
  97. * Destroy a watcher_t.
  98. */
  99. void (*destroy)(watcher_t *this);
  100. };
  101. /**
  102. * Create a watcher instance.
  103. *
  104. * @return watcher
  105. */
  106. watcher_t *watcher_create();
  107. #endif /** WATCHER_H_ @}*/