|
| 1 | +/** |
| 2 | + * \file |
| 3 | + * \author Jakub Antonín Štigler <[email protected]> |
| 4 | + * \brief C++ wrapper around epoll (header file) |
| 5 | + * \date 2024 |
| 6 | + * |
| 7 | + * Copyright: (C) 2023 CESNET, z.s.p.o. |
| 8 | + * SPDX-License-Identifier: BSD-3-Clause |
| 9 | + */ |
| 10 | + |
| 11 | +#pragma once |
| 12 | + |
| 13 | +#include <sys/epoll.h> // epoll_event |
| 14 | + |
| 15 | +#include "UniqueFd.hpp" // UniqueFd |
| 16 | + |
| 17 | +namespace tcp_in { |
| 18 | + |
| 19 | +class Epoll { |
| 20 | +public: |
| 21 | + /** |
| 22 | + * @brief Craetes empty epoll. |
| 23 | + * @throws when fails to create epoll. |
| 24 | + */ |
| 25 | + Epoll(); |
| 26 | + |
| 27 | + /** |
| 28 | + * @brief Add file descriptor to the epoll. |
| 29 | + * @param fd File descriptor to add. |
| 30 | + * @param [in] data data asociated with the file descriptor, when null, descriptor is set as |
| 31 | + * data |
| 32 | + * @throws when fails to add to the epoll. |
| 33 | + */ |
| 34 | + void add(int fd, void *data); |
| 35 | + |
| 36 | + /** |
| 37 | + * @brief Waits for any of the file descriptors in the epoll to be active. |
| 38 | + * @param[out] ev store all active descriptors here |
| 39 | + * @param max_events max number of events to store in `events` |
| 40 | + * @param timeout maximum time to wait in milliseconds, -1 for infinite wait |
| 41 | + * @returns Number of events written to `events`, negative on error (errno is set) |
| 42 | + */ |
| 43 | + int wait(epoll_event *events, int max_events, int timeout = -1); |
| 44 | + |
| 45 | + /** |
| 46 | + * @brief Removes file descriptor from the epoll. |
| 47 | + * @param fd File descriptor to remove from the epoll. |
| 48 | + * @return true on success, otherwise false |
| 49 | + */ |
| 50 | + bool remove(int fd) noexcept { |
| 51 | + return epoll_ctl(m_fd.get(), EPOLL_CTL_DEL, fd, nullptr) == 0; |
| 52 | + } |
| 53 | + |
| 54 | +private: |
| 55 | + UniqueFd m_fd; |
| 56 | +}; |
| 57 | + |
| 58 | +} // namespace tcp_in |
0 commit comments