BReactor.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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
  118. #define BREACTOR_WRITE 2
  119. /**
  120. * Handler function invoked by the reactor when one or more monitored events
  121. * are detected.
  122. * The file descriptor object is in active state, being called from within
  123. * the associated reactor.
  124. *
  125. * @param user value passed to {@link BFileDescriptor_Init}
  126. * @param events bitmask of detected events (BREACTOR_READ, BREACTOR_WRITE).
  127. * Will be nonzero, and a subset of current monitored events.
  128. */
  129. typedef void (*BFileDescriptor_handler) (void *user, int events);
  130. /**
  131. * File descriptor object used with {@link BReactor}.
  132. */
  133. typedef struct BFileDescriptor_t {
  134. int fd;
  135. BFileDescriptor_handler handler;
  136. void *user;
  137. int active;
  138. int waitEvents;
  139. struct BFileDescriptor_t **epoll_returned_ptr;
  140. } BFileDescriptor;
  141. /**
  142. * Intializes the file descriptor object.
  143. * The object is initialized in not active state.
  144. *
  145. * @param bs file descriptor object to initialize
  146. * @param fb file descriptor to represent
  147. * @param handler handler function invoked by the reactor when a monitored event is detected
  148. * @param user value passed to the handler functuon
  149. */
  150. void BFileDescriptor_Init (BFileDescriptor *bs, int fd, BFileDescriptor_handler handler, void *user);
  151. #endif
  152. // BReactor
  153. #define BSYSTEM_MAX_RESULTS 64
  154. #define BSYSTEM_MAX_HANDLES 64
  155. /**
  156. * Event loop that supports file desciptor (Linux) or HANDLE (Windows) events
  157. * and timers.
  158. */
  159. typedef struct {
  160. DebugObject d_obj;
  161. int exiting;
  162. int exit_code;
  163. // jobs
  164. BPendingGroup pending_jobs;
  165. // timers
  166. BHeap timers_heap;
  167. LinkedList2 timers_expired_list;
  168. #ifdef BADVPN_USE_WINAPI
  169. int num_handles; // number of user handles
  170. int enabled_num; // number of user handles in the enabled array
  171. HANDLE enabled_handles[BSYSTEM_MAX_HANDLES]; // enabled user handles
  172. BHandle *enabled_objects[BSYSTEM_MAX_HANDLES]; // objects corresponding to enabled handles
  173. BHandle *returned_object;
  174. #else
  175. int efd; // epoll fd
  176. struct epoll_event epoll_results[BSYSTEM_MAX_RESULTS]; // epoll returned events buffer
  177. int epoll_results_num; // number of events in the array
  178. int epoll_results_pos; // number of events processed so far
  179. DebugCounter d_fds_counter;
  180. #endif
  181. } BReactor;
  182. /**
  183. * Initializes the reactor.
  184. * {@link BLog_Init} must have been done.
  185. * {@link BTime_Init} must have been done.
  186. *
  187. * @param bsys the object
  188. * @return 1 on success, 0 on failure
  189. */
  190. int BReactor_Init (BReactor *bsys) WARN_UNUSED;
  191. /**
  192. * Frees the reactor.
  193. * Must not be called from within the event loop ({@link BReactor_Exec}).
  194. * There must be no {@link BPending} objects using the pending group
  195. * returned by {@link BReactor_PendingGroup}.
  196. * There must be no running timers in this reactor.
  197. * There must be no file descriptors or handles registered
  198. * with this reactor.
  199. *
  200. * @param bsys the object
  201. */
  202. void BReactor_Free (BReactor *bsys);
  203. /**
  204. * Runs the event loop.
  205. *
  206. * @param bsys the object
  207. * @return value passed to {@link BReactor_Quit}
  208. */
  209. int BReactor_Exec (BReactor *bsys);
  210. /**
  211. * Causes the event loop ({@link BReactor_Exec}) to cease
  212. * dispatching events and return.
  213. * If the event loop is not running, this function does nothing.
  214. *
  215. * @param bsys the object
  216. * @param code value {@link BReactor_Exec} should return. If this is
  217. * called more than once from an event loop, the last code
  218. * will be returned.
  219. */
  220. void BReactor_Quit (BReactor *bsys, int code);
  221. /**
  222. * Starts a timer to expire after its default time.
  223. * The timer must have been initialized with {@link BTimer_Init}.
  224. * If the timer is in running state, it must be associated with this reactor.
  225. * The timer enters running state, associated with this reactor.
  226. * The timer's expiration time is set to the time argument.
  227. *
  228. * @param bsys the object
  229. * @param bt timer to start
  230. */
  231. void BReactor_SetTimer (BReactor *bsys, BTimer *bt);
  232. /**
  233. * Starts a timer to expire at the specified time.
  234. * The timer must have been initialized with {@link BTimer_Init}.
  235. * If the timer is in running state, it must be associated with this reactor.
  236. * The timer enters running state, associated with this reactor.
  237. * The timer's expiration time is set to the time argument.
  238. *
  239. * @param bsys the object
  240. * @param bt timer to start
  241. * @param time absolute expiration time (according to {@link btime_gettime})
  242. */
  243. void BReactor_SetTimerAbsolute (BReactor *bsys, BTimer *bt, btime_t time);
  244. /**
  245. * Stops a timer.
  246. * If the timer is in running state, it must be associated with this reactor.
  247. * The timer enters not running state.
  248. *
  249. * @param bsys the object
  250. * @param bt timer to stop
  251. */
  252. void BReactor_RemoveTimer (BReactor *bsys, BTimer *bt);
  253. /**
  254. * Returns a {@link BPendingGroup} object that can be used to schedule jobs for
  255. * the reactor to execute. These jobs have complete priority over other events
  256. * (timers, file descriptors and Windows handles).
  257. * The returned pending group may only be used as an argument to {@link BPending_Init},
  258. * and must not be accessed by other means.
  259. * All {@link BPending} objects using this group must be freed before freeing
  260. * the reactor.
  261. *
  262. * @param bsys the object
  263. * @return pending group for scheduling jobs for the reactor to execute
  264. */
  265. BPendingGroup * BReactor_PendingGroup (BReactor *bsys);
  266. #ifdef BADVPN_USE_WINAPI
  267. /**
  268. * Registers a Windows handle to be monitored.
  269. *
  270. * @param bsys the object
  271. * @param bh handle object. Must have been initialized with {@link BHandle_Init}.
  272. * Must be in not active state. On success, the handle object enters
  273. * active state, associated with this reactor, and is initialized
  274. * to not monitored state.
  275. * @return 1 on success, 0 on failure
  276. */
  277. int BReactor_AddHandle (BReactor *bsys, BHandle *bh) WARN_UNUSED;
  278. /**
  279. * Unregisters a Windows handle.
  280. *
  281. * @param bsys the object
  282. * @param bh handle object. Must be in active state, associated with this reactor.
  283. * The handle object enters not active state.
  284. */
  285. void BReactor_RemoveHandle (BReactor *bsys, BHandle *bh);
  286. /**
  287. * Starts monitoring a Windows handle.
  288. *
  289. * @param bsys the object
  290. * @param bh handle object. Must be in active state, associated with this reactor.
  291. * Must be in not monitored state.
  292. * The handle object enters monitored state.
  293. */
  294. void BReactor_EnableHandle (BReactor *bsys, BHandle *bh);
  295. /**
  296. * Stops monitoring a Windows handle.
  297. *
  298. * @param bsys the object
  299. * @param bh handle object. Must be in active state, associated with this reactor.
  300. * Must be in monitored state.
  301. * The handle object enters not monitored state.
  302. */
  303. void BReactor_DisableHandle (BReactor *bsys, BHandle *bh);
  304. #else
  305. /**
  306. * Starts monitoring a file descriptor.
  307. *
  308. * @param bsys the object
  309. * @param bs file descriptor object. Must have been initialized with
  310. * {@link BFileDescriptor_Init} Must be in not active state.
  311. * On success, the file descriptor object enters active state,
  312. * associated with this reactor.
  313. * @return 1 on success, 0 on failure
  314. */
  315. int BReactor_AddFileDescriptor (BReactor *bsys, BFileDescriptor *bs) WARN_UNUSED;
  316. /**
  317. * Stops monitoring a file descriptor.
  318. *
  319. * @param bsys the object
  320. * @param bs {@link BFileDescriptor} object. Must be in active state,
  321. * associated with this reactor. The file descriptor object
  322. * enters not active state.
  323. */
  324. void BReactor_RemoveFileDescriptor (BReactor *bsys, BFileDescriptor *bs);
  325. /**
  326. * Sets monitored file descriptor events.
  327. *
  328. * @param bsys the object
  329. * @param bs {@link BFileDescriptor} object. Must be in active state,
  330. * associated with this reactor.
  331. * @param events events to watch for. Must not have any bits other than
  332. * BREACTOR_READ and BREACTOR_WRITE.
  333. * This overrides previosly monitored events.
  334. */
  335. void BReactor_SetFileDescriptorEvents (BReactor *bsys, BFileDescriptor *bs, int events);
  336. #endif
  337. #endif