include/boost/corosio/native/detail/epoll/epoll_traits.hpp
79.5% Lines (31/39)
90.9% List of functions (10/11)
Functions (11)
Function
Calls
Lines
Blocks
boost::corosio::detail::epoll_traits::stream_socket_hook::on_set_option(int, int, int, void const*, unsigned long)
:47
32x
80.0%
86.0%
boost::corosio::detail::epoll_traits::stream_socket_hook::pre_shutdown(int)
:57
18753x
100.0%
100.0%
boost::corosio::detail::epoll_traits::stream_socket_hook::pre_destroy(int)
:58
6237x
100.0%
100.0%
boost::corosio::detail::epoll_traits::write_policy::write(int, iovec*, int)
:63
0
0.0%
0.0%
boost::corosio::detail::epoll_traits::write_policy::write_one(int, void const*, unsigned long)
:78
126459x
100.0%
100.0%
boost::corosio::detail::epoll_traits::accept_policy::do_accept(int, sockaddr_storage&, unsigned int&)
:93
4118x
100.0%
100.0%
boost::corosio::detail::epoll_traits::create_socket(int, int, int)
:110
2237x
100.0%
100.0%
boost::corosio::detail::epoll_traits::configure_ip_socket(int, int)
:118
2123x
100.0%
100.0%
boost::corosio::detail::epoll_traits::configure_ip_acceptor(int, int)
:132
94x
100.0%
100.0%
boost::corosio::detail::epoll_traits::configure_local_socket(int)
:145
20x
100.0%
100.0%
boost::corosio::detail::epoll_traits::validate_assigned_fd(int)
:153
14x
100.0%
100.0%
| Line | TLA | Hits | Source Code |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2026 Michael Vandeberg | ||
| 3 | // | ||
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
| 6 | // | ||
| 7 | // Official repository: https://github.com/cppalliance/corosio | ||
| 8 | // | ||
| 9 | |||
| 10 | #ifndef BOOST_COROSIO_NATIVE_DETAIL_EPOLL_EPOLL_TRAITS_HPP | ||
| 11 | #define BOOST_COROSIO_NATIVE_DETAIL_EPOLL_EPOLL_TRAITS_HPP | ||
| 12 | |||
| 13 | #include <boost/corosio/detail/platform.hpp> | ||
| 14 | |||
| 15 | #if BOOST_COROSIO_HAS_EPOLL | ||
| 16 | |||
| 17 | #include <boost/corosio/native/detail/make_err.hpp> | ||
| 18 | #include <boost/corosio/native/detail/reactor/reactor_descriptor_state.hpp> | ||
| 19 | |||
| 20 | #include <system_error> | ||
| 21 | |||
| 22 | #include <errno.h> | ||
| 23 | #include <netinet/in.h> | ||
| 24 | #include <sys/socket.h> | ||
| 25 | |||
| 26 | /* epoll backend traits. | ||
| 27 | |||
| 28 | Captures the platform-specific behavior of the Linux epoll backend: | ||
| 29 | atomic SOCK_NONBLOCK|SOCK_CLOEXEC on socket(), accept4() for | ||
| 30 | accepted connections, and sendmsg(MSG_NOSIGNAL) for writes. | ||
| 31 | */ | ||
| 32 | |||
| 33 | namespace boost::corosio::detail { | ||
| 34 | |||
| 35 | class epoll_scheduler; | ||
| 36 | |||
| 37 | struct epoll_traits | ||
| 38 | { | ||
| 39 | using scheduler_type = epoll_scheduler; | ||
| 40 | using desc_state_type = reactor_descriptor_state; | ||
| 41 | |||
| 42 | static constexpr bool needs_write_notification = false; | ||
| 43 | |||
| 44 | // No extra per-socket state or lifecycle hooks needed for epoll. | ||
| 45 | struct stream_socket_hook | ||
| 46 | { | ||
| 47 | 32x | std::error_code on_set_option( | |
| 48 | int fd, int level, int optname, | ||
| 49 | void const* data, std::size_t size) noexcept | ||
| 50 | { | ||
| 51 | 32x | if (::setsockopt( | |
| 52 | fd, level, optname, data, | ||
| 53 | 32x | static_cast<socklen_t>(size)) != 0) | |
| 54 | ✗ | return make_err(errno); | |
| 55 | 32x | return {}; | |
| 56 | } | ||
| 57 | 18753x | static void pre_shutdown(int) noexcept {} | |
| 58 | 6237x | static void pre_destroy(int) noexcept {} | |
| 59 | }; | ||
| 60 | |||
| 61 | struct write_policy | ||
| 62 | { | ||
| 63 | ✗ | static ssize_t write(int fd, iovec* iovecs, int count) noexcept | |
| 64 | { | ||
| 65 | ✗ | msghdr msg{}; | |
| 66 | ✗ | msg.msg_iov = iovecs; | |
| 67 | ✗ | msg.msg_iovlen = static_cast<std::size_t>(count); | |
| 68 | |||
| 69 | ssize_t n; | ||
| 70 | do | ||
| 71 | { | ||
| 72 | ✗ | n = ::sendmsg(fd, &msg, MSG_NOSIGNAL); | |
| 73 | } | ||
| 74 | ✗ | while (n < 0 && errno == EINTR); | |
| 75 | ✗ | return n; | |
| 76 | } | ||
| 77 | |||
| 78 | 126459x | static ssize_t write_one( | |
| 79 | int fd, void const* data, std::size_t size) noexcept | ||
| 80 | { | ||
| 81 | ssize_t n; | ||
| 82 | do | ||
| 83 | { | ||
| 84 | 126459x | n = ::send(fd, data, size, MSG_NOSIGNAL); | |
| 85 | } | ||
| 86 | 126459x | while (n < 0 && errno == EINTR); | |
| 87 | 126459x | return n; | |
| 88 | } | ||
| 89 | }; | ||
| 90 | |||
| 91 | struct accept_policy | ||
| 92 | { | ||
| 93 | 4118x | static int do_accept( | |
| 94 | int fd, sockaddr_storage& peer, socklen_t& addrlen) noexcept | ||
| 95 | { | ||
| 96 | 4118x | addrlen = sizeof(peer); | |
| 97 | int new_fd; | ||
| 98 | do | ||
| 99 | { | ||
| 100 | 4118x | new_fd = ::accept4( | |
| 101 | fd, reinterpret_cast<sockaddr*>(&peer), &addrlen, | ||
| 102 | SOCK_NONBLOCK | SOCK_CLOEXEC); | ||
| 103 | } | ||
| 104 | 4118x | while (new_fd < 0 && errno == EINTR); | |
| 105 | 4118x | return new_fd; | |
| 106 | } | ||
| 107 | }; | ||
| 108 | |||
| 109 | // Create a nonblocking, close-on-exec socket using Linux's atomic flags. | ||
| 110 | 2237x | static int create_socket(int family, int type, int protocol) noexcept | |
| 111 | { | ||
| 112 | 2237x | return ::socket(family, type | SOCK_NONBLOCK | SOCK_CLOEXEC, protocol); | |
| 113 | } | ||
| 114 | |||
| 115 | // Apply protocol-specific options after socket creation. | ||
| 116 | // For IP sockets, sets IPV6_V6ONLY on AF_INET6 (best-effort). | ||
| 117 | static std::error_code | ||
| 118 | 2123x | configure_ip_socket(int fd, int family) noexcept | |
| 119 | { | ||
| 120 | 2123x | if (family == AF_INET6) | |
| 121 | { | ||
| 122 | 14x | int one = 1; | |
| 123 | 14x | (void)::setsockopt( | |
| 124 | fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one)); | ||
| 125 | } | ||
| 126 | 2123x | return {}; | |
| 127 | } | ||
| 128 | |||
| 129 | // Apply protocol-specific options for acceptor sockets. | ||
| 130 | // For IP acceptors, sets IPV6_V6ONLY=0 (dual-stack, best-effort). | ||
| 131 | static std::error_code | ||
| 132 | 94x | configure_ip_acceptor(int fd, int family) noexcept | |
| 133 | { | ||
| 134 | 94x | if (family == AF_INET6) | |
| 135 | { | ||
| 136 | 9x | int val = 0; | |
| 137 | 9x | (void)::setsockopt( | |
| 138 | fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val)); | ||
| 139 | } | ||
| 140 | 94x | return {}; | |
| 141 | } | ||
| 142 | |||
| 143 | // No extra configuration needed for local (unix) sockets on epoll. | ||
| 144 | static std::error_code | ||
| 145 | 20x | configure_local_socket(int /*fd*/) noexcept | |
| 146 | { | ||
| 147 | 20x | return {}; | |
| 148 | } | ||
| 149 | |||
| 150 | // Non-mutating validation for fds adopted via assign(). Used when | ||
| 151 | // the caller retains fd ownership responsibility. | ||
| 152 | static std::error_code | ||
| 153 | 14x | validate_assigned_fd(int /*fd*/) noexcept | |
| 154 | { | ||
| 155 | 14x | return {}; | |
| 156 | } | ||
| 157 | }; | ||
| 158 | |||
| 159 | } // namespace boost::corosio::detail | ||
| 160 | |||
| 161 | #endif // BOOST_COROSIO_HAS_EPOLL | ||
| 162 | |||
| 163 | #endif // BOOST_COROSIO_NATIVE_DETAIL_EPOLL_EPOLL_TRAITS_HPP | ||
| 164 |