hevent.h 7.2 KB

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