Kea 3.1.5
select_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
14#ifndef FD_COPY
15#define FD_COPY(orig, copy) \
16 do { \
17 memcpy(copy, orig, sizeof(fd_set)); \
18 } while (0)
19#endif
20
21namespace isc {
22namespace util {
23
27
29 if (fd < 0) {
30 isc_throw(BadValue, "invalid negative value for fd");
31 }
32 if (fd >= FD_SETSIZE) {
33 isc_throw(BadValue, "invalid value for fd exceeds maximum allowed " << FD_SETSIZE);
34 }
35 // Add this socket to read set
36 FD_SET(fd, &read_fd_set_);
37 if (fd > max_fd_) {
38 max_fd_ = fd;
39 }
40}
41
42int SelectEventHandler::waitEvent(uint32_t timeout_sec, uint32_t timeout_usec /* = 0 */,
43 bool use_timeout /* = true */) {
44 // Sanity check for microsecond timeout.
45 if (timeout_usec >= 1000000) {
46 isc_throw(BadValue, "fractional timeout must be shorter than"
47 " one million microseconds");
48 }
49 struct timeval select_timeout;
50 struct timeval* select_timeout_p = 0;
51 if (use_timeout) {
52 select_timeout.tv_sec = timeout_sec;
53 select_timeout.tv_usec = timeout_usec;
54 select_timeout_p = &select_timeout;
55 }
56
57 FD_COPY(&read_fd_set_, &read_fd_set_data_);
58
59 return (select(max_fd_ + 1, &read_fd_set_data_, 0, 0, select_timeout_p));
60}
61
63 if (fd < 0) {
64 isc_throw(BadValue, "invalid negative value for fd");
65 }
66 if (fd >= FD_SETSIZE) {
67 isc_throw(BadValue, "invalid value for fd exceeds maximum allowed " << FD_SETSIZE);
68 }
69 return (FD_ISSET(fd, &read_fd_set_data_));
70}
71
73 if (fd < 0) {
74 isc_throw(BadValue, "invalid negative value for fd");
75 }
76 if (fd >= FD_SETSIZE) {
77 isc_throw(BadValue, "invalid value for fd exceeds maximum allowed " << FD_SETSIZE);
78 }
79 return (false);
80}
81
83 FD_ZERO(&read_fd_set_);
84 max_fd_ = 0;
85}
86
87} // end of namespace isc::util
88} // 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.
int waitEvent(uint32_t timeout_sec, uint32_t timeout_usec=0, bool use_timeout=true)
Wait for events on registered file descriptors.
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.
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.
#define FD_COPY(orig, copy)