hevent.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #ifndef HV_EVENT_H_
  2. #define HV_EVENT_H_
  3. #include "hloop.h"
  4. #include "hbuf.h"
  5. #include "hmutex.h"
  6. #include "array.h"
  7. #include "list.h"
  8. #include "heap.h"
  9. #include "queue.h"
  10. #define HLOOP_READ_BUFSIZE 8192
  11. ARRAY_DECL(hio_t*, io_array);
  12. QUEUE_DECL(hevent_t, event_queue);
  13. struct hloop_s {
  14. uint32_t flags;
  15. hloop_status_e status;
  16. uint64_t start_ms; // ms
  17. uint64_t start_hrtime; // us
  18. uint64_t end_hrtime;
  19. uint64_t cur_hrtime;
  20. uint64_t loop_cnt;
  21. long pid;
  22. long tid;
  23. void* userdata;
  24. //private:
  25. // events
  26. uint32_t intern_nevents;
  27. uint32_t nactives;
  28. uint32_t npendings;
  29. // pendings: with priority as array.index
  30. hevent_t* pendings[HEVENT_PRIORITY_SIZE];
  31. // idles
  32. struct list_head idles;
  33. uint32_t nidles;
  34. // timers
  35. struct heap timers;
  36. uint32_t ntimers;
  37. // ios: with fd as array.index
  38. struct io_array ios;
  39. uint32_t nios;
  40. // one loop per thread, so one readbuf per loop is OK.
  41. hbuf_t readbuf;
  42. void* iowatcher;
  43. // custom_events
  44. int sockpair[2];
  45. event_queue custom_events;
  46. hmutex_t custom_events_mutex;
  47. };
  48. uint64_t hloop_next_event_id();
  49. struct hidle_s {
  50. HEVENT_FIELDS
  51. uint32_t repeat;
  52. //private:
  53. struct list_node node;
  54. };
  55. #define HTIMER_FIELDS \
  56. HEVENT_FIELDS \
  57. uint32_t repeat; \
  58. uint64_t next_timeout; \
  59. struct heap_node node;
  60. struct htimer_s {
  61. HTIMER_FIELDS
  62. };
  63. struct htimeout_s {
  64. HTIMER_FIELDS
  65. uint32_t timeout; \
  66. };
  67. struct hperiod_s {
  68. HTIMER_FIELDS
  69. int8_t minute;
  70. int8_t hour;
  71. int8_t day;
  72. int8_t week;
  73. int8_t month;
  74. };
  75. QUEUE_DECL(offset_buf_t, write_queue);
  76. struct hio_s {
  77. HEVENT_FIELDS
  78. // flags
  79. unsigned ready :1;
  80. unsigned closed :1;
  81. unsigned accept :1;
  82. unsigned connect :1;
  83. unsigned connectex :1; // for ConnectEx/DisconnectEx
  84. unsigned recv :1;
  85. unsigned send :1;
  86. unsigned recvfrom :1;
  87. unsigned sendto :1;
  88. unsigned close :1;
  89. unsigned alloced_readbuf :1;
  90. // public:
  91. uint32_t id; // fd cannot be used as unique identifier, so we provide an id
  92. int fd;
  93. hio_type_e io_type;
  94. int error;
  95. int events;
  96. int revents;
  97. struct sockaddr* localaddr;
  98. struct sockaddr* peeraddr;
  99. offset_buf_t readbuf; // for hread
  100. struct write_queue write_queue; // for hwrite
  101. hrecursive_mutex_t write_mutex; // lock write and write_queue
  102. // callbacks
  103. hread_cb read_cb;
  104. hwrite_cb write_cb;
  105. hclose_cb close_cb;
  106. haccept_cb accept_cb;
  107. hconnect_cb connect_cb;
  108. // timers
  109. int connect_timeout; // ms
  110. htimer_t* connect_timer;
  111. int close_timeout; // ms
  112. htimer_t* close_timer;
  113. int keepalive_timeout; // ms
  114. htimer_t* keepalive_timer;
  115. int heartbeat_interval; // ms
  116. hio_send_heartbeat_fn heartbeat_fn;
  117. htimer_t* heartbeat_timer;
  118. // upstream
  119. struct hio_s* upstream_io;
  120. // unpack
  121. unpack_setting_t* unpack_setting;
  122. // private:
  123. int event_index[2]; // for poll,kqueue
  124. void* hovlp; // for iocp/overlapio
  125. void* ssl; // for SSL
  126. void* ctx;
  127. };
  128. /*
  129. * hio lifeline:
  130. * fd =>
  131. * hio_get => HV_ALLOC_SIZEOF(io) => hio_init =>
  132. * hio_ready => hio_add => hio_del => hio_done =>
  133. * hio_close => hclose_cb =>
  134. * hio_free => HV_FREE(io)
  135. */
  136. void hio_init(hio_t* io);
  137. void hio_ready(hio_t* io);
  138. void hio_done(hio_t* io);
  139. void hio_free(hio_t* io);
  140. uint32_t hio_next_id();
  141. void hio_del_connect_timer(hio_t* io);
  142. void hio_del_close_timer(hio_t* io);
  143. void hio_del_keepalive_timer(hio_t* io);
  144. void hio_del_heartbeat_timer(hio_t* io);
  145. bool hio_is_alloced_readbuf(hio_t* io);
  146. void hio_alloc_readbuf(hio_t* io, int len);
  147. void hio_free_readbuf(hio_t* io);
  148. #define EVENT_ENTRY(p) container_of(p, hevent_t, pending_node)
  149. #define IDLE_ENTRY(p) container_of(p, hidle_t, node)
  150. #define TIMER_ENTRY(p) container_of(p, htimer_t, node)
  151. #define EVENT_ACTIVE(ev) \
  152. if (!ev->active) {\
  153. ev->active = 1;\
  154. ev->loop->nactives++;\
  155. }\
  156. #define EVENT_INACTIVE(ev) \
  157. if (ev->active) {\
  158. ev->active = 0;\
  159. ev->loop->nactives--;\
  160. }\
  161. #define EVENT_PENDING(ev) \
  162. do {\
  163. if (!ev->pending) {\
  164. ev->pending = 1;\
  165. ev->loop->npendings++;\
  166. hevent_t** phead = &ev->loop->pendings[HEVENT_PRIORITY_INDEX(ev->priority)];\
  167. ev->pending_next = *phead;\
  168. *phead = (hevent_t*)ev;\
  169. }\
  170. } while(0)
  171. #define EVENT_ADD(loop, ev, cb) \
  172. do {\
  173. ev->loop = loop;\
  174. ev->event_id = hloop_next_event_id();\
  175. ev->cb = (hevent_cb)cb;\
  176. EVENT_ACTIVE(ev);\
  177. } while(0)
  178. #define EVENT_DEL(ev) \
  179. do {\
  180. EVENT_INACTIVE(ev);\
  181. if (!ev->pending) {\
  182. HV_FREE(ev);\
  183. }\
  184. } while(0)
  185. #define EVENT_RESET(ev) \
  186. do {\
  187. ev->destroy = 0;\
  188. EVENT_ACTIVE(ev);\
  189. ev->pending = 0;\
  190. } while(0)
  191. #endif // HV_EVENT_H_