hevent.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. *
  137. * fd =>
  138. * hio_get => HV_ALLOC_SIZEOF(io) => hio_init =>
  139. *
  140. * hio_ready => hio_add => hio_read_cb/hio_write_cb =>
  141. * hio_close => hio_done => hio_close_cb =>
  142. *
  143. * hloop_stop => hloop_free => hio_free => HV_FREE(io)
  144. */
  145. void hio_init(hio_t* io);
  146. void hio_ready(hio_t* io);
  147. void hio_done(hio_t* io);
  148. void hio_free(hio_t* io);
  149. uint32_t hio_next_id();
  150. void hio_accept_cb(hio_t* io);
  151. void hio_connect_cb(hio_t* io);
  152. void hio_read_cb(hio_t* io, void* buf, int len);
  153. void hio_write_cb(hio_t* io, const void* buf, int len);
  154. void hio_close_cb(hio_t* io);
  155. void hio_del_connect_timer(hio_t* io);
  156. void hio_del_close_timer(hio_t* io);
  157. void hio_del_keepalive_timer(hio_t* io);
  158. void hio_del_heartbeat_timer(hio_t* io);
  159. static inline bool hio_is_loop_readbuf(hio_t* io) {
  160. return io->readbuf.base == io->loop->readbuf.base;
  161. }
  162. static inline bool hio_is_alloced_readbuf(hio_t* io) {
  163. return io->alloced_readbuf;
  164. }
  165. void hio_alloc_readbuf(hio_t* io, int len);
  166. void hio_free_readbuf(hio_t* io);
  167. #define EVENT_ENTRY(p) container_of(p, hevent_t, pending_node)
  168. #define IDLE_ENTRY(p) container_of(p, hidle_t, node)
  169. #define TIMER_ENTRY(p) container_of(p, htimer_t, node)
  170. #define EVENT_ACTIVE(ev) \
  171. if (!ev->active) {\
  172. ev->active = 1;\
  173. ev->loop->nactives++;\
  174. }\
  175. #define EVENT_INACTIVE(ev) \
  176. if (ev->active) {\
  177. ev->active = 0;\
  178. ev->loop->nactives--;\
  179. }\
  180. #define EVENT_PENDING(ev) \
  181. do {\
  182. if (!ev->pending) {\
  183. ev->pending = 1;\
  184. ev->loop->npendings++;\
  185. hevent_t** phead = &ev->loop->pendings[HEVENT_PRIORITY_INDEX(ev->priority)];\
  186. ev->pending_next = *phead;\
  187. *phead = (hevent_t*)ev;\
  188. }\
  189. } while(0)
  190. #define EVENT_ADD(loop, ev, cb) \
  191. do {\
  192. ev->loop = loop;\
  193. ev->event_id = hloop_next_event_id();\
  194. ev->cb = (hevent_cb)cb;\
  195. EVENT_ACTIVE(ev);\
  196. } while(0)
  197. #define EVENT_DEL(ev) \
  198. do {\
  199. EVENT_INACTIVE(ev);\
  200. if (!ev->pending) {\
  201. HV_FREE(ev);\
  202. }\
  203. } while(0)
  204. #define EVENT_RESET(ev) \
  205. do {\
  206. ev->destroy = 0;\
  207. EVENT_ACTIVE(ev);\
  208. ev->pending = 0;\
  209. } while(0)
  210. #endif // HV_EVENT_H_