hevent.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "hevent.h"
  2. #include "hsocket.h"
  3. #include "hatomic.h"
  4. uint64_t hloop_next_event_id() {
  5. static hatomic_t s_id = HATOMIC_VAR_INIT(0);
  6. return ++s_id;
  7. }
  8. uint32_t hio_next_id() {
  9. static hatomic_t s_id = HATOMIC_VAR_INIT(0);
  10. return ++s_id;
  11. }
  12. uint32_t hio_id (hio_t* io) {
  13. return io->id;
  14. }
  15. int hio_fd(hio_t* io) {
  16. return io->fd;
  17. }
  18. hio_type_e hio_type(hio_t* io) {
  19. return io->io_type;
  20. }
  21. int hio_error(hio_t* io) {
  22. return io->error;
  23. }
  24. int hio_events(hio_t* io) {
  25. return io->events;
  26. }
  27. int hio_revents(hio_t* io) {
  28. return io->revents;
  29. }
  30. struct sockaddr* hio_localaddr(hio_t* io) {
  31. return io->localaddr;
  32. }
  33. struct sockaddr* hio_peeraddr(hio_t* io) {
  34. return io->peeraddr;
  35. }
  36. void hio_setcb_accept (hio_t* io, haccept_cb accept_cb) {
  37. io->accept_cb = accept_cb;
  38. }
  39. void hio_setcb_connect (hio_t* io, hconnect_cb connect_cb) {
  40. io->connect_cb = connect_cb;
  41. }
  42. void hio_setcb_read (hio_t* io, hread_cb read_cb) {
  43. io->read_cb = read_cb;
  44. }
  45. void hio_setcb_write (hio_t* io, hwrite_cb write_cb) {
  46. io->write_cb = write_cb;
  47. }
  48. void hio_setcb_close (hio_t* io, hclose_cb close_cb) {
  49. io->close_cb = close_cb;
  50. }
  51. void hio_set_type(hio_t* io, hio_type_e type) {
  52. io->io_type = type;
  53. }
  54. void hio_set_localaddr(hio_t* io, struct sockaddr* addr, int addrlen) {
  55. if (io->localaddr == NULL) {
  56. HV_ALLOC(io->localaddr, sizeof(sockaddr_u));
  57. }
  58. memcpy(io->localaddr, addr, addrlen);
  59. }
  60. void hio_set_peeraddr (hio_t* io, struct sockaddr* addr, int addrlen) {
  61. if (io->peeraddr == NULL) {
  62. HV_ALLOC(io->peeraddr, sizeof(sockaddr_u));
  63. }
  64. memcpy(io->peeraddr, addr, addrlen);
  65. }
  66. int hio_enable_ssl(hio_t* io) {
  67. io->io_type = HIO_TYPE_SSL;
  68. return 0;
  69. }
  70. void hio_set_readbuf(hio_t* io, void* buf, size_t len) {
  71. if (buf == NULL || len == 0) {
  72. hloop_t* loop = io->loop;
  73. if (loop && (loop->readbuf.base == NULL || loop->readbuf.len == 0)) {
  74. loop->readbuf.len = HLOOP_READ_BUFSIZE;
  75. HV_ALLOC(loop->readbuf.base, loop->readbuf.len);
  76. io->readbuf = loop->readbuf;
  77. }
  78. }
  79. else {
  80. io->readbuf.base = (char*)buf;
  81. io->readbuf.len = len;
  82. }
  83. }
  84. void hio_set_connect_timeout(hio_t* io, int timeout_ms) {
  85. io->connect_timeout = timeout_ms;
  86. }
  87. void hio_set_close_timeout(hio_t* io, int timeout_ms) {
  88. io->close_timeout = timeout_ms;
  89. }
  90. void hio_set_keepalive_timeout(hio_t* io, int timeout_ms) {
  91. io->keepalive_timeout = timeout_ms;
  92. }
  93. void hio_set_heartbeat(hio_t* io, int interval_ms, hio_send_heartbeat_fn fn) {
  94. io->heartbeat_interval = interval_ms;
  95. io->heartbeat_fn = fn;
  96. }