hevent.h 6.2 KB

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