hevent.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #ifndef HV_EVENT_H_
  2. #define HV_EVENT_H_
  3. #include "hloop.h"
  4. #include "iowatcher.h"
  5. #include "hbuf.h"
  6. #include "hmutex.h"
  7. #include "array.h"
  8. #include "list.h"
  9. #include "heap.h"
  10. #include "queue.h"
  11. #define HLOOP_READ_BUFSIZE 8192 // 8K
  12. #define READ_BUFSIZE_HIGH_WATER 65536 // 64K
  13. #define WRITE_QUEUE_HIGH_WATER (1U << 23) // 8M
  14. ARRAY_DECL(hio_t*, io_array);
  15. QUEUE_DECL(hevent_t, event_queue);
  16. struct hloop_s {
  17. uint32_t flags;
  18. hloop_status_e status;
  19. uint64_t start_ms; // ms
  20. uint64_t start_hrtime; // us
  21. uint64_t end_hrtime;
  22. uint64_t cur_hrtime;
  23. uint64_t loop_cnt;
  24. long pid;
  25. long tid;
  26. void* userdata;
  27. //private:
  28. // events
  29. uint32_t intern_nevents;
  30. uint32_t nactives;
  31. uint32_t npendings;
  32. // pendings: with priority as array.index
  33. hevent_t* pendings[HEVENT_PRIORITY_SIZE];
  34. // idles
  35. struct list_head idles;
  36. uint32_t nidles;
  37. // timers
  38. struct heap timers;
  39. uint32_t ntimers;
  40. // ios: with fd as array.index
  41. struct io_array ios;
  42. uint32_t nios;
  43. // one loop per thread, so one readbuf per loop is OK.
  44. hbuf_t readbuf;
  45. void* iowatcher;
  46. // custom_events
  47. int sockpair[2];
  48. event_queue custom_events;
  49. hmutex_t custom_events_mutex;
  50. };
  51. uint64_t hloop_next_event_id();
  52. struct hidle_s {
  53. HEVENT_FIELDS
  54. uint32_t repeat;
  55. //private:
  56. struct list_node node;
  57. };
  58. #define HTIMER_FIELDS \
  59. HEVENT_FIELDS \
  60. uint32_t repeat; \
  61. uint64_t next_timeout; \
  62. struct heap_node node;
  63. struct htimer_s {
  64. HTIMER_FIELDS
  65. };
  66. struct htimeout_s {
  67. HTIMER_FIELDS
  68. uint32_t timeout; \
  69. };
  70. struct hperiod_s {
  71. HTIMER_FIELDS
  72. int8_t minute;
  73. int8_t hour;
  74. int8_t day;
  75. int8_t week;
  76. int8_t month;
  77. };
  78. QUEUE_DECL(offset_buf_t, write_queue);
  79. struct hio_s {
  80. HEVENT_FIELDS
  81. // flags
  82. unsigned ready :1;
  83. unsigned closed :1;
  84. unsigned accept :1;
  85. unsigned connect :1;
  86. unsigned connectex :1; // for ConnectEx/DisconnectEx
  87. unsigned recv :1;
  88. unsigned send :1;
  89. unsigned recvfrom :1;
  90. unsigned sendto :1;
  91. unsigned close :1;
  92. unsigned read_once :1; // for hio_read_once
  93. unsigned alloced_readbuf :1; // for hio_read_until, hio_set_unpack
  94. // public:
  95. uint32_t id; // fd cannot be used as unique identifier, so we provide an id
  96. int fd;
  97. hio_type_e io_type;
  98. int error;
  99. int events;
  100. int revents;
  101. struct sockaddr* localaddr;
  102. struct sockaddr* peeraddr;
  103. offset_buf_t readbuf; // for read
  104. int read_until; // for hio_read_until
  105. uint32_t small_readbytes_cnt;
  106. struct write_queue write_queue; // for write
  107. hrecursive_mutex_t write_mutex; // lock write and write_queue
  108. uint32_t write_queue_bytes;
  109. // callbacks
  110. hread_cb read_cb;
  111. hwrite_cb write_cb;
  112. hclose_cb close_cb;
  113. haccept_cb accept_cb;
  114. hconnect_cb connect_cb;
  115. // timers
  116. int connect_timeout; // ms
  117. htimer_t* connect_timer;
  118. int close_timeout; // ms
  119. htimer_t* close_timer;
  120. int keepalive_timeout; // ms
  121. htimer_t* keepalive_timer;
  122. int heartbeat_interval; // ms
  123. hio_send_heartbeat_fn heartbeat_fn;
  124. htimer_t* heartbeat_timer;
  125. // upstream
  126. struct hio_s* upstream_io; // for hio_setup_upstream
  127. // unpack
  128. unpack_setting_t* unpack_setting; // for hio_set_unpack
  129. // ssl
  130. void* ssl; // for hio_enable_ssl / hio_set_ssl
  131. void* ctx; // for hio_context / hio_set_context
  132. // private:
  133. #if defined(EVENT_POLL) || defined(EVENT_KQUEUE)
  134. int event_index[2]; // for poll,kqueue
  135. #endif
  136. #ifdef EVENT_IOCP
  137. void* hovlp; // for iocp/overlapio
  138. #endif
  139. };
  140. /*
  141. * hio lifeline:
  142. *
  143. * fd =>
  144. * hio_get => HV_ALLOC_SIZEOF(io) => hio_init =>
  145. *
  146. * hio_ready => hio_add => hio_read_cb/hio_write_cb =>
  147. * hio_close => hio_done => hio_close_cb =>
  148. *
  149. * hloop_stop => hloop_free => hio_free => HV_FREE(io)
  150. */
  151. void hio_init(hio_t* io);
  152. void hio_ready(hio_t* io);
  153. void hio_done(hio_t* io);
  154. void hio_free(hio_t* io);
  155. uint32_t hio_next_id();
  156. void hio_accept_cb(hio_t* io);
  157. void hio_connect_cb(hio_t* io);
  158. void hio_read_cb(hio_t* io, void* buf, int len);
  159. void hio_write_cb(hio_t* io, const void* buf, int len);
  160. void hio_close_cb(hio_t* io);
  161. void hio_del_connect_timer(hio_t* io);
  162. void hio_del_close_timer(hio_t* io);
  163. void hio_del_keepalive_timer(hio_t* io);
  164. void hio_del_heartbeat_timer(hio_t* io);
  165. static inline bool hio_is_loop_readbuf(hio_t* io) {
  166. return io->readbuf.base == io->loop->readbuf.base;
  167. }
  168. static inline bool hio_is_alloced_readbuf(hio_t* io) {
  169. return io->alloced_readbuf;
  170. }
  171. void hio_alloc_readbuf(hio_t* io, int len);
  172. void hio_free_readbuf(hio_t* io);
  173. #define EVENT_ENTRY(p) container_of(p, hevent_t, pending_node)
  174. #define IDLE_ENTRY(p) container_of(p, hidle_t, node)
  175. #define TIMER_ENTRY(p) container_of(p, htimer_t, node)
  176. #define EVENT_ACTIVE(ev) \
  177. if (!ev->active) {\
  178. ev->active = 1;\
  179. ev->loop->nactives++;\
  180. }\
  181. #define EVENT_INACTIVE(ev) \
  182. if (ev->active) {\
  183. ev->active = 0;\
  184. ev->loop->nactives--;\
  185. }\
  186. #define EVENT_PENDING(ev) \
  187. do {\
  188. if (!ev->pending) {\
  189. ev->pending = 1;\
  190. ev->loop->npendings++;\
  191. hevent_t** phead = &ev->loop->pendings[HEVENT_PRIORITY_INDEX(ev->priority)];\
  192. ev->pending_next = *phead;\
  193. *phead = (hevent_t*)ev;\
  194. }\
  195. } while(0)
  196. #define EVENT_ADD(loop, ev, cb) \
  197. do {\
  198. ev->loop = loop;\
  199. ev->event_id = hloop_next_event_id();\
  200. ev->cb = (hevent_cb)cb;\
  201. EVENT_ACTIVE(ev);\
  202. } while(0)
  203. #define EVENT_DEL(ev) \
  204. do {\
  205. EVENT_INACTIVE(ev);\
  206. if (!ev->pending) {\
  207. HV_FREE(ev);\
  208. }\
  209. } while(0)
  210. #define EVENT_RESET(ev) \
  211. do {\
  212. ev->destroy = 0;\
  213. EVENT_ACTIVE(ev);\
  214. ev->pending = 0;\
  215. } while(0)
  216. #endif // HV_EVENT_H_