Kea 3.1.9
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
11
12#include <cstring>
13
14namespace isc {
15namespace util {
16
20
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
33int 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
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
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
73 data_.clear();
74 map_.clear();
75}
76
77} // end of namespace isc::util
78} // end of namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
FDEventHandler(HandlerType type=TYPE_UNKNOWN)
Constructor.
void add(int fd)
Add file descriptor to watch for events.
virtual bool hasError(int fd)
Check if file descriptor has error.
bool readReady(int fd)
Check if file descriptor is ready for read operation.
int waitEvent(uint32_t timeout_sec, uint32_t timeout_usec=0, bool use_timeout=true)
Wait for events on registered file descriptors.
void clear()
Clear registered file descriptors.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Defines the logger used by the top-level component of kea-lfc.