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

100.0% Lines (34 / 34) 89.9% Functions (80 / 89)
native_socket_base.hpp
f(x) Functions (89)
Function Calls Lines Blocks
boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::native_socket_base() :64 148x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::native_socket_base() :64 199x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::native_socket_base() :64 13151x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::native_socket_base() :64 166x 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() :64 137x 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() :64 180x 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() :64 6752x 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() :64 191x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::native_socket_base() :64 127x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::native_socket_base() :64 162x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::native_socket_base() :64 6112x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::native_socket_base() :64 138x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::~native_socket_base() :72 148x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::~native_socket_base() :72 199x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::~native_socket_base() :72 13151x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::~native_socket_base() :72 166x 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() :72 137x 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() :72 180x 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() :72 6752x 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() :72 191x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::~native_socket_base() :72 126x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::~native_socket_base() :72 160x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::~native_socket_base() :72 6109x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::~native_socket_base() :72 138x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::native_handle() const :75 718x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::native_handle() const :75 607x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::native_handle() const :75 44228x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::native_handle() const :75 846x 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 :75 670x 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 :75 548x 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 :75 20506x 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 :75 946x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::native_handle() const :75 883x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::native_handle() const :75 501x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::native_handle() const :75 12682x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::native_handle() const :75 1033x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::family() const :80 14x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::family() const :80 10x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::family() const :80 2197x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::family() const :80 80x 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 :80 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 :80 10x 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 :80 158x 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 :80 76x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::family() const :80 14x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::family() const :80 8x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::family() const :80 129x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::family() const :80 77x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::local_endpoint() const :86 1x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::local_endpoint() const :86 2x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::local_endpoint() const :86 29x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::local_endpoint() const :86 66x 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 :86 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 :86 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 :86 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 :86 64x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::local_endpoint() const :86 1x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::local_endpoint() const :86 2x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::local_endpoint() const :86 0 0.0% 0.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::local_endpoint() const :86 53x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::set_option(int, int, void const*, unsigned long) :98 11x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::set_option(int, int, void const*, unsigned long) :98 0 0.0% 0.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::set_option(int, int, void const*, unsigned long) :98 0 0.0% 0.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::set_option(int, int, void const*, unsigned long) :98 49x 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>::set_option(int, int, void const*, unsigned long) :98 11x 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_option(int, int, void const*, unsigned long) :98 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) :98 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) :98 46x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::set_option(int, int, void const*, unsigned long) :98 11x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::set_option(int, int, void const*, unsigned long) :98 4x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::set_option(int, int, void const*, unsigned long) :98 86x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::set_option(int, int, void const*, unsigned long) :98 47x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::get_option(int, int, void*, unsigned long*) const :112 3x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::get_option(int, int, void*, unsigned long*) const :112 0 0.0% 0.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::get_option(int, int, void*, unsigned long*) const :112 49x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::get_option(int, int, void*, unsigned long*) const :112 31x 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>::get_option(int, int, void*, unsigned long*) const :112 3x 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>::get_option(int, int, void*, unsigned long*) const :112 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>::get_option(int, int, void*, unsigned long*) const :112 48x 100.0% 100.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 :112 30x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_local_datagram_socket, boost::corosio::local_datagram_socket::implementation, boost::corosio::local_endpoint>::get_option(int, int, void*, unsigned long*) const :112 3x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::get_option(int, int, void*, unsigned long*) const :112 0 0.0% 0.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::get_option(int, int, void*, unsigned long*) const :112 43x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::uring_udp_socket, boost::corosio::udp_socket::implementation, boost::corosio::endpoint>::get_option(int, int, void*, unsigned long*) const :112 30x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_local_stream_socket, boost::corosio::local_stream_socket::implementation, boost::corosio::local_endpoint>::set_socket(int) :123 13x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::set_socket(int) :123 4226x 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) :123 12x 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) :123 2195x 100.0% 100.0% boost::corosio::detail::native_socket_base<boost::corosio::detail::epoll_tcp_socket, boost::corosio::tcp_socket::implementation, boost::corosio::endpoint>::do_bind(boost::corosio::endpoint const&) :142 0 0.0% 0.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_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 local endpoint, the impl lifecycle
31 bases (enable_shared_from_this + the service's intrusive tracking node),
32 and the synchronous accessors (native_handle / options / bind).
33
34 reactor_basic_socket derives from this and adds the readiness machinery
35 (descriptor_state, register_op, the cancel/close that deregister from
36 the reactor). io_uring's socket impls derive from it and add their op
37 slots + SQE submission. This is the socket-layer analogue of io_uring
38 deriving from reactor_scheduler: io_uring sockets share the reactor's
39 readiness-agnostic socket surface, while the on-EAGAIN action (park vs
40 submit-SQE) and the op model stay backend-specific.
41
42 @tparam Derived The concrete socket type (CRTP, for shared_from_this
43 and the intrusive node).
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 , public std::enable_shared_from_this<Derived>
55 {
56 protected:
57 // CRTP base: not publicly constructible. The check's preferred fix
58 // (private ctor + `friend Derived`) is infeasible here — the reactor
59 // sockets reach this base through intermediate templates
60 // (reactor_stream_socket -> reactor_basic_socket) that are not `Derived`,
61 // so a private ctor would stop those intermediates from constructing it.
62 // Protected is the correct access; suppress the private-only suggestion.
63 // NOLINTNEXTLINE(bugprone-crtp-constructor-accessibility)
64 27463x native_socket_base() = default;
65
66 int fd_ = -1;
67 // mutable so a derived const local_endpoint() override can lazily fill
68 // it via getsockname() on first read (io_uring's lazy_pending state).
69 mutable Endpoint local_endpoint_;
70
71 public:
72 27457x ~native_socket_base() override = default;
73
74 /// Return the underlying file descriptor.
75 84168x native_handle_type native_handle() const noexcept override
76 {
77 84168x return fd_;
78 }
79
80 2787x corosio::family family() const noexcept override
81 {
82 2787x return to_family(socket_family(fd_));
83 }
84
85 /// Return the cached local endpoint.
86 250x Endpoint local_endpoint() const noexcept override
87 {
88 250x return local_endpoint_;
89 }
90
91 /// Return true if the socket has an open file descriptor.
92 bool is_open() const noexcept
93 {
94 return fd_ >= 0;
95 }
96
97 /// Set a socket option.
98 265x std::error_code set_option(
99 int level,
100 int optname,
101 void const* data,
102 std::size_t size) noexcept override
103 {
104 265x if (::setsockopt(
105 265x fd_, level, optname, data, static_cast<socklen_t>(size)) != 0)
106 16x return make_err(errno);
107 249x return {};
108 }
109
110 /// Get a socket option.
111 std::error_code
112 252x get_option(int level, int optname, void* data, std::size_t* size)
113 const noexcept override
114 {
115 252x socklen_t len = static_cast<socklen_t>(*size);
116 252x if (::getsockopt(fd_, level, optname, data, &len) != 0)
117 21x return make_err(errno);
118 231x *size = static_cast<std::size_t>(len);
119 231x return {};
120 }
121
122 /// Assign the file descriptor.
123 6446x void set_socket(int fd) noexcept
124 {
125 6446x fd_ = fd;
126 6446x }
127
128 /// Cache the local endpoint.
129 void set_local_endpoint(Endpoint ep) noexcept
130 {
131 local_endpoint_ = ep;
132 }
133
134 /** Bind the socket to a local endpoint.
135
136 Calls ::bind() and caches the resulting local endpoint via
137 getsockname(). Readiness-agnostic; usable by any fd backend.
138
139 @param ep The endpoint to bind to.
140 @return Error code on failure, empty on success.
141 */
142 276x std::error_code do_bind(Endpoint const& ep) noexcept
143 {
144 276x sockaddr_storage storage{};
145 276x socklen_t addrlen = to_sockaddr(ep, socket_family(fd_), storage);
146 276x if (::bind(fd_, reinterpret_cast<sockaddr*>(&storage), addrlen) != 0)
147 24x return make_err(errno);
148
149 252x sockaddr_storage local_storage{};
150 252x socklen_t local_len = sizeof(local_storage);
151 252x if (::getsockname(
152 252x fd_, reinterpret_cast<sockaddr*>(&local_storage), &local_len) ==
153 0)
154 189x local_endpoint_ =
155 252x from_sockaddr_as(local_storage, local_len, Endpoint{});
156
157 252x return {};
158 }
159 };
160
161 } // namespace boost::corosio::detail
162
163 #endif // BOOST_COROSIO_NATIVE_DETAIL_NATIVE_SOCKET_BASE_HPP
164