hevent.h 6.9 KB

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