diff --git a/backend/Makefile b/backend/Makefile index 91efa4d..609f9ce 100644 --- a/backend/Makefile +++ b/backend/Makefile @@ -37,7 +37,10 @@ build_dir = build obj_dir = $(build_dir)/obj sources = \ - src/main.cpp + src/main.cpp \ + src/tcp.cpp \ + src/http.cpp + target=$(build_dir)/backend diff --git a/backend/src/tcp.cpp b/backend/src/tcp.cpp index beb01f3..1000ecb 100644 --- a/backend/src/tcp.cpp +++ b/backend/src/tcp.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -61,14 +62,20 @@ auto TcpListener::bind(const std::string& host, uint16_t port) return std::unexpected(errno_shim("could not listen")); } - return TcpListener(socket_fd, address); + auto epoll_fd = ::epoll_create(0); + + auto events_accepted = epoll_event { .events = EPOLLIN, .data = { } }; + if (::epoll_ctl(epoll_fd, EPOLL_CTL_ADD, socket_fd, &events_accepted) < 0) { + return std::unexpected(errno_shim("could not connect to epoll")); + } + + return TcpListener(socket_fd, epoll_fd, address); } auto TcpListener::accept() -> Result { socklen_t size = sizeof(address); - - int socket = ::accept(this->fd, (struct sockaddr*)&address, &size); + int socket = ::accept(this->listener_fd, (struct sockaddr*)&address, &size); if (socket < 0) { return std::unexpected(errno_shim("could not accept")); } diff --git a/backend/src/tcp.hpp b/backend/src/tcp.hpp index b6e2b78..7252ab2 100644 --- a/backend/src/tcp.hpp +++ b/backend/src/tcp.hpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace mst { template using Result = std::expected; @@ -29,10 +30,13 @@ public: auto accept() -> Result; private: - TcpListener(int fd, sockaddr_in address) - : fd(fd) + TcpListener(int listener_fd, int epoll_fd, sockaddr_in address) + : listener_fd(listener_fd) + , epoll_fd(epoll_fd) , address(address) { }; - int fd; + int listener_fd; + int epoll_fd; sockaddr_in address; + std::vector epoll_fds; }; }