hevent.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 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. 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. // callbacks
  100. hread_cb read_cb;
  101. hwrite_cb write_cb;
  102. hclose_cb close_cb;
  103. haccept_cb accept_cb;
  104. hconnect_cb connect_cb;
  105. // timers
  106. int connect_timeout; // ms
  107. htimer_t* connect_timer;
  108. int close_timeout; // ms
  109. htimer_t* close_timer;
  110. int keepalive_timeout; // ms
  111. htimer_t* keepalive_timer;
  112. int heartbeat_interval; // ms
  113. hio_send_heartbeat_fn heartbeat_fn;
  114. htimer_t* heartbeat_timer;
  115. // private:
  116. int event_index[2]; // for poll,kqueue
  117. void* hovlp; // for iocp/overlapio
  118. void* ssl; // for SSL
  119. };
  120. /*
  121. * hio lifeline:
  122. * fd =>
  123. * hio_get => HV_ALLOC_SIZEOF(io) => hio_init =>
  124. * hio_ready => hio_add => hio_del => hio_done =>
  125. * hio_close => hclose_cb =>
  126. * hio_free => HV_FREE(io)
  127. */
  128. void hio_init(hio_t* io);
  129. void hio_ready(hio_t* io);
  130. void hio_done(hio_t* io);
  131. void hio_free(hio_t* io);
  132. #define EVENT_ENTRY(p) container_of(p, hevent_t, pending_node)
  133. #define IDLE_ENTRY(p) container_of(p, hidle_t, node)
  134. #define TIMER_ENTRY(p) container_of(p, htimer_t, node)
  135. #define EVENT_ACTIVE(ev) \
  136. if (!ev->active) {\
  137. ev->active = 1;\
  138. ev->loop->nactives++;\
  139. }\
  140. #define EVENT_INACTIVE(ev) \
  141. if (ev->active) {\
  142. ev->active = 0;\
  143. ev->loop->nactives--;\
  144. }\
  145. #define EVENT_PENDING(ev) \
  146. do {\
  147. if (!ev->pending) {\
  148. ev->pending = 1;\
  149. ev->loop->npendings++;\
  150. hevent_t** phead = &ev->loop->pendings[HEVENT_PRIORITY_INDEX(ev->priority)];\
  151. ev->pending_next = *phead;\
  152. *phead = (hevent_t*)ev;\
  153. }\
  154. } while(0)
  155. #define EVENT_ADD(loop, ev, cb) \
  156. do {\
  157. ev->loop = loop;\
  158. ev->event_id = ++loop->event_counter;\
  159. ev->cb = (hevent_cb)cb;\
  160. EVENT_ACTIVE(ev);\
  161. } while(0)
  162. #define EVENT_DEL(ev) \
  163. do {\
  164. EVENT_INACTIVE(ev);\
  165. if (!ev->pending) {\
  166. HV_FREE(ev);\
  167. }\
  168. } while(0)
  169. #define EVENT_RESET(ev) \
  170. do {\
  171. ev->destroy = 0;\
  172. EVENT_ACTIVE(ev);\
  173. ev->pending = 0;\
  174. } while(0)
  175. #endif // HV_EVENT_H_