net.h 995 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #ifndef __GWS__net_h__
  2. #define __GWS__net_h__
  3. #include <errno.h>
  4. #include <sys/epoll.h>
  5. #include <sys/resource.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. #include <netinet/in.h>
  11. #include "../common/sti/sti.h"
  12. struct server;
  13. typedef struct connection {
  14. struct server* srv;
  15. int peerfd;
  16. struct sockaddr_storage peeraddr;
  17. char** buf;
  18. size_t* buf_remain;
  19. int new_data;
  20. void (*buffer_full)(struct connection*);
  21. void (*got_data)(struct connection*);
  22. void* user_data;
  23. char* write_buf;
  24. size_t wb_len, wb_alloc;
  25. } connection_t;
  26. typedef struct server {
  27. int epollfd;
  28. int listen_socket;
  29. VEC(connection_t*) cons;
  30. void (*on_accept)(struct server*, connection_t*);
  31. void* user_data;
  32. } server_t;
  33. server_t* server_init(int epollfd, int port);
  34. void server_tick(server_t* srv, int wait);
  35. void connection_close(connection_t* con);
  36. void connection_write(connection_t* con, char* s, ssize_t len);
  37. #endif // __GWS__net_h__