net.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include <stdio.h>
  2. #include "net.h"
  3. #define fatal(fmt, ...) do { \
  4. fprintf(stderr, "%s:%d " fmt, __FILE__, __LINE__ __VA_OPT__(,) __VA_ARGS__); \
  5. exit(1); \
  6. } while(0);
  7. void add_epoll_watch(int epollfd, int fd, void* data, int events) {
  8. struct epoll_event ee = {0};
  9. ee.events = events;
  10. ee.data.ptr = data;
  11. int ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ee);
  12. if(ret < 0) {
  13. printf("epoll error: %s\n", strerror(errno));
  14. }
  15. }
  16. server_t* server_init(int epollfd, int port) {
  17. // int epollfd = epoll_create(16);
  18. server_t* srv = calloc(1, sizeof(*srv));
  19. srv->epollfd = epollfd;
  20. srv->listen_socket = socket(AF_INET, SOCK_STREAM, 0);
  21. fcntl(srv->listen_socket, F_SETFL, O_NONBLOCK);
  22. int on = 1;
  23. setsockopt(srv->listen_socket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  24. struct sockaddr_in bindaddr, peeraddr;
  25. bindaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  26. bindaddr.sin_family = AF_INET;
  27. bindaddr.sin_port = htons(port);
  28. if(bind(srv->listen_socket, (struct sockaddr*)&bindaddr, sizeof(bindaddr)) < 0) {
  29. fatal("Failed to bind socket");
  30. }
  31. listen(srv->listen_socket, SOMAXCONN);
  32. add_epoll_watch(epollfd, srv->listen_socket, srv->listen_socket, EPOLLIN);
  33. return srv;
  34. }
  35. void server_tick(server_t* srv, int wait) {
  36. struct epoll_event ee = {0};
  37. // printf("epoll waiting\n");
  38. int ret = epoll_wait(srv->epollfd, &ee, 1, wait);
  39. if(ret == -1) {
  40. if(errno == EINTR) return;
  41. fatal("epoll error\n");
  42. }
  43. if(ret == 0) return;
  44. // new connections
  45. if(ee.data.fd == srv->listen_socket) {
  46. connection_t* con = calloc(1, sizeof(*con));
  47. int addrlen;
  48. memset(&con->peeraddr, 0, sizeof(con->peeraddr)); // shutup, valgrind
  49. con->peerfd = accept(srv->listen_socket, (struct sockaddr*)&con->peeraddr, &addrlen);
  50. fcntl(con->peerfd, F_SETFL, O_NONBLOCK);
  51. if(con->peerfd < 0) {
  52. printf("socket error: %s\n", strerror(errno));
  53. exit(5);
  54. }
  55. con->srv = srv;
  56. srv->on_accept(srv, con);
  57. add_epoll_watch(srv->epollfd, con->peerfd, con, EPOLLIN);
  58. VEC_PUSH(&srv->cons, con);
  59. return;
  60. }
  61. if(ee.events & EPOLLIN) { // data to read
  62. // printf("got data\n\n");
  63. connection_t* con = ee.data.ptr;
  64. int bread = 0;
  65. do {
  66. if(*con->buf_remain == 0) {
  67. con->buffer_full(con);
  68. }
  69. bread = recv(con->peerfd, *con->buf, *con->buf_remain, 0);
  70. if(bread > 0) {
  71. *con->buf_remain -= bread;
  72. con->new_data = 1;
  73. }
  74. if(errno == EAGAIN) {
  75. break;
  76. }
  77. } while(bread > 0);
  78. if(con->new_data) {
  79. printf("got new data\n");
  80. con->new_data = 0;
  81. if(con->got_data) con->got_data(con);
  82. }
  83. }
  84. }
  85. void connection_close(connection_t* con) {
  86. // flush the write buffer
  87. ssize_t wb, bytes_written = 0;
  88. if(con->write_buf && con->wb_len) {
  89. do {
  90. wb = send(con->peerfd, con->write_buf + bytes_written, con->wb_len - bytes_written, 0);
  91. bytes_written += wb;
  92. } while(wb > 0);
  93. free(con->write_buf);
  94. }
  95. // make sure we don't get any more events from epoll
  96. struct epoll_event ee = {0};
  97. epoll_ctl(con->srv->epollfd, EPOLL_CTL_DEL, con->peerfd, &ee);
  98. close(con->peerfd);
  99. VEC_RM_VAL(&con->srv->cons, con);
  100. free(con);
  101. }
  102. void connection_write(connection_t* con, char* s, ssize_t len) {
  103. if(len < 0) len = strlen(s);
  104. if(len == 0) return;
  105. if(con->wb_len + len >= con->wb_alloc) {
  106. con->wb_alloc = nextPOT(con->wb_len + len + 1);
  107. con->write_buf = realloc(con->write_buf, con->wb_alloc);
  108. }
  109. memcpy(con->write_buf + con->wb_len, s, len);
  110. con->wb_len += len;
  111. }