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 "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. int fd;
  90. hio_type_e io_type;
  91. int error;
  92. int events;
  93. int revents;
  94. struct sockaddr* localaddr;
  95. struct sockaddr* peeraddr;
  96. hbuf_t readbuf; // for hread
  97. struct write_queue write_queue; // for hwrite
  98. hrecursive_mutex_t write_mutex; // lock write and write_queue
  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 = hloop_next_event_id();\
  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_