hevent.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. // public:
  90. uint32_t id; // fd cannot be used as unique identifier, so we provide an id
  91. int fd;
  92. hio_type_e io_type;
  93. int error;
  94. int events;
  95. int revents;
  96. struct sockaddr* localaddr;
  97. struct sockaddr* peeraddr;
  98. hbuf_t readbuf; // for hread
  99. struct write_queue write_queue; // for hwrite
  100. hrecursive_mutex_t write_mutex; // lock write and write_queue
  101. // callbacks
  102. hread_cb read_cb;
  103. hwrite_cb write_cb;
  104. hclose_cb close_cb;
  105. haccept_cb accept_cb;
  106. hconnect_cb connect_cb;
  107. // timers
  108. int connect_timeout; // ms
  109. htimer_t* connect_timer;
  110. int close_timeout; // ms
  111. htimer_t* close_timer;
  112. int keepalive_timeout; // ms
  113. htimer_t* keepalive_timer;
  114. int heartbeat_interval; // ms
  115. hio_send_heartbeat_fn heartbeat_fn;
  116. htimer_t* heartbeat_timer;
  117. // upstream
  118. struct hio_s* upstream_io;
  119. // private:
  120. int event_index[2]; // for poll,kqueue
  121. void* hovlp; // for iocp/overlapio
  122. void* ssl; // for SSL
  123. void* ctx;
  124. };
  125. /*
  126. * hio lifeline:
  127. * fd =>
  128. * hio_get => HV_ALLOC_SIZEOF(io) => hio_init =>
  129. * hio_ready => hio_add => hio_del => hio_done =>
  130. * hio_close => hclose_cb =>
  131. * hio_free => HV_FREE(io)
  132. */
  133. void hio_init(hio_t* io);
  134. void hio_ready(hio_t* io);
  135. void hio_done(hio_t* io);
  136. void hio_free(hio_t* io);
  137. uint32_t hio_next_id();
  138. #define EVENT_ENTRY(p) container_of(p, hevent_t, pending_node)
  139. #define IDLE_ENTRY(p) container_of(p, hidle_t, node)
  140. #define TIMER_ENTRY(p) container_of(p, htimer_t, node)
  141. #define EVENT_ACTIVE(ev) \
  142. if (!ev->active) {\
  143. ev->active = 1;\
  144. ev->loop->nactives++;\
  145. }\
  146. #define EVENT_INACTIVE(ev) \
  147. if (ev->active) {\
  148. ev->active = 0;\
  149. ev->loop->nactives--;\
  150. }\
  151. #define EVENT_PENDING(ev) \
  152. do {\
  153. if (!ev->pending) {\
  154. ev->pending = 1;\
  155. ev->loop->npendings++;\
  156. hevent_t** phead = &ev->loop->pendings[HEVENT_PRIORITY_INDEX(ev->priority)];\
  157. ev->pending_next = *phead;\
  158. *phead = (hevent_t*)ev;\
  159. }\
  160. } while(0)
  161. #define EVENT_ADD(loop, ev, cb) \
  162. do {\
  163. ev->loop = loop;\
  164. ev->event_id = hloop_next_event_id();\
  165. ev->cb = (hevent_cb)cb;\
  166. EVENT_ACTIVE(ev);\
  167. } while(0)
  168. #define EVENT_DEL(ev) \
  169. do {\
  170. EVENT_INACTIVE(ev);\
  171. if (!ev->pending) {\
  172. HV_FREE(ev);\
  173. }\
  174. } while(0)
  175. #define EVENT_RESET(ev) \
  176. do {\
  177. ev->destroy = 0;\
  178. EVENT_ACTIVE(ev);\
  179. ev->pending = 0;\
  180. } while(0)
  181. #endif // HV_EVENT_H_