hevent.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #ifndef HV_EVENT_H_
  2. #define HV_EVENT_H_
  3. #include "hloop.h"
  4. #include "hbuf.h"
  5. #include "hatomic.h"
  6. #include "hmutex.h"
  7. #include "array.h"
  8. #include "list.h"
  9. #include "heap.h"
  10. #include "queue.h"
  11. #define HLOOP_READ_BUFSIZE 8192
  12. ARRAY_DECL(hio_t*, io_array);
  13. QUEUE_DECL(hevent_t, event_queue);
  14. struct hloop_s {
  15. uint32_t flags;
  16. hloop_status_e status;
  17. uint64_t start_ms; // ms
  18. uint64_t start_hrtime; // us
  19. uint64_t end_hrtime;
  20. uint64_t cur_hrtime;
  21. uint64_t loop_cnt;
  22. long pid;
  23. long tid;
  24. void* userdata;
  25. //private:
  26. // events
  27. hatomic_t event_counter;
  28. uint32_t nactives;
  29. uint32_t npendings;
  30. // pendings: with priority as array.index
  31. hevent_t* pendings[HEVENT_PRIORITY_SIZE];
  32. // idles
  33. struct list_head idles;
  34. uint32_t nidles;
  35. // timers
  36. struct heap timers;
  37. uint32_t ntimers;
  38. // ios: with fd as array.index
  39. struct io_array ios;
  40. uint32_t nios;
  41. // one loop per thread, so one readbuf per loop is OK.
  42. hbuf_t readbuf;
  43. void* iowatcher;
  44. // custom_events
  45. int sockpair[2];
  46. event_queue custom_events;
  47. hmutex_t custom_events_mutex;
  48. };
  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 connected :1;
  81. unsigned closed :1;
  82. unsigned accept :1;
  83. unsigned connect :1;
  84. unsigned connectex :1; // for ConnectEx/DisconnectEx
  85. unsigned recv :1;
  86. unsigned send :1;
  87. unsigned recvfrom :1;
  88. unsigned sendto :1;
  89. unsigned close :1;
  90. // public:
  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. // 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. // private:
  117. int event_index[2]; // for poll,kqueue
  118. void* hovlp; // for iocp/overlapio
  119. void* ssl; // for SSL
  120. };
  121. /*
  122. * hio lifeline:
  123. * fd =>
  124. * hio_get => HV_ALLOC_SIZEOF(io) => hio_init =>
  125. * hio_ready => hio_add => hio_del => hio_done =>
  126. * hio_close => hclose_cb =>
  127. * hio_free => HV_FREE(io)
  128. */
  129. void hio_init(hio_t* io);
  130. void hio_ready(hio_t* io);
  131. void hio_done(hio_t* io);
  132. void hio_free(hio_t* io);
  133. #define EVENT_ENTRY(p) container_of(p, hevent_t, pending_node)
  134. #define IDLE_ENTRY(p) container_of(p, hidle_t, node)
  135. #define TIMER_ENTRY(p) container_of(p, htimer_t, node)
  136. #define EVENT_ACTIVE(ev) \
  137. if (!ev->active) {\
  138. ev->active = 1;\
  139. ev->loop->nactives++;\
  140. }\
  141. #define EVENT_INACTIVE(ev) \
  142. if (ev->active) {\
  143. ev->active = 0;\
  144. ev->loop->nactives--;\
  145. }\
  146. #define EVENT_PENDING(ev) \
  147. do {\
  148. if (!ev->pending) {\
  149. ev->pending = 1;\
  150. ev->loop->npendings++;\
  151. hevent_t** phead = &ev->loop->pendings[HEVENT_PRIORITY_INDEX(ev->priority)];\
  152. ev->pending_next = *phead;\
  153. *phead = (hevent_t*)ev;\
  154. }\
  155. } while(0)
  156. #define EVENT_ADD(loop, ev, cb) \
  157. do {\
  158. ev->loop = loop;\
  159. ev->event_id = ++loop->event_counter;\
  160. ev->cb = (hevent_cb)cb;\
  161. EVENT_ACTIVE(ev);\
  162. } while(0)
  163. #define EVENT_DEL(ev) \
  164. do {\
  165. EVENT_INACTIVE(ev);\
  166. if (!ev->pending) {\
  167. HV_FREE(ev);\
  168. }\
  169. } while(0)
  170. #define EVENT_RESET(ev) \
  171. do {\
  172. ev->destroy = 0;\
  173. EVENT_ACTIVE(ev);\
  174. ev->pending = 0;\
  175. } while(0)
  176. #endif // HV_EVENT_H_