Kea: poll_event_handler.cc Source File

| Kea 3.2.0-git

poll_event_handler.cc
Go to the documentation of this file.
1// Copyright (C) 2025 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
9#include <exceptions/exceptions.h>
10#include <util/poll_event_handler.h>
11
12#include <cstring>
13
14namespace isc {
15namespace util {
16
17PollEventHandler::PollEventHandler() : FDEventHandler(TYPE_POLL) {
18 clear();
19}
20
21 void PollEventHandler::add(int fd) {
22 if (fd < 0) {
23 isc_throw(BadValue, "invalid negative value for fd");
24 }
25 struct pollfd data;
26 memset(&data, 0, sizeof(data));
27 data.fd = fd;
28 // Add this socket to read events
29 data.events |= POLLIN;
30 data_.push_back(data);
31}
32
33 int PollEventHandler::waitEvent(uint32_t timeout_sec, uint32_t timeout_usec /* = 0 */,
34 bool use_timeout /* = true */) {
35 // Sanity check for microsecond timeout.
36 if (timeout_usec >= 1000000) {
37 isc_throw(BadValue, "fractional timeout must be shorter than"
38 " one million microseconds");
39 }
40 int timeout = -1;
41 if (use_timeout) {
42 timeout = timeout_sec * 1000 + timeout_usec / 1000;
43 }
44 map_.clear();
45 for (size_t i = 0; i < data_.size(); ++i) {
46 map_[data_[i].fd] = &data_[i];
47 }
48 return (poll(data_.data(), data_.size(), timeout));
49}
50
51 bool PollEventHandler::readReady(int fd) {
52 if (fd < 0) {
53 isc_throw(BadValue, "invalid negative value for fd");
54 }
55 if (map_.find(fd) == map_.end()) {
56 return (false);
57 }
58 return (map_[fd]->revents & (POLLIN | POLLHUP));
59}
60
61
62 bool PollEventHandler::hasError(int fd) {
63 if (fd < 0) {
64 isc_throw(BadValue, "invalid negative value for fd");
65 }
66 if (map_.find(fd) == map_.end()) {
67 return (false);
68 }
69 return (map_[fd]->revents & POLLERR);
70}
71
72 void PollEventHandler::clear() {
73 data_.clear();
74 map_.clear();
75}
76
77} // end of namespace isc::util
78} // end of namespace isc
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition exceptions/exceptions.h:132
isc::util::FDEventHandler::TYPE_POLL
@ TYPE_POLL
Definition fd_event_handler.h:23
isc::util::FDEventHandler::FDEventHandler
FDEventHandler(HandlerType type=TYPE_UNKNOWN)
Constructor.
Definition fd_event_handler.cc:14
isc::util::PollEventHandler::add
void add(int fd)
Add file descriptor to watch for events.
Definition poll_event_handler.cc:21
isc::util::PollEventHandler::hasError
virtual bool hasError(int fd)
Check if file descriptor has error.
Definition poll_event_handler.cc:62
isc::util::PollEventHandler::readReady
bool readReady(int fd)
Check if file descriptor is ready for read operation.
Definition poll_event_handler.cc:51
isc::util::PollEventHandler::waitEvent
int waitEvent(uint32_t timeout_sec, uint32_t timeout_usec=0, bool use_timeout=true)
Wait for events on registered file descriptors.
Definition poll_event_handler.cc:33
isc::util::PollEventHandler::PollEventHandler
PollEventHandler()
Constructor.
Definition poll_event_handler.cc:17
isc::util::PollEventHandler::clear
void clear()
Clear registered file descriptors.
Definition poll_event_handler.cc:72
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::data
Definition base_stamped_element.cc:12
isc::util
Definition time_utils.cc:43
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
poll_event_handler.h