hevent.h 7.3 KB

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