#include "EpollInstance.hpp" #include #include #include #include #include "src/Exceptions/Exception.hpp" namespace Bloom { using Exceptions::Exception; EpollInstance::EpollInstance() { this->fileDescriptor = ::epoll_create(1); if (this->fileDescriptor < 0) { throw Exception( "Failed to create epoll instance - error number " + std::to_string(errno) + " returned." ); } } void EpollInstance::addEntry(int fileDescriptor, std::uint16_t eventMask) { struct ::epoll_event event = { .events = eventMask, .data = { .fd = fileDescriptor } }; if (::epoll_ctl(this->fileDescriptor.value(), EPOLL_CTL_ADD, fileDescriptor, &event) != 0) { throw Exception( "Failed to add entry to epoll instance - error number " + std::to_string(errno) + " returned." ); } } void EpollInstance::removeEntry(int fileDescriptor) { if (::epoll_ctl(this->fileDescriptor.value(), EPOLL_CTL_DEL, fileDescriptor, NULL) != 0) { throw Exception( "Failed to remove entry from epoll instance - error number " + std::to_string(errno) + " returned." ); } } std::optional EpollInstance::waitForEvent(std::optional timeout) const { std::array events = {}; const auto eventCount = ::epoll_wait( this->fileDescriptor.value(), events.data(), 1, timeout.has_value() ? static_cast(timeout->count()) : -1 ); if (eventCount < 1) { return std::nullopt; } return static_cast(events.at(0).data.fd); } EpollInstance::EpollInstance(EpollInstance&& other) noexcept : fileDescriptor(other.fileDescriptor) { other.fileDescriptor = std::nullopt; } EpollInstance::~EpollInstance() noexcept { if (this->fileDescriptor.value_or(-1) >= 0) { ::close(this->fileDescriptor.value()); } } }