hevent.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. void hio_setcb_accept (hio_t* io, haccept_cb accept_cb) {
  44. io->accept_cb = accept_cb;
  45. }
  46. void hio_setcb_connect (hio_t* io, hconnect_cb connect_cb) {
  47. io->connect_cb = connect_cb;
  48. }
  49. void hio_setcb_read (hio_t* io, hread_cb read_cb) {
  50. io->read_cb = read_cb;
  51. }
  52. void hio_setcb_write (hio_t* io, hwrite_cb write_cb) {
  53. io->write_cb = write_cb;
  54. }
  55. void hio_setcb_close (hio_t* io, hclose_cb close_cb) {
  56. io->close_cb = close_cb;
  57. }
  58. void hio_set_type(hio_t* io, hio_type_e type) {
  59. io->io_type = type;
  60. }
  61. void hio_set_localaddr(hio_t* io, struct sockaddr* addr, int addrlen) {
  62. if (io->localaddr == NULL) {
  63. HV_ALLOC(io->localaddr, sizeof(sockaddr_u));
  64. }
  65. memcpy(io->localaddr, addr, addrlen);
  66. }
  67. void hio_set_peeraddr (hio_t* io, struct sockaddr* addr, int addrlen) {
  68. if (io->peeraddr == NULL) {
  69. HV_ALLOC(io->peeraddr, sizeof(sockaddr_u));
  70. }
  71. memcpy(io->peeraddr, addr, addrlen);
  72. }
  73. int hio_enable_ssl(hio_t* io) {
  74. io->io_type = HIO_TYPE_SSL;
  75. return 0;
  76. }
  77. void hio_set_readbuf(hio_t* io, void* buf, size_t len) {
  78. if (buf == NULL || len == 0) {
  79. hloop_t* loop = io->loop;
  80. if (loop && (loop->readbuf.base == NULL || loop->readbuf.len == 0)) {
  81. loop->readbuf.len = HLOOP_READ_BUFSIZE;
  82. HV_ALLOC(loop->readbuf.base, loop->readbuf.len);
  83. io->readbuf = loop->readbuf;
  84. }
  85. }
  86. else {
  87. io->readbuf.base = (char*)buf;
  88. io->readbuf.len = len;
  89. }
  90. }
  91. void hio_set_connect_timeout(hio_t* io, int timeout_ms) {
  92. io->connect_timeout = timeout_ms;
  93. }
  94. void hio_set_close_timeout(hio_t* io, int timeout_ms) {
  95. io->close_timeout = timeout_ms;
  96. }
  97. static void __keepalive_timeout_cb(htimer_t* timer) {
  98. hio_t* io = (hio_t*)timer->privdata;
  99. if (io) {
  100. char localaddrstr[SOCKADDR_STRLEN] = {0};
  101. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  102. hlogw("keepalive timeout [%s] <=> [%s]",
  103. SOCKADDR_STR(io->localaddr, localaddrstr),
  104. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  105. io->error = ETIMEDOUT;
  106. hio_close(io);
  107. }
  108. }
  109. void hio_set_keepalive_timeout(hio_t* io, int timeout_ms) {
  110. if (io->keepalive_timer) {
  111. if (timeout_ms == 0) {
  112. htimer_del(io->keepalive_timer);
  113. io->keepalive_timer = NULL;
  114. } else {
  115. ((struct htimeout_s*)io->keepalive_timer)->timeout = timeout_ms;
  116. htimer_reset(io->keepalive_timer);
  117. }
  118. } else {
  119. io->keepalive_timer = htimer_add(io->loop, __keepalive_timeout_cb, timeout_ms, 1);
  120. io->keepalive_timer->privdata = io;
  121. }
  122. io->keepalive_timeout = timeout_ms;
  123. }
  124. static void __heartbeat_timer_cb(htimer_t* timer) {
  125. hio_t* io = (hio_t*)timer->privdata;
  126. if (io && io->heartbeat_fn) {
  127. io->heartbeat_fn(io);
  128. }
  129. }
  130. void hio_set_heartbeat(hio_t* io, int interval_ms, hio_send_heartbeat_fn fn) {
  131. if (io->heartbeat_timer) {
  132. if (interval_ms == 0) {
  133. htimer_del(io->heartbeat_timer);
  134. io->heartbeat_timer = NULL;
  135. } else {
  136. ((struct htimeout_s*)io->heartbeat_fn)->timeout = interval_ms;
  137. htimer_reset(io->keepalive_timer);
  138. }
  139. } else {
  140. io->heartbeat_timer = htimer_add(io->loop, __heartbeat_timer_cb, interval_ms, INFINITE);
  141. io->heartbeat_timer->privdata = io;
  142. }
  143. io->heartbeat_interval = interval_ms;
  144. io->heartbeat_fn = fn;
  145. }