1
0

hevent.h 5.1 KB

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