1
0

hevent.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #ifndef HV_EVENT_H_
  2. #define HV_EVENT_H_
  3. #include "array.h"
  4. #include "list.h"
  5. #include "heap.h"
  6. #include "queue.h"
  7. #include "hloop.h"
  8. #include "hbuf.h"
  9. typedef enum {
  10. HLOOP_STATUS_STOP,
  11. HLOOP_STATUS_RUNNING,
  12. HLOOP_STATUS_PAUSE
  13. } hloop_status_e;
  14. ARRAY_DECL(hio_t*, io_array);
  15. struct hloop_s {
  16. uint32_t flags;
  17. hloop_status_e status;
  18. uint64_t start_ms; // ms
  19. uint64_t start_hrtime; // us
  20. uint64_t end_hrtime;
  21. uint64_t cur_hrtime;
  22. uint64_t loop_cnt;
  23. void* userdata;
  24. //private:
  25. // events
  26. uint64_t event_counter;
  27. uint32_t nactives;
  28. uint32_t npendings;
  29. // pendings: with priority as array.index
  30. hevent_t* pendings[HEVENT_PRIORITY_SIZE];
  31. // idles
  32. struct list_head idles;
  33. uint32_t nidles;
  34. // timers
  35. struct heap timers;
  36. uint32_t ntimers;
  37. // ios: with fd as array.index
  38. struct io_array ios;
  39. uint32_t nios;
  40. void* iowatcher;
  41. };
  42. struct hidle_s {
  43. HEVENT_FIELDS
  44. uint32_t repeat;
  45. //private:
  46. struct list_node node;
  47. };
  48. #define HTIMER_FIELDS \
  49. HEVENT_FIELDS \
  50. uint32_t repeat; \
  51. uint64_t next_timeout; \
  52. struct heap_node node;
  53. struct htimer_s {
  54. HTIMER_FIELDS
  55. };
  56. struct htimeout_s {
  57. HTIMER_FIELDS
  58. uint32_t timeout; \
  59. };
  60. struct hperiod_s {
  61. HTIMER_FIELDS
  62. int8_t minute;
  63. int8_t hour;
  64. int8_t day;
  65. int8_t week;
  66. int8_t month;
  67. };
  68. QUEUE_DECL(offset_buf_t, write_queue);
  69. struct hio_s {
  70. HEVENT_FIELDS
  71. unsigned ready :1;
  72. unsigned closed :1;
  73. unsigned accept :1;
  74. unsigned connect :1;
  75. unsigned connectex :1; // for ConnectEx/DisconnectEx
  76. unsigned recv :1;
  77. unsigned send :1;
  78. unsigned recvfrom :1;
  79. unsigned sendto :1;
  80. int fd;
  81. hio_type_e io_type;
  82. int error;
  83. int events;
  84. int revents;
  85. struct sockaddr* localaddr;
  86. struct sockaddr* peeraddr;
  87. hbuf_t readbuf; // for hread
  88. struct write_queue write_queue; // for hwrite
  89. // callbacks
  90. hread_cb read_cb;
  91. hwrite_cb write_cb;
  92. hclose_cb close_cb;
  93. haccept_cb accept_cb;
  94. hconnect_cb connect_cb;
  95. //private:
  96. int event_index[2]; // for poll,kqueue
  97. void* hovlp; // for iocp/overlapio
  98. void* ssl; // for SSL
  99. };
  100. #define EVENT_ENTRY(p) container_of(p, hevent_t, pending_node)
  101. #define IDLE_ENTRY(p) container_of(p, hidle_t, node)
  102. #define TIMER_ENTRY(p) container_of(p, htimer_t, node)
  103. #define EVENT_ACTIVE(ev) \
  104. if (!ev->active) {\
  105. ev->active = 1;\
  106. ev->loop->nactives++;\
  107. }\
  108. #define EVENT_INACTIVE(ev) \
  109. if (ev->active) {\
  110. ev->active = 0;\
  111. ev->loop->nactives--;\
  112. }\
  113. #define EVENT_PENDING(ev) \
  114. do {\
  115. if (!ev->pending) {\
  116. ev->pending = 1;\
  117. ev->loop->npendings++;\
  118. hevent_t** phead = &ev->loop->pendings[HEVENT_PRIORITY_INDEX(ev->priority)];\
  119. ev->pending_next = *phead;\
  120. *phead = (hevent_t*)ev;\
  121. }\
  122. } while(0)
  123. #define EVENT_ADD(loop, ev, cb) \
  124. do {\
  125. ev->loop = loop;\
  126. ev->event_id = ++loop->event_counter;\
  127. ev->cb = (hevent_cb)cb;\
  128. EVENT_ACTIVE(ev);\
  129. } while(0)
  130. #define EVENT_DEL(ev) \
  131. do {\
  132. EVENT_INACTIVE(ev);\
  133. if (!ev->pending) {\
  134. SAFE_FREE(ev);\
  135. }\
  136. } while(0)
  137. #define EVENT_RESET(ev) \
  138. do {\
  139. ev->destroy = 0;\
  140. ev->active = 1;\
  141. ev->pending = 0;\
  142. } while(0)
  143. #endif // HV_EVENT_H_