hloop.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #ifndef HV_LOOP_H_
  2. #define HV_LOOP_H_
  3. #include "hexport.h"
  4. #include "hplatform.h"
  5. #include "hdef.h"
  6. typedef struct hloop_s hloop_t;
  7. typedef struct hevent_s hevent_t;
  8. typedef struct hidle_s hidle_t;
  9. typedef struct htimer_s htimer_t;
  10. typedef struct htimeout_s htimeout_t;
  11. typedef struct hperiod_s hperiod_t;
  12. typedef struct hio_s hio_t;
  13. typedef void (*hevent_cb) (hevent_t* ev);
  14. typedef void (*hidle_cb) (hidle_t* idle);
  15. typedef void (*htimer_cb) (htimer_t* timer);
  16. typedef void (*hio_cb) (hio_t* io);
  17. typedef void (*haccept_cb) (hio_t* io);
  18. typedef void (*hconnect_cb) (hio_t* io);
  19. typedef void (*hread_cb) (hio_t* io, void* buf, int readbytes);
  20. typedef void (*hwrite_cb) (hio_t* io, const void* buf, int writebytes);
  21. typedef void (*hclose_cb) (hio_t* io);
  22. typedef enum {
  23. HEVENT_TYPE_NONE = 0,
  24. HEVENT_TYPE_IO = 0x00000001,
  25. HEVENT_TYPE_TIMEOUT = 0x00000010,
  26. HEVENT_TYPE_PERIOD = 0x00000020,
  27. HEVENT_TYPE_TIMER = HEVENT_TYPE_TIMEOUT|HEVENT_TYPE_PERIOD,
  28. HEVENT_TYPE_IDLE = 0x00000100,
  29. HEVENT_TYPE_CUSTOM = 0x00000400, // 1024
  30. } hevent_type_e;
  31. #define HEVENT_LOWEST_PRIORITY (-5)
  32. #define HEVENT_LOW_PRIORITY (-3)
  33. #define HEVENT_NORMAL_PRIORITY 0
  34. #define HEVENT_HIGH_PRIORITY 3
  35. #define HEVENT_HIGHEST_PRIORITY 5
  36. #define HEVENT_PRIORITY_SIZE (HEVENT_HIGHEST_PRIORITY-HEVENT_LOWEST_PRIORITY+1)
  37. #define HEVENT_PRIORITY_INDEX(priority) (priority-HEVENT_LOWEST_PRIORITY)
  38. #define HEVENT_FLAGS \
  39. unsigned destroy :1; \
  40. unsigned active :1; \
  41. unsigned pending :1;
  42. #define HEVENT_FIELDS \
  43. hloop_t* loop; \
  44. hevent_type_e event_type; \
  45. uint64_t event_id; \
  46. hevent_cb cb; \
  47. void* userdata; \
  48. void* privdata; \
  49. int priority; \
  50. struct hevent_s* pending_next; \
  51. HEVENT_FLAGS
  52. struct hevent_s {
  53. HEVENT_FIELDS
  54. };
  55. #define hevent_set_priority(ev, prio) ((hevent_t*)(ev))->priority = prio
  56. #define hevent_set_userdata(ev, udata) ((hevent_t*)(ev))->userdata = (void*)udata
  57. #define hevent_loop(ev) (((hevent_t*)(ev))->loop)
  58. #define hevent_type(ev) (((hevent_t*)(ev))->event_type)
  59. #define hevent_id(ev) (((hevent_t*)(ev))->event_id)
  60. #define hevent_priority(ev) (((hevent_t*)(ev))->priority)
  61. #define hevent_userdata(ev) (((hevent_t*)(ev))->userdata)
  62. typedef enum {
  63. HIO_TYPE_UNKNOWN = 0,
  64. HIO_TYPE_STDIN = 0x00000001,
  65. HIO_TYPE_STDOUT = 0x00000002,
  66. HIO_TYPE_STDERR = 0x00000004,
  67. HIO_TYPE_STDIO = 0x0000000F,
  68. HIO_TYPE_FILE = 0x00000010,
  69. HIO_TYPE_IP = 0x00000100,
  70. HIO_TYPE_UDP = 0x00001000,
  71. HIO_TYPE_TCP = 0x00010000,
  72. HIO_TYPE_SSL = 0x00020000,
  73. HIO_TYPE_SOCKET = 0x00FFFF00,
  74. } hio_type_e;
  75. #define HIO_DEFAULT_CONNECT_TIMEOUT 5000 // ms
  76. #define HIO_DEFAULT_CLOSE_TIMEOUT 60000 // ms
  77. #define HIO_DEFAULT_KEEPALIVE_TIMEOUT 75000 // ms
  78. #define HIO_DEFAULT_HEARTBEAT_INTERVAL 3000 // ms
  79. BEGIN_EXTERN_C
  80. // loop
  81. #define HLOOP_FLAG_RUN_ONCE 0x00000001
  82. #define HLOOP_FLAG_AUTO_FREE 0x00000002
  83. #define HLOOP_FLAG_QUIT_WHEN_NO_ACTIVE_EVENTS 0x00000004
  84. HV_EXPORT hloop_t* hloop_new(int flags DEFAULT(HLOOP_FLAG_AUTO_FREE));
  85. // WARN: Forbid to call hloop_free if HLOOP_INIT_FLAG_AUTO_FREE set.
  86. HV_EXPORT void hloop_free(hloop_t** pp);
  87. // NOTE: when no active events, loop will quit if HLOOP_FLAG_QUIT_WHEN_NO_ACTIVE_EVENTS set.
  88. HV_EXPORT int hloop_run(hloop_t* loop);
  89. HV_EXPORT int hloop_stop(hloop_t* loop);
  90. HV_EXPORT int hloop_pause(hloop_t* loop);
  91. HV_EXPORT int hloop_resume(hloop_t* loop);
  92. HV_EXPORT void hloop_update_time(hloop_t* loop);
  93. HV_EXPORT uint64_t hloop_now(hloop_t* loop); // s
  94. HV_EXPORT uint64_t hloop_now_ms(hloop_t* loop); // ms
  95. HV_EXPORT uint64_t hloop_now_hrtime(hloop_t* loop); // us
  96. #define hloop_now_us hloop_now_hrtime
  97. // userdata
  98. HV_EXPORT void hloop_set_userdata(hloop_t* loop, void* userdata);
  99. HV_EXPORT void* hloop_userdata(hloop_t* loop);
  100. // custom_event
  101. /*
  102. * hevent_t ev;
  103. * memset(&ev, 0, sizeof(hevent_t));
  104. * ev.event_type = (hevent_type_e)(HEVENT_TYPE_CUSTOM + 1);
  105. * ev.cb = custom_event_cb;
  106. * ev.userdata = userdata;
  107. * hloop_post_event(loop, &ev);
  108. */
  109. // NOTE: hloop_post_event is thread-safe
  110. HV_EXPORT void hloop_post_event(hloop_t* loop, hevent_t* ev);
  111. // idle
  112. HV_EXPORT hidle_t* hidle_add(hloop_t* loop, hidle_cb cb, uint32_t repeat DEFAULT(INFINITE));
  113. HV_EXPORT void hidle_del(hidle_t* idle);
  114. // timer
  115. // @param timeout: unit(ms)
  116. HV_EXPORT htimer_t* htimer_add(hloop_t* loop, htimer_cb cb, uint32_t timeout, uint32_t repeat DEFAULT(INFINITE));
  117. /*
  118. * minute hour day week month cb
  119. * 0~59 0~23 1~31 0~6 1~12
  120. * 30 -1 -1 -1 -1 cron.hourly
  121. * 30 1 -1 -1 -1 cron.daily
  122. * 30 1 15 -1 -1 cron.monthly
  123. * 30 1 -1 5 -1 cron.weekly
  124. * 30 1 1 -1 10 cron.yearly
  125. */
  126. HV_EXPORT htimer_t* htimer_add_period(hloop_t* loop, htimer_cb cb,
  127. int8_t minute DEFAULT(0), int8_t hour DEFAULT(-1), int8_t day DEFAULT(-1),
  128. int8_t week DEFAULT(-1), int8_t month DEFAULT(-1), uint32_t repeat DEFAULT(INFINITE));
  129. HV_EXPORT void htimer_del(htimer_t* timer);
  130. HV_EXPORT void htimer_reset(htimer_t* timer);
  131. // io
  132. //-----------------------low-level apis---------------------------------------
  133. #define HV_READ 0x0001
  134. #define HV_WRITE 0x0004
  135. #define HV_RDWR (HV_READ|HV_WRITE)
  136. /*
  137. const char* hio_engine() {
  138. #ifdef EVENT_SELECT
  139. return "select";
  140. #elif defined(EVENT_POLL)
  141. return "poll";
  142. #elif defined(EVENT_EPOLL)
  143. return "epoll";
  144. #elif defined(EVENT_KQUEUE)
  145. return "kqueue";
  146. #elif defined(EVENT_IOCP)
  147. return "iocp";
  148. #elif defined(EVENT_PORT)
  149. return "evport";
  150. #else
  151. return "noevent";
  152. #endif
  153. }
  154. */
  155. HV_EXPORT const char* hio_engine();
  156. HV_EXPORT hio_t* hio_get(hloop_t* loop, int fd);
  157. HV_EXPORT int hio_add(hio_t* io, hio_cb cb, int events DEFAULT(HV_READ));
  158. HV_EXPORT int hio_del(hio_t* io, int events DEFAULT(HV_RDWR));
  159. // hio_t fields
  160. HV_EXPORT int hio_fd (hio_t* io);
  161. HV_EXPORT int hio_error (hio_t* io);
  162. HV_EXPORT hio_type_e hio_type(hio_t* io);
  163. HV_EXPORT struct sockaddr* hio_localaddr(hio_t* io);
  164. HV_EXPORT struct sockaddr* hio_peeraddr (hio_t* io);
  165. // set callbacks
  166. HV_EXPORT void hio_setcb_accept (hio_t* io, haccept_cb accept_cb);
  167. HV_EXPORT void hio_setcb_connect (hio_t* io, hconnect_cb connect_cb);
  168. HV_EXPORT void hio_setcb_read (hio_t* io, hread_cb read_cb);
  169. HV_EXPORT void hio_setcb_write (hio_t* io, hwrite_cb write_cb);
  170. HV_EXPORT void hio_setcb_close (hio_t* io, hclose_cb close_cb);
  171. // some useful settings
  172. // Enable SSL/TLS is so easy :)
  173. HV_EXPORT int hio_enable_ssl(hio_t* io);
  174. // TODO: One loop per thread, one readbuf per loop.
  175. // But you can pass in your own readbuf instead of the default readbuf to avoid memcopy.
  176. HV_EXPORT void hio_set_readbuf(hio_t* io, void* buf, size_t len);
  177. // connect timeout => hclose_cb
  178. HV_EXPORT void hio_set_connect_timeout(hio_t* io, int timeout_ms DEFAULT(HIO_DEFAULT_CONNECT_TIMEOUT));
  179. // close timeout => hclose_cb
  180. HV_EXPORT void hio_set_close_timeout(hio_t* io, int timeout_ms DEFAULT(HIO_DEFAULT_CLOSE_TIMEOUT));
  181. // keepalive timeout => hclose_cb
  182. HV_EXPORT void hio_set_keepalive_timeout(hio_t* io, int timeout_ms DEFAULT(HIO_DEFAULT_KEEPALIVE_TIMEOUT));
  183. /*
  184. void send_heartbeat(hio_t* io) {
  185. static char buf[] = "PING\r\n";
  186. hio_write(io, buf, 6);
  187. }
  188. hio_set_heartbeat(io, 3000, send_heartbeat);
  189. */
  190. typedef void (*hio_send_heartbeat_fn)(hio_t* io);
  191. // heartbeat interval => hio_send_heartbeat_fn
  192. HV_EXPORT void hio_set_heartbeat(hio_t* io, int interval_ms, hio_send_heartbeat_fn fn);
  193. // Nonblocking, poll IO events in the loop to call corresponding callback.
  194. // hio_add(io, HV_READ) => accept => haccept_cb
  195. HV_EXPORT int hio_accept (hio_t* io);
  196. // connect => hio_add(io, HV_WRITE) => hconnect_cb
  197. HV_EXPORT int hio_connect(hio_t* io);
  198. // hio_add(io, HV_READ) => read => hread_cb
  199. HV_EXPORT int hio_read (hio_t* io);
  200. // hio_try_write => hio_add(io, HV_WRITE) => write => hwrite_cb
  201. HV_EXPORT int hio_write (hio_t* io, const void* buf, size_t len);
  202. // hio_del(io, HV_RDWR) => close => hclose_cb
  203. HV_EXPORT int hio_close (hio_t* io);
  204. //------------------high-level apis-------------------------------------------
  205. // hio_get -> hio_set_readbuf -> hio_setcb_read -> hio_read
  206. HV_EXPORT hio_t* hread (hloop_t* loop, int fd, void* buf, size_t len, hread_cb read_cb);
  207. // hio_get -> hio_setcb_write -> hio_write
  208. HV_EXPORT hio_t* hwrite (hloop_t* loop, int fd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL));
  209. // hio_get -> hio_close
  210. HV_EXPORT void hclose (hloop_t* loop, int fd);
  211. // tcp
  212. // hio_get -> hio_setcb_accept -> hio_accept
  213. HV_EXPORT hio_t* haccept (hloop_t* loop, int listenfd, haccept_cb accept_cb);
  214. // hio_get -> hio_setcb_connect -> hio_connect
  215. HV_EXPORT hio_t* hconnect (hloop_t* loop, int connfd, hconnect_cb connect_cb);
  216. // hio_get -> hio_set_readbuf -> hio_setcb_read -> hio_read
  217. HV_EXPORT hio_t* hrecv (hloop_t* loop, int connfd, void* buf, size_t len, hread_cb read_cb);
  218. // hio_get -> hio_setcb_write -> hio_write
  219. HV_EXPORT hio_t* hsend (hloop_t* loop, int connfd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL));
  220. // udp
  221. HV_EXPORT void hio_set_type(hio_t* io, hio_type_e type);
  222. HV_EXPORT void hio_set_localaddr(hio_t* io, struct sockaddr* addr, int addrlen);
  223. HV_EXPORT void hio_set_peeraddr (hio_t* io, struct sockaddr* addr, int addrlen);
  224. // NOTE: must call hio_set_peeraddr before hrecvfrom/hsendto
  225. // hio_get -> hio_set_readbuf -> hio_setcb_read -> hio_read
  226. HV_EXPORT hio_t* hrecvfrom (hloop_t* loop, int sockfd, void* buf, size_t len, hread_cb read_cb);
  227. // hio_get -> hio_setcb_write -> hio_write
  228. HV_EXPORT hio_t* hsendto (hloop_t* loop, int sockfd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL));
  229. //----------------- top-level apis---------------------------------------------
  230. // @tcp_server: socket -> bind -> listen -> haccept
  231. // @see examples/tcp.c
  232. HV_EXPORT hio_t* hloop_create_tcp_server (hloop_t* loop, const char* host, int port, haccept_cb accept_cb);
  233. // @tcp_client: resolver -> socket -> hio_get -> hio_set_peeraddr -> hconnect
  234. // @see examples/nc.c
  235. HV_EXPORT hio_t* hloop_create_tcp_client (hloop_t* loop, const char* host, int port, hconnect_cb connect_cb);
  236. // @udp_server: socket -> bind -> hio_get
  237. // @see examples/udp.c
  238. HV_EXPORT hio_t* hloop_create_udp_server (hloop_t* loop, const char* host, int port);
  239. // @udp_client: resolver -> socket -> hio_get -> hio_set_peeraddr
  240. // @see examples/nc.c
  241. HV_EXPORT hio_t* hloop_create_udp_client (hloop_t* loop, const char* host, int port);
  242. END_EXTERN_C
  243. #endif // HV_LOOP_H_