1
0

hloop.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #ifndef HW_LOOP_H_
  2. #define HW_LOOP_H_
  3. #include "hdef.h"
  4. BEGIN_EXTERN_C
  5. typedef struct hloop_s hloop_t; typedef struct hevent_s hevent_t;
  6. typedef struct hidle_s hidle_t;
  7. typedef struct htimer_s htimer_t;
  8. typedef struct htimeout_s htimeout_t;
  9. typedef struct hperiod_s hperiod_t;
  10. typedef struct hio_s hio_t;
  11. typedef void (*hevent_cb) (hevent_t* ev);
  12. typedef void (*hidle_cb) (hidle_t* idle);
  13. typedef void (*htimer_cb) (htimer_t* timer);
  14. typedef void (*hio_cb) (hio_t* io);
  15. typedef void (*haccept_cb) (hio_t* io);
  16. typedef void (*hconnect_cb) (hio_t* io);
  17. typedef void (*hread_cb) (hio_t* io, void* buf, int readbytes);
  18. typedef void (*hwrite_cb) (hio_t* io, const void* buf, int writebytes);
  19. typedef void (*hclose_cb) (hio_t* io);
  20. typedef enum {
  21. HEVENT_TYPE_NONE = 0,
  22. HEVENT_TYPE_IDLE = 0x00000010,
  23. HEVENT_TYPE_TIMEOUT = 0x00000100,
  24. HEVENT_TYPE_PERIOD = 0x00000200,
  25. HEVENT_TYPE_TIMER = HEVENT_TYPE_TIMEOUT|HEVENT_TYPE_PERIOD,
  26. HEVENT_TYPE_IO = 0x00001000,
  27. } hevent_type_e;
  28. #define HEVENT_LOWEST_PRIORITY (-5)
  29. #define HEVENT_LOW_PRIORITY (-3)
  30. #define HEVENT_NORMAL_PRIORITY 0
  31. #define HEVENT_HIGH_PRIORITY 3
  32. #define HEVENT_HIGHEST_PRIORITY 5
  33. #define HEVENT_PRIORITY_SIZE (HEVENT_HIGHEST_PRIORITY-HEVENT_LOWEST_PRIORITY+1)
  34. #define HEVENT_PRIORITY_INDEX(priority) (priority-HEVENT_LOWEST_PRIORITY)
  35. #define HEVENT_FLAGS \
  36. unsigned destroy :1; \
  37. unsigned active :1; \
  38. unsigned pending :1;
  39. #define HEVENT_FIELDS \
  40. hloop_t* loop; \
  41. hevent_type_e event_type; \
  42. uint64_t event_id; \
  43. hevent_cb cb; \
  44. void* userdata; \
  45. int priority; \
  46. struct hevent_s* pending_next; \
  47. HEVENT_FLAGS
  48. struct hevent_s {
  49. HEVENT_FIELDS
  50. };
  51. #define hevent_set_priority(ev, prio) ((hevent_t*)(ev))->priority = prio
  52. #define hevent_set_userdata(ev, udata) ((hevent_t*)(ev))->userdata = (void*)udata
  53. #define hevent_loop(ev) (((hevent_t*)(ev))->loop)
  54. #define hevent_type(ev) (((hevent_t*)(ev))->event_type)
  55. #define hevent_id(ev) (((hevent_t*)(ev))->event_id)
  56. #define hevent_priority(ev) (((hevent_t*)(ev))->priority)
  57. #define hevent_userdata(ev) (((hevent_t*)(ev))->userdata)
  58. typedef enum {
  59. HIO_TYPE_UNKNOWN = 0,
  60. HIO_TYPE_STDIN = 0x00000001,
  61. HIO_TYPE_STDOUT = 0x00000002,
  62. HIO_TYPE_STDERR = 0x00000004,
  63. HIO_TYPE_STDIO = 0x0000000F,
  64. HIO_TYPE_FILE = 0x00000010,
  65. HIO_TYPE_IP = 0x00000100,
  66. HIO_TYPE_UDP = 0x00001000,
  67. HIO_TYPE_TCP = 0x00010000,
  68. HIO_TYPE_SSL = 0x00020000,
  69. HIO_TYPE_SOCKET = 0x00FFFF00,
  70. } hio_type_e;
  71. // loop
  72. #define HLOOP_FLAG_RUN_ONCE 0x00000001
  73. #define HLOOP_FLAG_AUTO_FREE 0x00000002
  74. hloop_t* hloop_new(int flags DEFAULT(HLOOP_FLAG_AUTO_FREE));
  75. // WARN: Not allow to call hloop_free when HLOOP_INIT_FLAG_AUTO_FREE set.
  76. void hloop_free(hloop_t** pp);
  77. // NOTE: when no active events, loop will quit.
  78. int hloop_run(hloop_t* loop);
  79. int hloop_stop(hloop_t* loop);
  80. int hloop_pause(hloop_t* loop);
  81. int hloop_resume(hloop_t* loop);
  82. void hloop_update_time(hloop_t* loop);
  83. time_t hloop_now(hloop_t* loop); // s
  84. uint64_t hloop_now_hrtime(hloop_t* loop); // us
  85. void hloop_set_userdata(hloop_t* loop, void* userdata);
  86. void* hloop_userdata(hloop_t* loop);
  87. // idle
  88. hidle_t* hidle_add(hloop_t* loop, hidle_cb cb, uint32_t repeat DEFAULT(INFINITE));
  89. void hidle_del(hidle_t* idle);
  90. // timer
  91. // @param timeout: unit(ms)
  92. htimer_t* htimer_add(hloop_t* loop, htimer_cb cb, uint64_t timeout, uint32_t repeat DEFAULT(INFINITE));
  93. /*
  94. * minute hour day week month cb
  95. * 0~59 0~23 1~31 0~6 1~12
  96. * 30 -1 -1 -1 -1 cron.hourly
  97. * 30 1 -1 -1 -1 cron.daily
  98. * 30 1 15 -1 -1 cron.monthly
  99. * 30 1 -1 7 -1 cron.weekly
  100. * 30 1 1 -1 10 cron.yearly
  101. */
  102. htimer_t* htimer_add_period(hloop_t* loop, htimer_cb cb,
  103. int8_t minute DEFAULT(0), int8_t hour DEFAULT(-1), int8_t day DEFAULT(-1),
  104. int8_t week DEFAULT(-1), int8_t month DEFAULT(-1), uint32_t repeat DEFAULT(INFINITE));
  105. void htimer_del(htimer_t* timer);
  106. void htimer_reset(htimer_t* timer);
  107. // io
  108. //-----------------------low-level apis---------------------------------------
  109. #define READ_EVENT 0x0001
  110. #define WRITE_EVENT 0x0004
  111. #define ALL_EVENTS READ_EVENT|WRITE_EVENT
  112. hio_t* hio_get(hloop_t* loop, int fd);
  113. int hio_add(hio_t* io, hio_cb cb, int events DEFAULT(READ_EVENT));
  114. int hio_del(hio_t* io, int events DEFAULT(ALL_EVENTS));
  115. int hio_fd (hio_t* io);
  116. int hio_error (hio_t* io);
  117. hio_type_e hio_type(hio_t* io);
  118. struct sockaddr* hio_localaddr(hio_t* io);
  119. struct sockaddr* hio_peeraddr (hio_t* io);
  120. void hio_set_readbuf(hio_t* io, void* buf, size_t len);
  121. // ssl
  122. int hio_enable_ssl(hio_t* io);
  123. void hio_setcb_accept (hio_t* io, haccept_cb accept_cb);
  124. void hio_setcb_connect (hio_t* io, hconnect_cb connect_cb);
  125. void hio_setcb_read (hio_t* io, hread_cb read_cb);
  126. void hio_setcb_write (hio_t* io, hwrite_cb write_cb);
  127. void hio_setcb_close (hio_t* io, hclose_cb close_cb);
  128. // NOTE: don't forget to call hio_set_readbuf
  129. int hio_read (hio_t* io);
  130. int hio_write (hio_t* io, const void* buf, size_t len);
  131. int hio_close (hio_t* io);
  132. int hio_accept (hio_t* io);
  133. int hio_connect(hio_t* io);
  134. //------------------high-level apis-------------------------------------------
  135. // hio_get -> hio_set_readbuf -> hio_setcb_read -> hio_read
  136. hio_t* hread (hloop_t* loop, int fd, void* buf, size_t len, hread_cb read_cb);
  137. // hio_get -> hio_setcb_write -> hio_write
  138. hio_t* hwrite (hloop_t* loop, int fd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL));
  139. // hio_get -> hio_close
  140. void hclose (hloop_t* loop, int fd);
  141. // tcp
  142. // hio_get -> hio_setcb_accept -> hio_accept
  143. hio_t* haccept (hloop_t* loop, int listenfd, haccept_cb accept_cb);
  144. // hio_get -> hio_setcb_connect -> hio_connect
  145. hio_t* hconnect (hloop_t* loop, int connfd, hconnect_cb connect_cb);
  146. // hio_get -> hio_set_readbuf -> hio_setcb_read -> hio_read
  147. hio_t* hrecv (hloop_t* loop, int connfd, void* buf, size_t len, hread_cb read_cb);
  148. // hio_get -> hio_setcb_write -> hio_write
  149. hio_t* hsend (hloop_t* loop, int connfd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL));
  150. // udp/ip
  151. // for HIO_TYPE_IP
  152. void hio_set_type(hio_t* io, hio_type_e type);
  153. void hio_set_localaddr(hio_t* io, struct sockaddr* addr, int addrlen);
  154. void hio_set_peeraddr (hio_t* io, struct sockaddr* addr, int addrlen);
  155. // NOTE: must call hio_set_peeraddr before hrecvfrom/hsendto
  156. // hio_get -> hio_set_readbuf -> hio_setcb_read -> hio_read
  157. hio_t* hrecvfrom (hloop_t* loop, int sockfd, void* buf, size_t len, hread_cb read_cb);
  158. // hio_get -> hio_setcb_write -> hio_write
  159. hio_t* hsendto (hloop_t* loop, int sockfd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL));
  160. //----------------- top-level apis---------------------------------------------
  161. // @tcp_server: socket -> bind -> listen -> haccept
  162. hio_t* create_tcp_server (hloop_t* loop, int port, haccept_cb accept_cb);
  163. // @tcp_client: resolver -> socket -> hio_get -> hio_set_peeraddr -> hconnect
  164. hio_t* create_tcp_client (hloop_t* loop, const char* host, int port, hconnect_cb connect_cb);
  165. // @udp_server: socket -> bind -> hio_get
  166. hio_t* create_udp_server (hloop_t* loop, int port);
  167. // @udp_client: resolver -> socket -> hio_get -> hio_set_peeraddr
  168. hio_t* create_udp_client (hloop_t* loop, const char* host, int port);
  169. END_EXTERN_C
  170. #endif // HW_LOOP_H_