hevent.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. typedef enum {
  12. HLOOP_STATUS_STOP,
  13. HLOOP_STATUS_RUNNING,
  14. HLOOP_STATUS_PAUSE
  15. } hloop_status_e;
  16. ARRAY_DECL(hio_t*, io_array);
  17. QUEUE_DECL(hevent_t, event_queue);
  18. struct hloop_s {
  19. uint32_t flags;
  20. hloop_status_e status;
  21. uint64_t start_ms; // ms
  22. uint64_t start_hrtime; // us
  23. uint64_t end_hrtime;
  24. uint64_t cur_hrtime;
  25. uint64_t loop_cnt;
  26. long pid;
  27. long tid;
  28. void* userdata;
  29. //private:
  30. // events
  31. uint64_t event_counter;
  32. uint32_t nactives;
  33. uint32_t npendings;
  34. // pendings: with priority as array.index
  35. hevent_t* pendings[HEVENT_PRIORITY_SIZE];
  36. // idles
  37. struct list_head idles;
  38. uint32_t nidles;
  39. // timers
  40. struct heap timers;
  41. uint32_t ntimers;
  42. // ios: with fd as array.index
  43. struct io_array ios;
  44. uint32_t nios;
  45. // one loop per thread, so one readbuf per loop is OK.
  46. hbuf_t readbuf;
  47. void* iowatcher;
  48. // custom_events
  49. int sockpair[2];
  50. event_queue custom_events;
  51. hmutex_t custom_events_mutex;
  52. };
  53. struct hidle_s {
  54. HEVENT_FIELDS
  55. uint32_t repeat;
  56. //private:
  57. struct list_node node;
  58. };
  59. #define HTIMER_FIELDS \
  60. HEVENT_FIELDS \
  61. uint32_t repeat; \
  62. uint64_t next_timeout; \
  63. struct heap_node node;
  64. struct htimer_s {
  65. HTIMER_FIELDS
  66. };
  67. struct htimeout_s {
  68. HTIMER_FIELDS
  69. uint32_t timeout; \
  70. };
  71. struct hperiod_s {
  72. HTIMER_FIELDS
  73. int8_t minute;
  74. int8_t hour;
  75. int8_t day;
  76. int8_t week;
  77. int8_t month;
  78. };
  79. QUEUE_DECL(offset_buf_t, write_queue);
  80. struct hio_s {
  81. HEVENT_FIELDS
  82. // flags
  83. unsigned ready :1;
  84. unsigned closed :1;
  85. unsigned accept :1;
  86. unsigned connect :1;
  87. unsigned connectex :1; // for ConnectEx/DisconnectEx
  88. unsigned recv :1;
  89. unsigned send :1;
  90. unsigned recvfrom :1;
  91. unsigned sendto :1;
  92. unsigned close :1;
  93. // public:
  94. int fd;
  95. hio_type_e io_type;
  96. int error;
  97. int events;
  98. int revents;
  99. struct sockaddr* localaddr;
  100. struct sockaddr* peeraddr;
  101. hbuf_t readbuf; // for hread
  102. struct write_queue write_queue; // for hwrite
  103. // callbacks
  104. hread_cb read_cb;
  105. hwrite_cb write_cb;
  106. hclose_cb close_cb;
  107. haccept_cb accept_cb;
  108. hconnect_cb connect_cb;
  109. // timers
  110. int connect_timeout; // ms
  111. htimer_t* connect_timer;
  112. int close_timeout; // ms
  113. htimer_t* close_timer;
  114. int keepalive_timeout; // ms
  115. htimer_t* keepalive_timer;
  116. int heartbeat_interval; // ms
  117. hio_send_heartbeat_fn heartbeat_fn;
  118. htimer_t* heartbeat_timer;
  119. // private:
  120. int event_index[2]; // for poll,kqueue
  121. void* hovlp; // for iocp/overlapio
  122. void* ssl; // for SSL
  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. #define EVENT_ENTRY(p) container_of(p, hevent_t, pending_node)
  137. #define IDLE_ENTRY(p) container_of(p, hidle_t, node)
  138. #define TIMER_ENTRY(p) container_of(p, htimer_t, node)
  139. #define EVENT_ACTIVE(ev) \
  140. if (!ev->active) {\
  141. ev->active = 1;\
  142. ev->loop->nactives++;\
  143. }\
  144. #define EVENT_INACTIVE(ev) \
  145. if (ev->active) {\
  146. ev->active = 0;\
  147. ev->loop->nactives--;\
  148. }\
  149. #define EVENT_PENDING(ev) \
  150. do {\
  151. if (!ev->pending) {\
  152. ev->pending = 1;\
  153. ev->loop->npendings++;\
  154. hevent_t** phead = &ev->loop->pendings[HEVENT_PRIORITY_INDEX(ev->priority)];\
  155. ev->pending_next = *phead;\
  156. *phead = (hevent_t*)ev;\
  157. }\
  158. } while(0)
  159. #define EVENT_ADD(loop, ev, cb) \
  160. do {\
  161. ev->loop = loop;\
  162. ev->event_id = ++loop->event_counter;\
  163. ev->cb = (hevent_cb)cb;\
  164. EVENT_ACTIVE(ev);\
  165. } while(0)
  166. #define EVENT_DEL(ev) \
  167. do {\
  168. EVENT_INACTIVE(ev);\
  169. if (!ev->pending) {\
  170. HV_FREE(ev);\
  171. }\
  172. } while(0)
  173. #define EVENT_RESET(ev) \
  174. do {\
  175. ev->destroy = 0;\
  176. ev->active = 1;\
  177. ev->pending = 0;\
  178. } while(0)
  179. #endif // HV_EVENT_H_