hloop.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #ifndef HW_LOOP_H_
  2. #define HW_LOOP_H_
  3. #include "hdef.h"
  4. BEGIN_EXTERN_C
  5. #include "htime.h"
  6. #include "array.h"
  7. #include "list.h"
  8. #include "heap.h"
  9. #include "queue.h"
  10. #include "hbuf.h"
  11. typedef struct hloop_s hloop_t;
  12. typedef struct hevent_s hevent_t;
  13. typedef struct hidle_s hidle_t;
  14. typedef struct htimer_s htimer_t;
  15. typedef struct htimeout_s htimeout_t;
  16. typedef struct hperiod_s hperiod_t;
  17. typedef struct hio_s hio_t;
  18. typedef void (*hevent_cb) (hevent_t* ev);
  19. typedef void (*hidle_cb) (hidle_t* idle);
  20. typedef void (*htimer_cb) (htimer_t* timer);
  21. typedef void (*hio_cb) (hio_t* io);
  22. typedef void (*haccept_cb) (hio_t* io, int connfd);
  23. typedef void (*hconnect_cb) (hio_t* io);
  24. typedef void (*hread_cb) (hio_t* io, void* buf, int readbytes);
  25. typedef void (*hwrite_cb) (hio_t* io, const void* buf, int writebytes);
  26. typedef void (*hclose_cb) (hio_t* io);
  27. typedef enum {
  28. HEVENT_TYPE_NONE = 0,
  29. HEVENT_TYPE_IDLE = 0x00000010,
  30. HEVENT_TYPE_TIMEOUT = 0x00000100,
  31. HEVENT_TYPE_PERIOD = 0x00000200,
  32. HEVENT_TYPE_TIMER = HEVENT_TYPE_TIMEOUT|HEVENT_TYPE_PERIOD,
  33. HEVENT_TYPE_IO = 0x00001000,
  34. } hevent_type_e;
  35. #define HEVENT_LOWEST_PRIORITY (-5)
  36. #define HEVENT_LOW_PRIORITY (-3)
  37. #define HEVENT_NORMAL_PRIORITY 0
  38. #define HEVENT_HIGH_PRIORITY 3
  39. #define HEVENT_HIGHEST_PRIORITY 5
  40. #define HEVENT_PRIORITY_SIZE (HEVENT_HIGHEST_PRIORITY-HEVENT_LOWEST_PRIORITY+1)
  41. #define HEVENT_PRIORITY_INDEX(priority) (priority-HEVENT_LOWEST_PRIORITY)
  42. #define HEVENT_FLAGS \
  43. unsigned destroy :1; \
  44. unsigned active :1; \
  45. unsigned pending :1;
  46. #define HEVENT_FIELDS \
  47. hloop_t* loop; \
  48. hevent_type_e event_type; \
  49. uint64_t event_id; \
  50. hevent_cb cb; \
  51. void* userdata; \
  52. int priority; \
  53. struct hevent_s* pending_next; \
  54. HEVENT_FLAGS
  55. struct hevent_s {
  56. HEVENT_FIELDS
  57. };
  58. struct hidle_s {
  59. HEVENT_FIELDS
  60. uint32_t repeat;
  61. //private:
  62. struct list_node node;
  63. };
  64. #define HTIMER_FIELDS \
  65. HEVENT_FIELDS \
  66. uint32_t repeat; \
  67. uint64_t next_timeout; \
  68. struct heap_node node;
  69. struct htimer_s {
  70. HTIMER_FIELDS
  71. };
  72. struct htimeout_s {
  73. HTIMER_FIELDS
  74. uint32_t timeout; \
  75. };
  76. struct hperiod_s {
  77. HTIMER_FIELDS
  78. int8_t minute;
  79. int8_t hour;
  80. int8_t day;
  81. int8_t week;
  82. int8_t month;
  83. };
  84. QUEUE_DECL(offset_buf_t, write_queue);
  85. typedef enum {
  86. HIO_TYPE_UNKNOWN = 0,
  87. HIO_TYPE_STDIN = 0x00000001,
  88. HIO_TYPE_STDOUT = 0x00000002,
  89. HIO_TYPE_STDERR = 0x00000004,
  90. HIO_TYPE_STDIO = HIO_TYPE_STDIN|HIO_TYPE_STDOUT|HIO_TYPE_STDERR,
  91. HIO_TYPE_FILE = 0x00000010,
  92. HIO_TYPE_TCP = 0x00000100,
  93. HIO_TYPE_UDP = 0x00001000,
  94. HIO_TYPE_IP = 0x00010000,
  95. HIO_TYPE_SOCKET = HIO_TYPE_TCP|HIO_TYPE_UDP|HIO_TYPE_IP
  96. } hio_type_e;
  97. struct hio_s {
  98. HEVENT_FIELDS
  99. unsigned ready :1;
  100. unsigned closed :1;
  101. unsigned accept :1;
  102. unsigned connect :1;
  103. unsigned connectex :1; // for ConnectEx/DisconnectEx
  104. unsigned recv :1;
  105. unsigned send :1;
  106. unsigned recvfrom :1;
  107. unsigned sendto :1;
  108. int fd;
  109. hio_type_e io_type;
  110. int error;
  111. int events;
  112. int revents;
  113. struct sockaddr* localaddr;
  114. struct sockaddr* peeraddr;
  115. hbuf_t readbuf; // for hread
  116. struct write_queue write_queue; // for hwrite
  117. // callbacks
  118. hread_cb read_cb;
  119. hwrite_cb write_cb;
  120. hclose_cb close_cb;
  121. haccept_cb accept_cb;
  122. hconnect_cb connect_cb;
  123. //private:
  124. int event_index[2]; // for poll,kqueue
  125. void* hovlp; // for iocp/overlapio
  126. };
  127. typedef enum {
  128. HLOOP_STATUS_STOP,
  129. HLOOP_STATUS_RUNNING,
  130. HLOOP_STATUS_PAUSE
  131. } hloop_status_e;
  132. ARRAY_DECL(hio_t*, io_array);
  133. struct hloop_s {
  134. hloop_status_e status;
  135. time_t start_time;
  136. // hrtime: us
  137. uint64_t start_hrtime;
  138. uint64_t end_hrtime;
  139. uint64_t cur_hrtime;
  140. uint64_t loop_cnt;
  141. void* userdata;
  142. //private:
  143. // events
  144. uint64_t event_counter;
  145. uint32_t nactives;
  146. uint32_t npendings;
  147. // pendings: with priority as array.index
  148. hevent_t* pendings[HEVENT_PRIORITY_SIZE];
  149. // idles
  150. struct list_head idles;
  151. uint32_t nidles;
  152. // timers
  153. struct heap timers;
  154. uint32_t ntimers;
  155. // ios: with fd as array.index
  156. struct io_array ios;
  157. uint32_t nios;
  158. void* iowatcher;
  159. };
  160. // loop
  161. int hloop_init(hloop_t* loop);
  162. //void hloop_cleanup(hloop_t* loop);
  163. int hloop_run(hloop_t* loop);
  164. int hloop_stop(hloop_t* loop);
  165. int hloop_pause(hloop_t* loop);
  166. int hloop_resume(hloop_t* loop);
  167. static inline void hloop_update_time(hloop_t* loop) {
  168. loop->cur_hrtime = gethrtime();
  169. }
  170. static inline time_t hloop_now(hloop_t* loop) {
  171. return loop->start_time + (loop->cur_hrtime - loop->start_hrtime) / 1000000;
  172. }
  173. static inline uint64_t hloop_now_hrtime(hloop_t* loop) {
  174. return loop->start_time*1e6 + (loop->cur_hrtime - loop->start_hrtime);
  175. }
  176. // idle
  177. hidle_t* hidle_add(hloop_t* loop, hidle_cb cb, uint32_t repeat DEFAULT(INFINITE));
  178. void hidle_del(hidle_t* idle);
  179. // timer
  180. // @param timeout: unit(ms)
  181. htimer_t* htimer_add(hloop_t* loop, htimer_cb cb, uint64_t timeout, uint32_t repeat DEFAULT(INFINITE));
  182. /*
  183. * minute hour day week month cb
  184. * 0~59 0~23 1~31 0~6 1~12
  185. * 30 -1 -1 -1 -1 cron.hourly
  186. * 30 1 -1 -1 -1 cron.daily
  187. * 30 1 15 -1 -1 cron.monthly
  188. * 30 1 -1 7 -1 cron.weekly
  189. * 30 1 1 -1 10 cron.yearly
  190. */
  191. htimer_t* htimer_add_period(hloop_t* loop, htimer_cb cb,
  192. int8_t minute DEFAULT(0), int8_t hour DEFAULT(-1), int8_t day DEFAULT(-1),
  193. int8_t week DEFAULT(-1), int8_t month DEFAULT(-1), uint32_t repeat DEFAULT(INFINITE));
  194. void htimer_del(htimer_t* timer);
  195. void htimer_reset(htimer_t* timer);
  196. // io
  197. // low-level api
  198. #define READ_EVENT 0x0001
  199. #define WRITE_EVENT 0x0004
  200. #define ALL_EVENTS READ_EVENT|WRITE_EVENT
  201. hio_t* hio_get(hloop_t* loop, int fd);
  202. int hio_add(hio_t* io, hio_cb cb, int events DEFAULT(READ_EVENT));
  203. int hio_del(hio_t* io, int events DEFAULT(ALL_EVENTS));
  204. void hio_setlocaladdr(hio_t* io, struct sockaddr* addr, int addrlen);
  205. void hio_setpeeraddr (hio_t* io, struct sockaddr* addr, int addrlen);
  206. // high-level api
  207. hio_t* hread (hloop_t* loop, int fd, void* buf, size_t len, hread_cb read_cb);
  208. hio_t* hwrite (hloop_t* loop, int fd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL));
  209. void hclose (hio_t* io);
  210. // tcp
  211. hio_t* haccept (hloop_t* loop, int listenfd, haccept_cb accept_cb);
  212. hio_t* hconnect (hloop_t* loop, int connfd, hconnect_cb connect_cb);
  213. hio_t* hrecv (hloop_t* loop, int connfd, void* buf, size_t len, hread_cb read_cb);
  214. hio_t* hsend (hloop_t* loop, int connfd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL));
  215. // @tcp_server: socket -> bind -> listen -> haccept
  216. hio_t* create_tcp_server (hloop_t* loop, int port, haccept_cb accept_cb);
  217. // @tcp_client: resolver -> socket -> hio_get -> hio_setpeeraddr -> hconnect
  218. hio_t* create_tcp_client (hloop_t* loop, const char* host, int port, hconnect_cb connect_cb);
  219. // udp/ip
  220. // NOTE: recvfrom/sendto struct sockaddr* addr save in io->peeraddr
  221. hio_t* hrecvfrom (hloop_t* loop, int sockfd, void* buf, size_t len, hread_cb read_cb);
  222. hio_t* hsendto (hloop_t* loop, int sockfd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL));
  223. // @udp_server: socket -> bind -> hio_get
  224. hio_t* create_udp_server(hloop_t* loop, int port);
  225. // @udp_client: resolver -> socket -> hio_get -> hio_setpeeraddr
  226. hio_t* create_udp_client(hloop_t* loop, const char* host, int port);
  227. END_EXTERN_C
  228. #endif // HW_LOOP_H_