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