1
0

hevent.c 2.5 KB

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