hloop.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. int priority; \
  49. struct hevent_s* pending_next; \
  50. HEVENT_FLAGS
  51. struct hevent_s {
  52. HEVENT_FIELDS
  53. };
  54. #define hevent_set_priority(ev, prio) ((hevent_t*)(ev))->priority = prio
  55. #define hevent_set_userdata(ev, udata) ((hevent_t*)(ev))->userdata = (void*)udata
  56. #define hevent_loop(ev) (((hevent_t*)(ev))->loop)
  57. #define hevent_type(ev) (((hevent_t*)(ev))->event_type)
  58. #define hevent_id(ev) (((hevent_t*)(ev))->event_id)
  59. #define hevent_priority(ev) (((hevent_t*)(ev))->priority)
  60. #define hevent_userdata(ev) (((hevent_t*)(ev))->userdata)
  61. typedef enum {
  62. HIO_TYPE_UNKNOWN = 0,
  63. HIO_TYPE_STDIN = 0x00000001,
  64. HIO_TYPE_STDOUT = 0x00000002,
  65. HIO_TYPE_STDERR = 0x00000004,
  66. HIO_TYPE_STDIO = 0x0000000F,
  67. HIO_TYPE_FILE = 0x00000010,
  68. HIO_TYPE_IP = 0x00000100,
  69. HIO_TYPE_UDP = 0x00001000,
  70. HIO_TYPE_TCP = 0x00010000,
  71. HIO_TYPE_SSL = 0x00020000,
  72. HIO_TYPE_SOCKET = 0x00FFFF00,
  73. } hio_type_e;
  74. BEGIN_EXTERN_C
  75. // loop
  76. #define HLOOP_FLAG_RUN_ONCE 0x00000001
  77. #define HLOOP_FLAG_AUTO_FREE 0x00000002
  78. #define HLOOP_FLAG_QUIT_WHEN_NO_ACTIVE_EVENTS 0x00000004
  79. HV_EXPORT hloop_t* hloop_new(int flags DEFAULT(HLOOP_FLAG_AUTO_FREE));
  80. // WARN: Forbid to call hloop_free if HLOOP_INIT_FLAG_AUTO_FREE set.
  81. HV_EXPORT void hloop_free(hloop_t** pp);
  82. // NOTE: when no active events, loop will quit if HLOOP_FLAG_QUIT_WHEN_NO_ACTIVE_EVENTS set.
  83. HV_EXPORT int hloop_run(hloop_t* loop);
  84. HV_EXPORT int hloop_stop(hloop_t* loop);
  85. HV_EXPORT int hloop_pause(hloop_t* loop);
  86. HV_EXPORT int hloop_resume(hloop_t* loop);
  87. HV_EXPORT void hloop_update_time(hloop_t* loop);
  88. HV_EXPORT uint64_t hloop_now(hloop_t* loop); // s
  89. HV_EXPORT uint64_t hloop_now_ms(hloop_t* loop); // ms
  90. HV_EXPORT uint64_t hloop_now_hrtime(hloop_t* loop); // us
  91. // userdata
  92. HV_EXPORT void hloop_set_userdata(hloop_t* loop, void* userdata);
  93. HV_EXPORT void* hloop_userdata(hloop_t* loop);
  94. // custom_event
  95. /*
  96. * hevent_t ev;
  97. * memset(&ev, 0, sizeof(hevent_t));
  98. * ev.event_type = (hevent_type_e)(HEVENT_TYPE_CUSTOM + 1);
  99. * ev.cb = custom_event_cb;
  100. * ev.userdata = userdata;
  101. * hloop_post_event(loop, &ev);
  102. */
  103. // NOTE: hloop_post_event is thread-safe
  104. HV_EXPORT void hloop_post_event(hloop_t* loop, hevent_t* ev);
  105. // idle
  106. HV_EXPORT hidle_t* hidle_add(hloop_t* loop, hidle_cb cb, uint32_t repeat DEFAULT(INFINITE));
  107. HV_EXPORT void hidle_del(hidle_t* idle);
  108. // timer
  109. // @param timeout: unit(ms)
  110. HV_EXPORT htimer_t* htimer_add(hloop_t* loop, htimer_cb cb, uint32_t timeout, uint32_t repeat DEFAULT(INFINITE));
  111. /*
  112. * minute hour day week month cb
  113. * 0~59 0~23 1~31 0~6 1~12
  114. * 30 -1 -1 -1 -1 cron.hourly
  115. * 30 1 -1 -1 -1 cron.daily
  116. * 30 1 15 -1 -1 cron.monthly
  117. * 30 1 -1 5 -1 cron.weekly
  118. * 30 1 1 -1 10 cron.yearly
  119. */
  120. HV_EXPORT htimer_t* htimer_add_period(hloop_t* loop, htimer_cb cb,
  121. int8_t minute DEFAULT(0), int8_t hour DEFAULT(-1), int8_t day DEFAULT(-1),
  122. int8_t week DEFAULT(-1), int8_t month DEFAULT(-1), uint32_t repeat DEFAULT(INFINITE));
  123. HV_EXPORT void htimer_del(htimer_t* timer);
  124. HV_EXPORT void htimer_reset(htimer_t* timer);
  125. // io
  126. //-----------------------low-level apis---------------------------------------
  127. #define HV_READ 0x0001
  128. #define HV_WRITE 0x0004
  129. #define HV_RDWR (HV_READ|HV_WRITE)
  130. /*
  131. const char* hio_engine() {
  132. #ifdef EVENT_SELECT
  133. return "select";
  134. #elif defined(EVENT_POLL)
  135. return "poll";
  136. #elif defined(EVENT_EPOLL)
  137. return "epoll";
  138. #elif defined(EVENT_KQUEUE)
  139. return "kqueue";
  140. #elif defined(EVENT_IOCP)
  141. return "iocp";
  142. #elif defined(EVENT_PORT)
  143. return "evport";
  144. #else
  145. return "noevent";
  146. #endif
  147. }
  148. */
  149. HV_EXPORT const char* hio_engine();
  150. HV_EXPORT hio_t* hio_get(hloop_t* loop, int fd);
  151. HV_EXPORT int hio_add(hio_t* io, hio_cb cb, int events DEFAULT(HV_READ));
  152. HV_EXPORT int hio_del(hio_t* io, int events DEFAULT(HV_RDWR));
  153. // hio_t fields
  154. HV_EXPORT int hio_fd (hio_t* io);
  155. HV_EXPORT int hio_error (hio_t* io);
  156. HV_EXPORT hio_type_e hio_type(hio_t* io);
  157. HV_EXPORT struct sockaddr* hio_localaddr(hio_t* io);
  158. HV_EXPORT struct sockaddr* hio_peeraddr (hio_t* io);
  159. // TODO: One loop per thread, one readbuf per loop.
  160. // But you can pass in your own readbuf instead of the default readbuf to avoid memcopy.
  161. HV_EXPORT void hio_set_readbuf(hio_t* io, void* buf, size_t len);
  162. // Enable SSL/TLS is so easy :)
  163. HV_EXPORT int hio_enable_ssl(hio_t* io);
  164. // set callbacks
  165. HV_EXPORT void hio_setcb_accept (hio_t* io, haccept_cb accept_cb);
  166. HV_EXPORT void hio_setcb_connect (hio_t* io, hconnect_cb connect_cb);
  167. HV_EXPORT void hio_setcb_read (hio_t* io, hread_cb read_cb);
  168. HV_EXPORT void hio_setcb_write (hio_t* io, hwrite_cb write_cb);
  169. HV_EXPORT void hio_setcb_close (hio_t* io, hclose_cb close_cb);
  170. // Nonblocking, poll IO events in the loop to call corresponding callback.
  171. HV_EXPORT int hio_accept (hio_t* io);
  172. HV_EXPORT int hio_connect(hio_t* io);
  173. HV_EXPORT int hio_read (hio_t* io);
  174. HV_EXPORT int hio_write (hio_t* io, const void* buf, size_t len);
  175. HV_EXPORT int hio_close (hio_t* io);
  176. //------------------high-level apis-------------------------------------------
  177. // hio_get -> hio_set_readbuf -> hio_setcb_read -> hio_read
  178. HV_EXPORT hio_t* hread (hloop_t* loop, int fd, void* buf, size_t len, hread_cb read_cb);
  179. // hio_get -> hio_setcb_write -> hio_write
  180. HV_EXPORT hio_t* hwrite (hloop_t* loop, int fd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL));
  181. // hio_get -> hio_close
  182. HV_EXPORT void hclose (hloop_t* loop, int fd);
  183. // tcp
  184. // hio_get -> hio_setcb_accept -> hio_accept
  185. HV_EXPORT hio_t* haccept (hloop_t* loop, int listenfd, haccept_cb accept_cb);
  186. // hio_get -> hio_setcb_connect -> hio_connect
  187. HV_EXPORT hio_t* hconnect (hloop_t* loop, int connfd, hconnect_cb connect_cb);
  188. // hio_get -> hio_set_readbuf -> hio_setcb_read -> hio_read
  189. HV_EXPORT hio_t* hrecv (hloop_t* loop, int connfd, void* buf, size_t len, hread_cb read_cb);
  190. // hio_get -> hio_setcb_write -> hio_write
  191. HV_EXPORT hio_t* hsend (hloop_t* loop, int connfd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL));
  192. // udp/ip
  193. // for HIO_TYPE_IP
  194. HV_EXPORT void hio_set_type(hio_t* io, hio_type_e type);
  195. HV_EXPORT void hio_set_localaddr(hio_t* io, struct sockaddr* addr, int addrlen);
  196. HV_EXPORT void hio_set_peeraddr (hio_t* io, struct sockaddr* addr, int addrlen);
  197. // NOTE: must call hio_set_peeraddr before hrecvfrom/hsendto
  198. // hio_get -> hio_set_readbuf -> hio_setcb_read -> hio_read
  199. HV_EXPORT hio_t* hrecvfrom (hloop_t* loop, int sockfd, void* buf, size_t len, hread_cb read_cb);
  200. // hio_get -> hio_setcb_write -> hio_write
  201. HV_EXPORT hio_t* hsendto (hloop_t* loop, int sockfd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL));
  202. //----------------- top-level apis---------------------------------------------
  203. // @tcp_server: socket -> bind -> listen -> haccept
  204. HV_EXPORT hio_t* create_tcp_server (hloop_t* loop, const char* host, int port, haccept_cb accept_cb);
  205. // @tcp_client: resolver -> socket -> hio_get -> hio_set_peeraddr -> hconnect
  206. HV_EXPORT hio_t* create_tcp_client (hloop_t* loop, const char* host, int port, hconnect_cb connect_cb);
  207. // @udp_server: socket -> bind -> hio_get
  208. HV_EXPORT hio_t* create_udp_server (hloop_t* loop, const char* host, int port);
  209. // @udp_client: resolver -> socket -> hio_get -> hio_set_peeraddr
  210. HV_EXPORT hio_t* create_udp_client (hloop_t* loop, const char* host, int port);
  211. END_EXTERN_C
  212. #endif // HV_LOOP_H_