backend: begin work on epoll

This commit is contained in:
Theis Pieter Hollebeek 2026-03-23 15:24:56 +01:00
parent 0ad5b4fbfc
commit c7adfaf96a
3 changed files with 21 additions and 7 deletions

View File

@ -37,7 +37,10 @@ build_dir = build
obj_dir = $(build_dir)/obj obj_dir = $(build_dir)/obj
sources = \ sources = \
src/main.cpp src/main.cpp \
src/tcp.cpp \
src/http.cpp
target=$(build_dir)/backend target=$(build_dir)/backend

View File

@ -10,6 +10,7 @@
#include <string.h> #include <string.h>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <sys/epoll.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <unistd.h> #include <unistd.h>
@ -61,14 +62,20 @@ auto TcpListener::bind(const std::string& host, uint16_t port)
return std::unexpected(errno_shim("could not listen")); 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<TcpConnection> auto TcpListener::accept() -> Result<TcpConnection>
{ {
socklen_t size = sizeof(address); socklen_t size = sizeof(address);
int socket = ::accept(this->listener_fd, (struct sockaddr*)&address, &size);
int socket = ::accept(this->fd, (struct sockaddr*)&address, &size);
if (socket < 0) { if (socket < 0) {
return std::unexpected(errno_shim("could not accept")); return std::unexpected(errno_shim("could not accept"));
} }

View File

@ -4,6 +4,7 @@
#include <expected> #include <expected>
#include <netinet/in.h> #include <netinet/in.h>
#include <string> #include <string>
#include <vector>
namespace mst { namespace mst {
template <typename T> using Result = std::expected<T, std::string>; template <typename T> using Result = std::expected<T, std::string>;
@ -29,10 +30,13 @@ public:
auto accept() -> Result<TcpConnection>; auto accept() -> Result<TcpConnection>;
private: private:
TcpListener(int fd, sockaddr_in address) TcpListener(int listener_fd, int epoll_fd, sockaddr_in address)
: fd(fd) : listener_fd(listener_fd)
, epoll_fd(epoll_fd)
, address(address) { }; , address(address) { };
int fd; int listener_fd;
int epoll_fd;
sockaddr_in address; sockaddr_in address;
std::vector<int> epoll_fds;
}; };
} }