hevent.c 2.2 KB

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