hevent.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include "hevent.h"
  2. #include "hsocket.h"
  3. #include "hatomic.h"
  4. #include "hlog.h"
  5. uint64_t hloop_next_event_id() {
  6. static hatomic_t s_id = HATOMIC_VAR_INIT(0);
  7. return ++s_id;
  8. }
  9. uint32_t hio_next_id() {
  10. static hatomic_t s_id = HATOMIC_VAR_INIT(0);
  11. return ++s_id;
  12. }
  13. uint32_t hio_id (hio_t* io) {
  14. return io->id;
  15. }
  16. int hio_fd(hio_t* io) {
  17. return io->fd;
  18. }
  19. hio_type_e hio_type(hio_t* io) {
  20. return io->io_type;
  21. }
  22. int hio_error(hio_t* io) {
  23. return io->error;
  24. }
  25. int hio_events(hio_t* io) {
  26. return io->events;
  27. }
  28. int hio_revents(hio_t* io) {
  29. return io->revents;
  30. }
  31. struct sockaddr* hio_localaddr(hio_t* io) {
  32. return io->localaddr;
  33. }
  34. struct sockaddr* hio_peeraddr(hio_t* io) {
  35. return io->peeraddr;
  36. }
  37. void hio_set_context(hio_t* io, void* ctx) {
  38. io->ctx = ctx;
  39. }
  40. void* hio_context(hio_t* io) {
  41. return io->ctx;
  42. }
  43. haccept_cb hio_getcb_accept(hio_t* io) {
  44. return io->accept_cb;
  45. }
  46. hconnect_cb hio_getcb_connect(hio_t* io) {
  47. return io->connect_cb;
  48. }
  49. hread_cb hio_getcb_read(hio_t* io) {
  50. return io->read_cb;
  51. }
  52. hwrite_cb hio_getcb_write(hio_t* io) {
  53. return io->write_cb;
  54. }
  55. hclose_cb hio_getcb_close(hio_t* io) {
  56. return io->close_cb;
  57. }
  58. void hio_setcb_accept (hio_t* io, haccept_cb accept_cb) {
  59. io->accept_cb = accept_cb;
  60. }
  61. void hio_setcb_connect (hio_t* io, hconnect_cb connect_cb) {
  62. io->connect_cb = connect_cb;
  63. }
  64. void hio_setcb_read (hio_t* io, hread_cb read_cb) {
  65. io->read_cb = read_cb;
  66. }
  67. void hio_setcb_write (hio_t* io, hwrite_cb write_cb) {
  68. io->write_cb = write_cb;
  69. }
  70. void hio_setcb_close (hio_t* io, hclose_cb close_cb) {
  71. io->close_cb = close_cb;
  72. }
  73. void hio_set_type(hio_t* io, hio_type_e type) {
  74. io->io_type = type;
  75. }
  76. void hio_set_localaddr(hio_t* io, struct sockaddr* addr, int addrlen) {
  77. if (io->localaddr == NULL) {
  78. HV_ALLOC(io->localaddr, sizeof(sockaddr_u));
  79. }
  80. memcpy(io->localaddr, addr, addrlen);
  81. }
  82. void hio_set_peeraddr (hio_t* io, struct sockaddr* addr, int addrlen) {
  83. if (io->peeraddr == NULL) {
  84. HV_ALLOC(io->peeraddr, sizeof(sockaddr_u));
  85. }
  86. memcpy(io->peeraddr, addr, addrlen);
  87. }
  88. int hio_enable_ssl(hio_t* io) {
  89. io->io_type = HIO_TYPE_SSL;
  90. return 0;
  91. }
  92. void hio_set_readbuf(hio_t* io, void* buf, size_t len) {
  93. if (buf == NULL || len == 0) {
  94. hloop_t* loop = io->loop;
  95. if (loop && (loop->readbuf.base == NULL || loop->readbuf.len == 0)) {
  96. loop->readbuf.len = HLOOP_READ_BUFSIZE;
  97. HV_ALLOC(loop->readbuf.base, loop->readbuf.len);
  98. io->readbuf = loop->readbuf;
  99. }
  100. }
  101. else {
  102. io->readbuf.base = (char*)buf;
  103. io->readbuf.len = len;
  104. }
  105. }
  106. void hio_set_connect_timeout(hio_t* io, int timeout_ms) {
  107. io->connect_timeout = timeout_ms;
  108. }
  109. void hio_set_close_timeout(hio_t* io, int timeout_ms) {
  110. io->close_timeout = timeout_ms;
  111. }
  112. static void __keepalive_timeout_cb(htimer_t* timer) {
  113. hio_t* io = (hio_t*)timer->privdata;
  114. if (io) {
  115. char localaddrstr[SOCKADDR_STRLEN] = {0};
  116. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  117. hlogw("keepalive timeout [%s] <=> [%s]",
  118. SOCKADDR_STR(io->localaddr, localaddrstr),
  119. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  120. io->error = ETIMEDOUT;
  121. hio_close(io);
  122. }
  123. }
  124. void hio_set_keepalive_timeout(hio_t* io, int timeout_ms) {
  125. if (io->keepalive_timer) {
  126. if (timeout_ms == 0) {
  127. htimer_del(io->keepalive_timer);
  128. io->keepalive_timer = NULL;
  129. } else {
  130. ((struct htimeout_s*)io->keepalive_timer)->timeout = timeout_ms;
  131. htimer_reset(io->keepalive_timer);
  132. }
  133. } else {
  134. io->keepalive_timer = htimer_add(io->loop, __keepalive_timeout_cb, timeout_ms, 1);
  135. io->keepalive_timer->privdata = io;
  136. }
  137. io->keepalive_timeout = timeout_ms;
  138. }
  139. static void __heartbeat_timer_cb(htimer_t* timer) {
  140. hio_t* io = (hio_t*)timer->privdata;
  141. if (io && io->heartbeat_fn) {
  142. io->heartbeat_fn(io);
  143. }
  144. }
  145. void hio_set_heartbeat(hio_t* io, int interval_ms, hio_send_heartbeat_fn fn) {
  146. if (io->heartbeat_timer) {
  147. if (interval_ms == 0) {
  148. htimer_del(io->heartbeat_timer);
  149. io->heartbeat_timer = NULL;
  150. } else {
  151. ((struct htimeout_s*)io->heartbeat_fn)->timeout = interval_ms;
  152. htimer_reset(io->keepalive_timer);
  153. }
  154. } else {
  155. io->heartbeat_timer = htimer_add(io->loop, __heartbeat_timer_cb, interval_ms, INFINITE);
  156. io->heartbeat_timer->privdata = io;
  157. }
  158. io->heartbeat_interval = interval_ms;
  159. io->heartbeat_fn = fn;
  160. }