BReactor.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /**
  2. * @file BReactor.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. * Event loop that supports file desciptor (Linux) or HANDLE (Windows) events
  25. * and timers.
  26. */
  27. #ifndef BADVPN_SYSTEM_BREACTOR_H
  28. #define BADVPN_SYSTEM_BREACTOR_H
  29. #ifdef BADVPN_USE_WINAPI
  30. #include <windows.h>
  31. #else
  32. #include <sys/epoll.h>
  33. #endif
  34. #include <stdint.h>
  35. #include <misc/debug.h>
  36. #include <misc/debugcounter.h>
  37. #include <system/DebugObject.h>
  38. #include <structure/LinkedList2.h>
  39. #include <structure/BHeap.h>
  40. #include <system/BTime.h>
  41. #include <system/BPending.h>
  42. struct BTimer_t;
  43. /**
  44. * Handler function invoked when the timer expires.
  45. * The timer was in running state.
  46. * The timer enters not running state before this function is invoked.
  47. * This function is being called from within the timer's previosly
  48. * associated reactor.
  49. *
  50. * @param user value passed to {@link BTimer_Init}
  51. */
  52. typedef void (*BTimer_handler) (void *user);
  53. /**
  54. * Timer object used with {@link BReactor}.
  55. */
  56. typedef struct BTimer_t {
  57. btime_t msTime;
  58. BTimer_handler handler;
  59. void *handler_pointer;
  60. uint8_t active;
  61. uint8_t expired;
  62. btime_t absTime;
  63. union {
  64. BHeapNode heap_node;
  65. LinkedList2Node list_node;
  66. };
  67. } BTimer;
  68. /**
  69. * Initializes the timer object.
  70. * The timer object is initialized in not running state.
  71. *
  72. * @param bt the object
  73. * @param msTime default timeout in milliseconds
  74. * @param handler handler function invoked when the timer expires
  75. * @param user value to pass to the handler function
  76. */
  77. void BTimer_Init (BTimer *bt, btime_t msTime, BTimer_handler handler, void *user);
  78. /**
  79. * Checks if the timer is running.
  80. *
  81. * @param bt the object
  82. * @return 1 if running, 0 if not running
  83. */
  84. int BTimer_IsRunning (BTimer *bt);
  85. #ifdef BADVPN_USE_WINAPI
  86. /**
  87. * Handler function invoked by the reactor when the handle is signalled
  88. * The handle object is in active state, is being called from within
  89. * the associated reactor, and is in monitored state.
  90. *
  91. * @param user value passed to {@link BHandle_Init}
  92. */
  93. typedef void (*BHandle_handler) (void *user);
  94. struct BHandle_t;
  95. /**
  96. * Windows handle object used with {@link BReactor}.
  97. */
  98. typedef struct BHandle_t {
  99. HANDLE h;
  100. BHandle_handler handler;
  101. void *user;
  102. int active;
  103. int position;
  104. } BHandle;
  105. /**
  106. * Initializes the handle object.
  107. * The handle object is initialized in not active state.
  108. *
  109. * @param bh the object
  110. * @param handle underlying Windows handle
  111. * @param handler handler function invoked when the handle is signalled
  112. * @param user value to pass to the handler function
  113. */
  114. void BHandle_Init (BHandle *bh, HANDLE handle, BHandle_handler handler, void *user);
  115. #else
  116. struct BFileDescriptor_t;
  117. #define BREACTOR_READ (1 << 0)
  118. #define BREACTOR_WRITE (1 << 1)
  119. #define BREACTOR_ERROR (1 << 2)
  120. /**
  121. * Handler function invoked by the reactor when one or more events are detected.
  122. * The events argument will contain a subset of the monitored events (BREACTOR_READ, BREACTOR_WRITE),
  123. * plus possibly the error event (BREACTOR_ERROR).
  124. * The file descriptor object is in active state, being called from within
  125. * the associated reactor.
  126. *
  127. * @param user value passed to {@link BFileDescriptor_Init}
  128. * @param events bitmask composed of a subset of monitored events (BREACTOR_READ, BREACTOR_WRITE),
  129. * and possibly the error event (BREACTOR_ERROR).
  130. * Will be nonzero.
  131. */
  132. typedef void (*BFileDescriptor_handler) (void *user, int events);
  133. /**
  134. * File descriptor object used with {@link BReactor}.
  135. */
  136. typedef struct BFileDescriptor_t {
  137. int fd;
  138. BFileDescriptor_handler handler;
  139. void *user;
  140. int active;
  141. int waitEvents;
  142. struct BFileDescriptor_t **epoll_returned_ptr;
  143. } BFileDescriptor;
  144. /**
  145. * Intializes the file descriptor object.
  146. * The object is initialized in not active state.
  147. *
  148. * @param bs file descriptor object to initialize
  149. * @param fb file descriptor to represent
  150. * @param handler handler function invoked by the reactor when a monitored event is detected
  151. * @param user value passed to the handler functuon
  152. */
  153. void BFileDescriptor_Init (BFileDescriptor *bs, int fd, BFileDescriptor_handler handler, void *user);
  154. #endif
  155. // BReactor
  156. #define BSYSTEM_MAX_RESULTS 64
  157. #define BSYSTEM_MAX_HANDLES 64
  158. /**
  159. * Event loop that supports file desciptor (Linux) or HANDLE (Windows) events
  160. * and timers.
  161. */
  162. typedef struct {
  163. DebugObject d_obj;
  164. int exiting;
  165. int exit_code;
  166. // jobs
  167. BPendingGroup pending_jobs;
  168. // timers
  169. BHeap timers_heap;
  170. LinkedList2 timers_expired_list;
  171. #ifdef BADVPN_USE_WINAPI
  172. int num_handles; // number of user handles
  173. int enabled_num; // number of user handles in the enabled array
  174. HANDLE enabled_handles[BSYSTEM_MAX_HANDLES]; // enabled user handles
  175. BHandle *enabled_objects[BSYSTEM_MAX_HANDLES]; // objects corresponding to enabled handles
  176. BHandle *returned_object;
  177. #else
  178. int efd; // epoll fd
  179. struct epoll_event epoll_results[BSYSTEM_MAX_RESULTS]; // epoll returned events buffer
  180. int epoll_results_num; // number of events in the array
  181. int epoll_results_pos; // number of events processed so far
  182. DebugCounter d_fds_counter;
  183. #endif
  184. } BReactor;
  185. /**
  186. * Initializes the reactor.
  187. * {@link BLog_Init} must have been done.
  188. * {@link BTime_Init} must have been done.
  189. *
  190. * @param bsys the object
  191. * @return 1 on success, 0 on failure
  192. */
  193. int BReactor_Init (BReactor *bsys) WARN_UNUSED;
  194. /**
  195. * Frees the reactor.
  196. * Must not be called from within the event loop ({@link BReactor_Exec}).
  197. * There must be no {@link BPending} objects using the pending group
  198. * returned by {@link BReactor_PendingGroup}.
  199. * There must be no running timers in this reactor.
  200. * There must be no file descriptors or handles registered
  201. * with this reactor.
  202. *
  203. * @param bsys the object
  204. */
  205. void BReactor_Free (BReactor *bsys);
  206. /**
  207. * Runs the event loop.
  208. *
  209. * @param bsys the object
  210. * @return value passed to {@link BReactor_Quit}
  211. */
  212. int BReactor_Exec (BReactor *bsys);
  213. /**
  214. * Causes the event loop ({@link BReactor_Exec}) to cease
  215. * dispatching events and return.
  216. * Any further calls of {@link BReactor_Exec} will return immediately.
  217. *
  218. * @param bsys the object
  219. * @param code value {@link BReactor_Exec} should return. If this is
  220. * called more than once, it will return the last code.
  221. */
  222. void BReactor_Quit (BReactor *bsys, int code);
  223. /**
  224. * Starts a timer to expire after its default time.
  225. * The timer must have been initialized with {@link BTimer_Init}.
  226. * If the timer is in running state, it must be associated with this reactor.
  227. * The timer enters running state, associated with this reactor.
  228. * The timer's expiration time is set to the time argument.
  229. *
  230. * @param bsys the object
  231. * @param bt timer to start
  232. */
  233. void BReactor_SetTimer (BReactor *bsys, BTimer *bt);
  234. /**
  235. * Starts a timer to expire at the specified time.
  236. * The timer must have been initialized with {@link BTimer_Init}.
  237. * If the timer is in running state, it must be associated with this reactor.
  238. * The timer enters running state, associated with this reactor.
  239. * The timer's expiration time is set to the time argument.
  240. *
  241. * @param bsys the object
  242. * @param bt timer to start
  243. * @param time absolute expiration time (according to {@link btime_gettime})
  244. */
  245. void BReactor_SetTimerAbsolute (BReactor *bsys, BTimer *bt, btime_t time);
  246. /**
  247. * Stops a timer.
  248. * If the timer is in running state, it must be associated with this reactor.
  249. * The timer enters not running state.
  250. *
  251. * @param bsys the object
  252. * @param bt timer to stop
  253. */
  254. void BReactor_RemoveTimer (BReactor *bsys, BTimer *bt);
  255. /**
  256. * Returns a {@link BPendingGroup} object that can be used to schedule jobs for
  257. * the reactor to execute. These jobs have complete priority over other events
  258. * (timers, file descriptors and Windows handles).
  259. * The returned pending group may only be used as an argument to {@link BPending_Init},
  260. * and must not be accessed by other means.
  261. * All {@link BPending} objects using this group must be freed before freeing
  262. * the reactor.
  263. *
  264. * @param bsys the object
  265. * @return pending group for scheduling jobs for the reactor to execute
  266. */
  267. BPendingGroup * BReactor_PendingGroup (BReactor *bsys);
  268. #ifdef BADVPN_USE_WINAPI
  269. /**
  270. * Registers a Windows handle to be monitored.
  271. *
  272. * @param bsys the object
  273. * @param bh handle object. Must have been initialized with {@link BHandle_Init}.
  274. * Must be in not active state. On success, the handle object enters
  275. * active state, associated with this reactor, and is initialized
  276. * to not monitored state.
  277. * @return 1 on success, 0 on failure
  278. */
  279. int BReactor_AddHandle (BReactor *bsys, BHandle *bh) WARN_UNUSED;
  280. /**
  281. * Unregisters a Windows handle.
  282. *
  283. * @param bsys the object
  284. * @param bh handle object. Must be in active state, associated with this reactor.
  285. * The handle object enters not active state.
  286. */
  287. void BReactor_RemoveHandle (BReactor *bsys, BHandle *bh);
  288. /**
  289. * Starts monitoring a Windows handle.
  290. *
  291. * @param bsys the object
  292. * @param bh handle object. Must be in active state, associated with this reactor.
  293. * Must be in not monitored state.
  294. * The handle object enters monitored state.
  295. */
  296. void BReactor_EnableHandle (BReactor *bsys, BHandle *bh);
  297. /**
  298. * Stops monitoring a Windows handle.
  299. *
  300. * @param bsys the object
  301. * @param bh handle object. Must be in active state, associated with this reactor.
  302. * Must be in monitored state.
  303. * The handle object enters not monitored state.
  304. */
  305. void BReactor_DisableHandle (BReactor *bsys, BHandle *bh);
  306. #else
  307. /**
  308. * Starts monitoring a file descriptor.
  309. *
  310. * @param bsys the object
  311. * @param bs file descriptor object. Must have been initialized with
  312. * {@link BFileDescriptor_Init} Must be in not active state.
  313. * On success, the file descriptor object enters active state,
  314. * associated with this reactor.
  315. * @return 1 on success, 0 on failure
  316. */
  317. int BReactor_AddFileDescriptor (BReactor *bsys, BFileDescriptor *bs) WARN_UNUSED;
  318. /**
  319. * Stops monitoring a file descriptor.
  320. *
  321. * @param bsys the object
  322. * @param bs {@link BFileDescriptor} object. Must be in active state,
  323. * associated with this reactor. The file descriptor object
  324. * enters not active state.
  325. */
  326. void BReactor_RemoveFileDescriptor (BReactor *bsys, BFileDescriptor *bs);
  327. /**
  328. * Sets monitored file descriptor events.
  329. *
  330. * @param bsys the object
  331. * @param bs {@link BFileDescriptor} object. Must be in active state,
  332. * associated with this reactor.
  333. * @param events events to watch for. Must not have any bits other than
  334. * BREACTOR_READ and BREACTOR_WRITE.
  335. * This overrides previosly monitored events.
  336. */
  337. void BReactor_SetFileDescriptorEvents (BReactor *bsys, BFileDescriptor *bs, int events);
  338. #endif
  339. #endif