hevent.h 8.0 KB

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