hevent.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. void* userdata;
  27. //private:
  28. // events
  29. uint64_t event_counter;
  30. uint32_t nactives;
  31. uint32_t npendings;
  32. // pendings: with priority as array.index
  33. hevent_t* pendings[HEVENT_PRIORITY_SIZE];
  34. // idles
  35. struct list_head idles;
  36. uint32_t nidles;
  37. // timers
  38. struct heap timers;
  39. uint32_t ntimers;
  40. // ios: with fd as array.index
  41. struct io_array ios;
  42. uint32_t nios;
  43. // one loop per thread, so one readbuf per loop is OK.
  44. hbuf_t readbuf;
  45. void* iowatcher;
  46. // custom_events
  47. int sockpair[2];
  48. event_queue custom_events;
  49. hmutex_t custom_events_mutex;
  50. };
  51. struct hidle_s {
  52. HEVENT_FIELDS
  53. uint32_t repeat;
  54. //private:
  55. struct list_node node;
  56. };
  57. #define HTIMER_FIELDS \
  58. HEVENT_FIELDS \
  59. uint32_t repeat; \
  60. uint64_t next_timeout; \
  61. struct heap_node node;
  62. struct htimer_s {
  63. HTIMER_FIELDS
  64. };
  65. struct htimeout_s {
  66. HTIMER_FIELDS
  67. uint32_t timeout; \
  68. };
  69. struct hperiod_s {
  70. HTIMER_FIELDS
  71. int8_t minute;
  72. int8_t hour;
  73. int8_t day;
  74. int8_t week;
  75. int8_t month;
  76. };
  77. QUEUE_DECL(offset_buf_t, write_queue);
  78. struct hio_s {
  79. HEVENT_FIELDS
  80. // flags
  81. unsigned ready :1;
  82. unsigned closed :1;
  83. unsigned accept :1;
  84. unsigned connect :1;
  85. unsigned connectex :1; // for ConnectEx/DisconnectEx
  86. unsigned recv :1;
  87. unsigned send :1;
  88. unsigned recvfrom :1;
  89. unsigned sendto :1;
  90. unsigned close :1;
  91. // public:
  92. int fd;
  93. hio_type_e io_type;
  94. int error;
  95. int events;
  96. int revents;
  97. struct sockaddr* localaddr;
  98. struct sockaddr* peeraddr;
  99. hbuf_t readbuf; // for hread
  100. struct write_queue write_queue; // for hwrite
  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. // private:
  118. int event_index[2]; // for poll,kqueue
  119. void* hovlp; // for iocp/overlapio
  120. void* ssl; // for SSL
  121. };
  122. #define EVENT_ENTRY(p) container_of(p, hevent_t, pending_node)
  123. #define IDLE_ENTRY(p) container_of(p, hidle_t, node)
  124. #define TIMER_ENTRY(p) container_of(p, htimer_t, node)
  125. #define EVENT_ACTIVE(ev) \
  126. if (!ev->active) {\
  127. ev->active = 1;\
  128. ev->loop->nactives++;\
  129. }\
  130. #define EVENT_INACTIVE(ev) \
  131. if (ev->active) {\
  132. ev->active = 0;\
  133. ev->loop->nactives--;\
  134. }\
  135. #define EVENT_PENDING(ev) \
  136. do {\
  137. if (!ev->pending) {\
  138. ev->pending = 1;\
  139. ev->loop->npendings++;\
  140. hevent_t** phead = &ev->loop->pendings[HEVENT_PRIORITY_INDEX(ev->priority)];\
  141. ev->pending_next = *phead;\
  142. *phead = (hevent_t*)ev;\
  143. }\
  144. } while(0)
  145. #define EVENT_ADD(loop, ev, cb) \
  146. do {\
  147. ev->loop = loop;\
  148. ev->event_id = ++loop->event_counter;\
  149. ev->cb = (hevent_cb)cb;\
  150. EVENT_ACTIVE(ev);\
  151. } while(0)
  152. #define EVENT_DEL(ev) \
  153. do {\
  154. EVENT_INACTIVE(ev);\
  155. if (!ev->pending) {\
  156. HV_FREE(ev);\
  157. }\
  158. } while(0)
  159. #define EVENT_RESET(ev) \
  160. do {\
  161. ev->destroy = 0;\
  162. ev->active = 1;\
  163. ev->pending = 0;\
  164. } while(0)
  165. #endif // HV_EVENT_H_