hevent.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. // private:
  117. int event_index[2]; // for poll,kqueue
  118. void* hovlp; // for iocp/overlapio
  119. void* ssl; // for SSL
  120. void* ctx;
  121. };
  122. /*
  123. * hio lifeline:
  124. * fd =>
  125. * hio_get => HV_ALLOC_SIZEOF(io) => hio_init =>
  126. * hio_ready => hio_add => hio_del => hio_done =>
  127. * hio_close => hclose_cb =>
  128. * hio_free => HV_FREE(io)
  129. */
  130. void hio_init(hio_t* io);
  131. void hio_ready(hio_t* io);
  132. void hio_done(hio_t* io);
  133. void hio_free(hio_t* io);
  134. uint32_t hio_next_id();
  135. #define EVENT_ENTRY(p) container_of(p, hevent_t, pending_node)
  136. #define IDLE_ENTRY(p) container_of(p, hidle_t, node)
  137. #define TIMER_ENTRY(p) container_of(p, htimer_t, node)
  138. #define EVENT_ACTIVE(ev) \
  139. if (!ev->active) {\
  140. ev->active = 1;\
  141. ev->loop->nactives++;\
  142. }\
  143. #define EVENT_INACTIVE(ev) \
  144. if (ev->active) {\
  145. ev->active = 0;\
  146. ev->loop->nactives--;\
  147. }\
  148. #define EVENT_PENDING(ev) \
  149. do {\
  150. if (!ev->pending) {\
  151. ev->pending = 1;\
  152. ev->loop->npendings++;\
  153. hevent_t** phead = &ev->loop->pendings[HEVENT_PRIORITY_INDEX(ev->priority)];\
  154. ev->pending_next = *phead;\
  155. *phead = (hevent_t*)ev;\
  156. }\
  157. } while(0)
  158. #define EVENT_ADD(loop, ev, cb) \
  159. do {\
  160. ev->loop = loop;\
  161. ev->event_id = hloop_next_event_id();\
  162. ev->cb = (hevent_cb)cb;\
  163. EVENT_ACTIVE(ev);\
  164. } while(0)
  165. #define EVENT_DEL(ev) \
  166. do {\
  167. EVENT_INACTIVE(ev);\
  168. if (!ev->pending) {\
  169. HV_FREE(ev);\
  170. }\
  171. } while(0)
  172. #define EVENT_RESET(ev) \
  173. do {\
  174. ev->destroy = 0;\
  175. EVENT_ACTIVE(ev);\
  176. ev->pending = 0;\
  177. } while(0)
  178. #endif // HV_EVENT_H_