include/boost/corosio/native/detail/native_socket_base.hpp

97.9% Lines (46 / 47) 26.6% Branches (50 / 188) 94.6% Functions (70 / 74)
native_socket_base.hpp
f(x) Functions (74)
Function Calls Lines Branches Blocks
boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::native_socket_base() :63 125x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::native_socket_base() :63 169x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::native_socket_base() :63 4522x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::native_socket_base() :63 167x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::native_socket_base() :63 121x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::native_socket_base() :63 163x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::native_socket_base() :63 1987x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::native_socket_base() :63 163x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::~native_socket_base() :75 125x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::~native_socket_base() :75 169x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::~native_socket_base() :75 4522x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::~native_socket_base() :75 167x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::~native_socket_base() :75 121x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::~native_socket_base() :75 163x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::~native_socket_base() :75 1987x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::~native_socket_base() :75 163x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::native_handle() const :78 541x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::native_handle() const :78 539x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::native_handle() const :78 106968x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::native_handle() const :78 891x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::native_handle() const :78 522x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::native_handle() const :78 514x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::native_handle() const :78 34273x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::native_handle() const :78 871x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::family() const :84 14x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::family() const :84 9x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::family() const :84 12588x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::family() const :84 74x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::family() const :84 14x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::family() const :84 9x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::family() const :84 1719x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::family() const :84 70x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::native_family() const :94 62x 100.0% – 66.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::native_family() const :94 27x 100.0% – 66.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::native_family() const :94 24976x 71.4% 50.0% 83.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::native_family() const :94 238x 100.0% – 66.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::native_family() const :94 58x 71.4% 18.8% 66.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::native_family() const :94 26x 71.4% 50.0% 66.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::native_family() const :94 3938x 85.7% 75.0% 83.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::native_family() const :94 226x 71.4% 50.0% 66.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::local_endpoint() const :105 1x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::local_endpoint() const :105 2x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::local_endpoint() const :105 29x 0.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::local_endpoint() const :105 69x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::local_endpoint() const :105 1x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::local_endpoint() const :105 2x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::local_endpoint() const :105 29x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::local_endpoint() const :105 67x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::set_option(int, int, void const*, unsigned long) :117 11x 100.0% – 85.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::set_option(int, int, void const*, unsigned long) :117 0 100.0% – 0.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::set_option(int, int, void const*, unsigned long) :117 0 0.0% 0.0% 0.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::set_option(int, int, void const*, unsigned long) :117 47x 100.0% – 85.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::set_option(int, int, void const*, unsigned long) :117 11x 100.0% 25.0% 85.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::set_option(int, int, void const*, unsigned long) :117 0 0.0% 0.0% 0.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::set_option(int, int, void const*, unsigned long) :117 0 0.0% 0.0% 0.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::set_option(int, int, void const*, unsigned long) :117 44x 83.3% 16.7% 85.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::get_option(int, int, void*, unsigned long*) const :131 3x 100.0% – 85.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::get_option(int, int, void*, unsigned long*) const :131 3x 100.0% – 57.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::get_option(int, int, void*, unsigned long*) const :131 44x 85.7% 33.3% 85.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::get_option(int, int, void*, unsigned long*) const :131 27x 100.0% – 85.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::get_option(int, int, void*, unsigned long*) const :131 3x 100.0% 25.0% 85.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::get_option(int, int, void*, unsigned long*) const :131 3x 85.7% 33.3% 57.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::get_option(int, int, void*, unsigned long*) const :131 44x 100.0% 66.7% 85.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::get_option(int, int, void*, unsigned long*) const :131 26x 85.7% 16.7% 85.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::set_socket(int, int) :142 17x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::set_socket(int, int) :142 7220x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::set_socket(int, int) :142 17x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::set_socket(int, int) :142 4337x 100.0% – 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::do_bind(boost::corosio::local_endpoint const&) :162 30x 100.0% – 90.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::do_bind(boost::corosio::endpoint const&) :162 8x 0.0% 0.0% 90.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::kqueue_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::do_bind(boost::corosio::endpoint const&) :162 98x 100.0% – 90.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::do_bind(boost::corosio::local_endpoint const&) :162 28x 100.0% 30.0% 90.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::do_bind(boost::corosio::endpoint const&) :162 8x 100.0% 60.0% 90.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::select_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::do_bind(boost::corosio::endpoint const&) :162 93x 92.3% 40.0% 90.0%
Line Branch 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_NATIVE_SOCKET_BASE_HPP
11 #define BOOST_COROSIO_NATIVE_DETAIL_NATIVE_SOCKET_BASE_HPP
12
13 #include <boost/corosio/detail/native_handle.hpp>
14 #include <boost/corosio/endpoint.hpp>
15 #include <boost/corosio/native/detail/endpoint_convert.hpp>
16 #include <boost/corosio/native/detail/make_err.hpp>
17
18 #include <memory>
19 #include <system_error>
20
21 #include <errno.h>
22 #include <sys/socket.h>
23
24 /*
25 Readiness/completion-agnostic socket base for the POSIX-fd backends.
26
27 Holds the part of a socket impl that does not care whether the backend
28 is readiness-based (epoll/kqueue/select, which park ops on a
29 descriptor_state) or completion-based (io_uring, which submits SQEs):
30 the file descriptor, the cached family and local endpoint, and the
31 synchronous accessors (native_handle / options / bind). The impl
32 lifecycle (the intrusive refcount + the owning service's recycling
33 pool) is carried by `io_object::implementation`, not by this base.
34
35 reactor_basic_socket derives from this and adds the readiness machinery
36 (descriptor_state, register_op, the cancel/close that deregister from
37 the reactor). io_uring's socket impls derive from it and add their op
38 slots + SQE submission. This is the socket-layer analogue of io_uring
39 deriving from reactor_scheduler: io_uring sockets share the reactor's
40 readiness-agnostic socket surface, while the on-EAGAIN action (park vs
41 submit-SQE) and the op model stay backend-specific.
42
43 @tparam Derived The concrete socket type (CRTP).
44 @tparam ImplBase The public vtable base (tcp_socket::implementation,
45 udp_socket::implementation, ...).
46 @tparam Endpoint The endpoint type (endpoint or local_endpoint).
47 */
48
49 namespace boost::corosio::detail {
50
51 template<class Derived, class ImplBase, class Endpoint = endpoint>
52 class native_socket_base
53 : public ImplBase
54 {
55 protected:
56 // CRTP base: not publicly constructible. The check's preferred fix
57 // (private ctor + `friend Derived`) is infeasible here — the reactor
58 // sockets reach this base through intermediate templates
59 // (reactor_stream_socket -> reactor_basic_socket) that are not `Derived`,
60 // so a private ctor would stop those intermediates from constructing it.
61 // Protected is the correct access; suppress the private-only suggestion.
62 // NOLINTNEXTLINE(bugprone-crtp-constructor-accessibility)
63 14834x native_socket_base() = default;
64
65 7417x int fd_ = -1;
66 // Option calls, connects and datagram sends all need the family.
67 // Backends seed it where they know it and clear it whenever the
68 // descriptor goes; otherwise it is looked up once, on first use.
69 7417x mutable int family_ = AF_UNSPEC;
70 // mutable so a derived const local_endpoint() override can lazily fill
71 // it via getsockname() on first read (io_uring's lazy_pending state).
72 mutable Endpoint local_endpoint_;
73
74 public:
75 7417x ~native_socket_base() override = default;
76
77 /// Return the underlying file descriptor.
78 145119x native_handle_type native_handle() const noexcept override
79 {
80 145119x return fd_;
81 }
82
83 /// Return the socket's address family.
84 14497x corosio::family family() const noexcept override
85 {
86 14497x return to_family(native_family());
87 }
88
89 /** Return the descriptor's native address family.
90
91 @return `AF_INET`, `AF_INET6`, `AF_UNIX`, or `AF_UNSPEC` if the
92 socket is closed or the lookup fails.
93 */
94 29551x int native_family() const noexcept
95 {
96
3/16
✓ Branch 0 taken 25088 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4248 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 215 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
29551x if (fd_ < 0)
97 ✗ return AF_UNSPEC;
98 // A failed lookup stays uncached, so the next call retries.
99
5/16
✓ Branch 0 taken 25087 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 4247 times.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 215 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
29551x if (family_ == AF_UNSPEC)
100 2x family_ = socket_family(fd_);
101 29551x return family_;
102 29551x }
103
104 /// Return the cached local endpoint.
105 200x Endpoint local_endpoint() const noexcept override
106 {
107 200x return local_endpoint_;
108 }
109
110 /// Return true if the socket has an open file descriptor.
111 bool is_open() const noexcept
112 {
113 return fd_ >= 0;
114 }
115
116 /// Set a socket option.
117 113x std::error_code set_option(
118 int level,
119 int optname,
120 void const* data,
121 std::size_t size) noexcept override
122 {
123
9/32
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 55 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 10 times.
✓ Branch 7 taken 45 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✓ Branch 20 taken 44 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 9 times.
✓ Branch 23 taken 35 times.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
113x if (::setsockopt(
124 113x fd_, level, optname, data, static_cast<socklen_t>(size)) != 0)
125
3/16
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 9 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
20x return make_err(errno);
126 93x return {};
127 113x }
128
129 /// Get a socket option.
130 std::error_code
131 153x get_option(int level, int optname, void* data, std::size_t* size)
132 const noexcept override
133 {
134 153x socklen_t len = static_cast<socklen_t>(*size);
135
9/32
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 48 times.
✓ Branch 4 taken 76 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 72 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✓ Branch 20 taken 26 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 1 time.
✓ Branch 23 taken 25 times.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
153x if (::getsockopt(fd_, level, optname, data, &len) != 0)
136
3/16
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 time.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
8x return make_err(errno);
137 145x *size = static_cast<std::size_t>(len);
138 145x return {};
139 153x }
140
141 /// Assign the file descriptor and, when known, its family.
142 11591x void set_socket(int fd, int family = AF_UNSPEC) noexcept
143 {
144 11591x fd_ = fd;
145 11591x family_ = family;
146 11591x }
147
148 /// Cache the local endpoint.
149 void set_local_endpoint(Endpoint ep) noexcept
150 {
151 local_endpoint_ = ep;
152 }
153
154 /** Bind the socket to a local endpoint.
155
156 Calls ::bind() and caches the resulting local endpoint via
157 getsockname(). Readiness-agnostic; usable by any fd backend.
158
159 @param ep The endpoint to bind to.
160 @return Error code on failure, empty on success.
161 */
162 265x std::error_code do_bind(Endpoint const& ep) noexcept
163 {
164 265x sockaddr_storage storage{};
165 265x socklen_t addrlen = to_sockaddr(ep, native_family(), storage);
166
9/24
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 42 times.
✓ Branch 4 taken 129 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 7 times.
✓ Branch 7 taken 122 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 16 taken 89 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 2 times.
✓ Branch 19 taken 87 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
265x if (::bind(fd_, reinterpret_cast<sockaddr*>(&storage), addrlen) != 0)
167
3/12
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
14x return make_err(errno);
168
169 251x sockaddr_storage local_storage{};
170 251x socklen_t local_len = sizeof(local_storage);
171
6/24
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 42 times.
✓ Branch 4 taken 122 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 122 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 16 taken 87 times.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✓ Branch 19 taken 87 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
251x if (::getsockname(
172 251x fd_, reinterpret_cast<sockaddr*>(&local_storage), &local_len) ==
173 0)
174 251x local_endpoint_ =
175 251x from_sockaddr_as(local_storage, local_len, Endpoint{});
176
177 251x return {};
178 265x }
179 };
180
181 } // namespace boost::corosio::detail
182
183 #endif // BOOST_COROSIO_NATIVE_DETAIL_NATIVE_SOCKET_BASE_HPP
184