1
0

hevent.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. hssl_t hio_get_ssl(hio_t* io) {
  93. return io->ssl;
  94. }
  95. int hio_set_ssl(hio_t* io, hssl_t ssl) {
  96. io->io_type = HIO_TYPE_SSL;
  97. io->ssl = ssl;
  98. return 0;
  99. }
  100. void hio_set_readbuf(hio_t* io, void* buf, size_t len) {
  101. if (buf == NULL || len == 0) {
  102. hloop_t* loop = io->loop;
  103. if (loop && (loop->readbuf.base == NULL || loop->readbuf.len == 0)) {
  104. loop->readbuf.len = HLOOP_READ_BUFSIZE;
  105. HV_ALLOC(loop->readbuf.base, loop->readbuf.len);
  106. io->readbuf = loop->readbuf;
  107. }
  108. }
  109. else {
  110. io->readbuf.base = (char*)buf;
  111. io->readbuf.len = len;
  112. }
  113. }
  114. void hio_set_connect_timeout(hio_t* io, int timeout_ms) {
  115. io->connect_timeout = timeout_ms;
  116. }
  117. void hio_set_close_timeout(hio_t* io, int timeout_ms) {
  118. io->close_timeout = timeout_ms;
  119. }
  120. static void __keepalive_timeout_cb(htimer_t* timer) {
  121. hio_t* io = (hio_t*)timer->privdata;
  122. if (io) {
  123. char localaddrstr[SOCKADDR_STRLEN] = {0};
  124. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  125. hlogw("keepalive timeout [%s] <=> [%s]",
  126. SOCKADDR_STR(io->localaddr, localaddrstr),
  127. SOCKADDR_STR(io->peeraddr, peeraddrstr));
  128. io->error = ETIMEDOUT;
  129. hio_close(io);
  130. }
  131. }
  132. void hio_set_keepalive_timeout(hio_t* io, int timeout_ms) {
  133. if (io->keepalive_timer) {
  134. if (timeout_ms == 0) {
  135. htimer_del(io->keepalive_timer);
  136. io->keepalive_timer = NULL;
  137. } else {
  138. ((struct htimeout_s*)io->keepalive_timer)->timeout = timeout_ms;
  139. htimer_reset(io->keepalive_timer);
  140. }
  141. } else {
  142. io->keepalive_timer = htimer_add(io->loop, __keepalive_timeout_cb, timeout_ms, 1);
  143. io->keepalive_timer->privdata = io;
  144. }
  145. io->keepalive_timeout = timeout_ms;
  146. }
  147. static void __heartbeat_timer_cb(htimer_t* timer) {
  148. hio_t* io = (hio_t*)timer->privdata;
  149. if (io && io->heartbeat_fn) {
  150. io->heartbeat_fn(io);
  151. }
  152. }
  153. void hio_set_heartbeat(hio_t* io, int interval_ms, hio_send_heartbeat_fn fn) {
  154. if (io->heartbeat_timer) {
  155. if (interval_ms == 0) {
  156. htimer_del(io->heartbeat_timer);
  157. io->heartbeat_timer = NULL;
  158. } else {
  159. ((struct htimeout_s*)io->heartbeat_timer)->timeout = interval_ms;
  160. htimer_reset(io->heartbeat_timer);
  161. }
  162. } else {
  163. io->heartbeat_timer = htimer_add(io->loop, __heartbeat_timer_cb, interval_ms, INFINITE);
  164. io->heartbeat_timer->privdata = io;
  165. }
  166. io->heartbeat_interval = interval_ms;
  167. io->heartbeat_fn = fn;
  168. }