hevent.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #ifndef HV_EVENT_H_
  2. #define HV_EVENT_H_
  3. #include "hloop.h"
  4. #include "iowatcher.h"
  5. #include "rudp.h"
  6. #include "hbuf.h"
  7. #include "hmutex.h"
  8. #include "array.h"
  9. #include "list.h"
  10. #include "heap.h"
  11. #include "queue.h"
  12. #define HLOOP_READ_BUFSIZE 8192 // 8K
  13. #define READ_BUFSIZE_HIGH_WATER 65536 // 64K
  14. #define WRITE_BUFSIZE_HIGH_WATER (1U << 23) // 8M
  15. #define MAX_READ_BUFSIZE (1U << 24) // 16M
  16. #define MAX_WRITE_BUFSIZE (1U << 24) // 16M
  17. // hio_read_flags
  18. #define HIO_READ_ONCE 0x1
  19. #define HIO_READ_UNTIL_LENGTH 0x2
  20. #define HIO_READ_UNTIL_DELIM 0x4
  21. ARRAY_DECL(hio_t*, io_array);
  22. ARRAY_DECL(hsignal_t*, signal_array);
  23. QUEUE_DECL(hevent_t, event_queue);
  24. struct hloop_s {
  25. uint32_t flags;
  26. hloop_status_e status;
  27. uint64_t start_ms; // ms
  28. uint64_t start_hrtime; // us
  29. uint64_t end_hrtime;
  30. uint64_t cur_hrtime;
  31. uint64_t loop_cnt;
  32. long pid;
  33. long tid;
  34. void* userdata;
  35. //private:
  36. // events
  37. uint32_t intern_nevents;
  38. uint32_t nactives;
  39. uint32_t npendings;
  40. // pendings: with priority as array.index
  41. hevent_t* pendings[HEVENT_PRIORITY_SIZE];
  42. // signals
  43. struct signal_array signals;
  44. uint32_t nsignals;
  45. // idles
  46. struct list_head idles;
  47. uint32_t nidles;
  48. // timers
  49. struct heap timers; // monotonic time
  50. struct heap realtimers; // realtime
  51. uint32_t ntimers;
  52. // ios: with fd as array.index
  53. struct io_array ios;
  54. uint32_t nios;
  55. // one loop per thread, so one readbuf per loop is OK.
  56. hbuf_t readbuf;
  57. void* iowatcher;
  58. // custom_events
  59. int eventfds[2];
  60. event_queue custom_events;
  61. hmutex_t custom_events_mutex;
  62. };
  63. uint64_t hloop_next_event_id();
  64. struct hidle_s {
  65. HEVENT_FIELDS
  66. uint32_t repeat;
  67. //private:
  68. struct list_node node;
  69. };
  70. #define HTIMER_FIELDS \
  71. HEVENT_FIELDS \
  72. uint32_t repeat; \
  73. uint64_t next_timeout; \
  74. struct heap_node node;
  75. struct htimer_s {
  76. HTIMER_FIELDS
  77. };
  78. struct htimeout_s {
  79. HTIMER_FIELDS
  80. uint32_t timeout; \
  81. };
  82. struct hperiod_s {
  83. HTIMER_FIELDS
  84. int8_t minute;
  85. int8_t hour;
  86. int8_t day;
  87. int8_t week;
  88. int8_t month;
  89. };
  90. QUEUE_DECL(offset_buf_t, write_queue);
  91. // sizeof(struct hio_s)=416 on linux-x64
  92. struct hio_s {
  93. HEVENT_FIELDS
  94. // flags
  95. unsigned ready :1;
  96. unsigned connected :1;
  97. unsigned closed :1;
  98. unsigned accept :1;
  99. unsigned connect :1;
  100. unsigned connectex :1; // for ConnectEx/DisconnectEx
  101. unsigned recv :1;
  102. unsigned send :1;
  103. unsigned recvfrom :1;
  104. unsigned sendto :1;
  105. unsigned close :1;
  106. unsigned alloced_readbuf :1; // for hio_alloc_readbuf
  107. unsigned alloced_ssl_ctx :1; // for hio_new_ssl_ctx
  108. // public:
  109. hio_type_e io_type;
  110. uint32_t id; // fd cannot be used as unique identifier, so we provide an id
  111. int fd;
  112. int error;
  113. int events;
  114. int revents;
  115. struct sockaddr* localaddr;
  116. struct sockaddr* peeraddr;
  117. uint64_t last_read_hrtime;
  118. uint64_t last_write_hrtime;
  119. // read
  120. fifo_buf_t readbuf;
  121. unsigned int read_flags;
  122. // for hio_read_until
  123. union {
  124. unsigned int read_until_length;
  125. unsigned char read_until_delim;
  126. };
  127. uint32_t max_read_bufsize;
  128. uint32_t small_readbytes_cnt; // for readbuf autosize
  129. // write
  130. struct write_queue write_queue;
  131. hrecursive_mutex_t write_mutex; // lock write and write_queue
  132. uint32_t write_bufsize;
  133. uint32_t max_write_bufsize;
  134. // callbacks
  135. hread_cb read_cb;
  136. hwrite_cb write_cb;
  137. hclose_cb close_cb;
  138. haccept_cb accept_cb;
  139. hconnect_cb connect_cb;
  140. // timers
  141. int connect_timeout; // ms
  142. int close_timeout; // ms
  143. int read_timeout; // ms
  144. int write_timeout; // ms
  145. int keepalive_timeout; // ms
  146. int heartbeat_interval; // ms
  147. hio_send_heartbeat_fn heartbeat_fn;
  148. htimer_t* connect_timer;
  149. htimer_t* close_timer;
  150. htimer_t* read_timer;
  151. htimer_t* write_timer;
  152. htimer_t* keepalive_timer;
  153. htimer_t* heartbeat_timer;
  154. // upstream
  155. struct hio_s* upstream_io; // for hio_setup_upstream
  156. // unpack
  157. unpack_setting_t* unpack_setting; // for hio_set_unpack
  158. // ssl
  159. void* ssl; // for hio_set_ssl
  160. void* ssl_ctx; // for hio_set_ssl_ctx
  161. char* hostname; // for hssl_set_sni_hostname
  162. // context
  163. void* ctx; // for hio_context / hio_set_context
  164. // private:
  165. #if defined(EVENT_POLL) || defined(EVENT_KQUEUE)
  166. int event_index[2]; // for poll,kqueue
  167. #endif
  168. #ifdef EVENT_IOCP
  169. void* hovlp; // for iocp/overlapio
  170. #endif
  171. #if WITH_RUDP
  172. rudp_t rudp;
  173. #if WITH_KCP
  174. kcp_setting_t* kcp_setting;
  175. #endif
  176. #endif
  177. };
  178. /*
  179. * hio lifeline:
  180. *
  181. * fd =>
  182. * hio_get => HV_ALLOC_SIZEOF(io) => hio_init => hio_ready
  183. *
  184. * hio_read => hio_add(HV_READ) => hio_read_cb
  185. * hio_write => hio_add(HV_WRITE) => hio_write_cb
  186. * hio_close => hio_done => hio_del(HV_RDWR) => hio_close_cb
  187. *
  188. * hloop_stop => hloop_free => hio_free => HV_FREE(io)
  189. */
  190. void hio_init(hio_t* io);
  191. void hio_ready(hio_t* io);
  192. void hio_done(hio_t* io);
  193. void hio_free(hio_t* io);
  194. uint32_t hio_next_id();
  195. void hio_accept_cb(hio_t* io);
  196. void hio_connect_cb(hio_t* io);
  197. void hio_handle_read(hio_t* io, void* buf, int readbytes);
  198. void hio_read_cb(hio_t* io, void* buf, int len);
  199. void hio_write_cb(hio_t* io, const void* buf, int len);
  200. void hio_close_cb(hio_t* io);
  201. void hio_del_connect_timer(hio_t* io);
  202. void hio_del_close_timer(hio_t* io);
  203. void hio_del_read_timer(hio_t* io);
  204. void hio_del_write_timer(hio_t* io);
  205. void hio_del_keepalive_timer(hio_t* io);
  206. void hio_del_heartbeat_timer(hio_t* io);
  207. static inline void hio_use_loop_readbuf(hio_t* io) {
  208. hloop_t* loop = io->loop;
  209. if (loop->readbuf.len == 0) {
  210. loop->readbuf.len = HLOOP_READ_BUFSIZE;
  211. HV_ALLOC(loop->readbuf.base, loop->readbuf.len);
  212. }
  213. io->readbuf.base = loop->readbuf.base;
  214. io->readbuf.len = loop->readbuf.len;
  215. }
  216. static inline bool hio_is_loop_readbuf(hio_t* io) {
  217. return io->readbuf.base == io->loop->readbuf.base;
  218. }
  219. static inline bool hio_is_alloced_readbuf(hio_t* io) {
  220. return io->alloced_readbuf;
  221. }
  222. void hio_alloc_readbuf(hio_t* io, int len);
  223. void hio_free_readbuf(hio_t* io);
  224. void hio_memmove_readbuf(hio_t* io);
  225. #define EVENT_ENTRY(p) container_of(p, hevent_t, pending_node)
  226. #define IDLE_ENTRY(p) container_of(p, hidle_t, node)
  227. #define TIMER_ENTRY(p) container_of(p, htimer_t, node)
  228. #define EVENT_ACTIVE(ev) \
  229. if (!ev->active) {\
  230. ev->active = 1;\
  231. ev->loop->nactives++;\
  232. }\
  233. #define EVENT_INACTIVE(ev) \
  234. if (ev->active) {\
  235. ev->active = 0;\
  236. ev->loop->nactives--;\
  237. }\
  238. #define EVENT_PENDING(ev) \
  239. do {\
  240. if (!ev->pending) {\
  241. ev->pending = 1;\
  242. ev->loop->npendings++;\
  243. hevent_t** phead = &ev->loop->pendings[HEVENT_PRIORITY_INDEX(ev->priority)];\
  244. ev->pending_next = *phead;\
  245. *phead = (hevent_t*)ev;\
  246. }\
  247. } while(0)
  248. #define EVENT_ADD(loop, ev, cb) \
  249. do {\
  250. ev->loop = loop;\
  251. ev->event_id = hloop_next_event_id();\
  252. ev->cb = (hevent_cb)cb;\
  253. EVENT_ACTIVE(ev);\
  254. } while(0)
  255. #define EVENT_DEL(ev) \
  256. do {\
  257. EVENT_INACTIVE(ev);\
  258. if (!ev->pending) {\
  259. HV_FREE(ev);\
  260. }\
  261. } while(0)
  262. #define EVENT_RESET(ev) \
  263. do {\
  264. ev->destroy = 0;\
  265. EVENT_ACTIVE(ev);\
  266. ev->pending = 0;\
  267. } while(0)
  268. #endif // HV_EVENT_H_