1
0

hevent.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. // sizeof(struct hio_s)=344 on linux-x64
  80. struct hio_s {
  81. HEVENT_FIELDS
  82. // flags
  83. unsigned ready :1;
  84. unsigned closed :1;
  85. unsigned accept :1;
  86. unsigned connect :1;
  87. unsigned connectex :1; // for ConnectEx/DisconnectEx
  88. unsigned recv :1;
  89. unsigned send :1;
  90. unsigned recvfrom :1;
  91. unsigned sendto :1;
  92. unsigned close :1;
  93. unsigned read_once :1; // for hio_read_once
  94. unsigned alloced_readbuf :1; // for hio_read_until, hio_set_unpack
  95. // public:
  96. hio_type_e io_type;
  97. uint32_t id; // fd cannot be used as unique identifier, so we provide an id
  98. int fd;
  99. int error;
  100. int events;
  101. int revents;
  102. struct sockaddr* localaddr;
  103. struct sockaddr* peeraddr;
  104. offset_buf_t readbuf; // for read
  105. int read_until; // for hio_read_until
  106. uint32_t small_readbytes_cnt; // for readbuf autosize
  107. struct write_queue write_queue; // for write
  108. hrecursive_mutex_t write_mutex; // lock write and write_queue
  109. uint32_t write_queue_bytes;
  110. // callbacks
  111. hread_cb read_cb;
  112. hwrite_cb write_cb;
  113. hclose_cb close_cb;
  114. haccept_cb accept_cb;
  115. hconnect_cb connect_cb;
  116. // timers
  117. int connect_timeout; // ms
  118. int close_timeout; // ms
  119. int keepalive_timeout; // ms
  120. int heartbeat_interval; // ms
  121. hio_send_heartbeat_fn heartbeat_fn;
  122. htimer_t* connect_timer;
  123. htimer_t* close_timer;
  124. htimer_t* keepalive_timer;
  125. htimer_t* heartbeat_timer;
  126. // upstream
  127. struct hio_s* upstream_io; // for hio_setup_upstream
  128. // unpack
  129. unpack_setting_t* unpack_setting; // for hio_set_unpack
  130. // ssl
  131. void* ssl; // for hio_enable_ssl / hio_set_ssl
  132. // context
  133. void* ctx; // for hio_context / hio_set_context
  134. // private:
  135. #if defined(EVENT_POLL) || defined(EVENT_KQUEUE)
  136. int event_index[2]; // for poll,kqueue
  137. #endif
  138. #ifdef EVENT_IOCP
  139. void* hovlp; // for iocp/overlapio
  140. #endif
  141. };
  142. /*
  143. * hio lifeline:
  144. *
  145. * fd =>
  146. * hio_get => HV_ALLOC_SIZEOF(io) => hio_init =>
  147. *
  148. * hio_ready => hio_add => hio_read_cb/hio_write_cb =>
  149. * hio_close => hio_done => hio_close_cb =>
  150. *
  151. * hloop_stop => hloop_free => hio_free => HV_FREE(io)
  152. */
  153. void hio_init(hio_t* io);
  154. void hio_ready(hio_t* io);
  155. void hio_done(hio_t* io);
  156. void hio_free(hio_t* io);
  157. uint32_t hio_next_id();
  158. void hio_accept_cb(hio_t* io);
  159. void hio_connect_cb(hio_t* io);
  160. void hio_read_cb(hio_t* io, void* buf, int len);
  161. void hio_write_cb(hio_t* io, const void* buf, int len);
  162. void hio_close_cb(hio_t* io);
  163. void hio_del_connect_timer(hio_t* io);
  164. void hio_del_close_timer(hio_t* io);
  165. void hio_del_keepalive_timer(hio_t* io);
  166. void hio_del_heartbeat_timer(hio_t* io);
  167. static inline bool hio_is_loop_readbuf(hio_t* io) {
  168. return io->readbuf.base == io->loop->readbuf.base;
  169. }
  170. static inline bool hio_is_alloced_readbuf(hio_t* io) {
  171. return io->alloced_readbuf;
  172. }
  173. void hio_alloc_readbuf(hio_t* io, int len);
  174. void hio_free_readbuf(hio_t* io);
  175. #define EVENT_ENTRY(p) container_of(p, hevent_t, pending_node)
  176. #define IDLE_ENTRY(p) container_of(p, hidle_t, node)
  177. #define TIMER_ENTRY(p) container_of(p, htimer_t, node)
  178. #define EVENT_ACTIVE(ev) \
  179. if (!ev->active) {\
  180. ev->active = 1;\
  181. ev->loop->nactives++;\
  182. }\
  183. #define EVENT_INACTIVE(ev) \
  184. if (ev->active) {\
  185. ev->active = 0;\
  186. ev->loop->nactives--;\
  187. }\
  188. #define EVENT_PENDING(ev) \
  189. do {\
  190. if (!ev->pending) {\
  191. ev->pending = 1;\
  192. ev->loop->npendings++;\
  193. hevent_t** phead = &ev->loop->pendings[HEVENT_PRIORITY_INDEX(ev->priority)];\
  194. ev->pending_next = *phead;\
  195. *phead = (hevent_t*)ev;\
  196. }\
  197. } while(0)
  198. #define EVENT_ADD(loop, ev, cb) \
  199. do {\
  200. ev->loop = loop;\
  201. ev->event_id = hloop_next_event_id();\
  202. ev->cb = (hevent_cb)cb;\
  203. EVENT_ACTIVE(ev);\
  204. } while(0)
  205. #define EVENT_DEL(ev) \
  206. do {\
  207. EVENT_INACTIVE(ev);\
  208. if (!ev->pending) {\
  209. HV_FREE(ev);\
  210. }\
  211. } while(0)
  212. #define EVENT_RESET(ev) \
  213. do {\
  214. ev->destroy = 0;\
  215. EVENT_ACTIVE(ev);\
  216. ev->pending = 0;\
  217. } while(0)
  218. #endif // HV_EVENT_H_