src/corosio/src/local_stream_acceptor.cpp
100.0% Lines (83 / 83)
100.0% Functions (15 / 15)
Functions (15)
Function
Calls
Lines
Blocks
boost::corosio::(anonymous namespace)::open_listener_or_throw(boost::corosio::local_stream_acceptor&, boost::corosio::local_endpoint, int)
:42
39x
100.0%
100.0%
boost::corosio::local_stream_acceptor::~local_stream_acceptor()
:55
295x
100.0%
100.0%
boost::corosio::local_stream_acceptor::local_stream_acceptor(boost::capy::execution_context&)
:60
253x
100.0%
100.0%
boost::corosio::local_stream_acceptor::local_stream_acceptor(boost::capy::execution_context&, boost::corosio::local_endpoint, int)
:70
33x
100.0%
100.0%
boost::corosio::local_stream_acceptor::local_stream_acceptor(boost::corosio::io_object::handle, boost::capy::execution_context&, boost::corosio::local_endpoint, int)
:77
6x
100.0%
100.0%
boost::corosio::local_stream_acceptor::open()
:88
164x
100.0%
100.0%
boost::corosio::local_stream_acceptor::assign(int)
:101
18x
100.0%
100.0%
boost::corosio::local_stream_acceptor::native_handle() const
:113
30x
100.0%
100.0%
boost::corosio::local_stream_acceptor::bind(boost::corosio::local_endpoint, boost::corosio::bind_option)
:127
139x
100.0%
100.0%
boost::corosio::local_stream_acceptor::listen(int)
:154
114x
100.0%
100.0%
boost::corosio::local_stream_acceptor::close()
:166
317x
100.0%
100.0%
boost::corosio::local_stream_acceptor::release()
:174
15x
100.0%
100.0%
boost::corosio::local_stream_acceptor::cancel()
:184
12x
100.0%
100.0%
boost::corosio::local_stream_acceptor::local_endpoint() const
:192
21x
100.0%
100.0%
boost::corosio::local_stream_acceptor::discard_peer(boost::corosio::local_stream_acceptor&, boost::corosio::io_object::implementation*)
:200
12x
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 | #include <boost/corosio/local_stream_acceptor.hpp> | ||
| 11 | #include <boost/corosio/error.hpp> | ||
| 12 | #include <boost/corosio/detail/except.hpp> | ||
| 13 | #include <boost/corosio/detail/platform.hpp> | ||
| 14 | #include <boost/corosio/detail/local_stream_acceptor_service.hpp> | ||
| 15 | |||
| 16 | #include "src/detail/use_backend_service.hpp" | ||
| 17 | |||
| 18 | #include <cstring> | ||
| 19 | |||
| 20 | #if BOOST_COROSIO_POSIX | ||
| 21 | #include <sys/socket.h> | ||
| 22 | #include <unistd.h> | ||
| 23 | #else | ||
| 24 | // Windows: AF_UNIX socket files are reparse points with tag | ||
| 25 | // IO_REPARSE_TAG_AF_UNIX. DeleteFileA reliably removes them; | ||
| 26 | // std::filesystem::remove via libstdc++/MS STL was observed to | ||
| 27 | // silently leave them in place on at least some Windows hosts, | ||
| 28 | // so call the Win32 API directly. | ||
| 29 | #define WIN32_LEAN_AND_MEAN | ||
| 30 | #include <winsock2.h> | ||
| 31 | #include <windows.h> | ||
| 32 | #ifndef AF_UNIX | ||
| 33 | #define AF_UNIX 1 | ||
| 34 | #endif | ||
| 35 | #endif | ||
| 36 | |||
| 37 | namespace boost::corosio { | ||
| 38 | |||
| 39 | namespace { | ||
| 40 | |||
| 41 | void | ||
| 42 | 39x | open_listener_or_throw( | |
| 43 | local_stream_acceptor& acc, corosio::local_endpoint ep, int backlog) | ||
| 44 | { | ||
| 45 | 39x | if (auto ec = acc.open()) | |
| 46 | 9x | detail::throw_system_error(ec, "local_stream_acceptor"); | |
| 47 | 30x | if (auto ec = acc.bind(ep)) | |
| 48 | 6x | detail::throw_system_error(ec, "local_stream_acceptor"); | |
| 49 | 24x | if (auto ec = acc.listen(backlog)) | |
| 50 | 9x | detail::throw_system_error(ec, "local_stream_acceptor"); | |
| 51 | 15x | } | |
| 52 | |||
| 53 | } // namespace | ||
| 54 | |||
| 55 | 295x | local_stream_acceptor::~local_stream_acceptor() | |
| 56 | { | ||
| 57 | 295x | close(); | |
| 58 | 295x | } | |
| 59 | |||
| 60 | 253x | local_stream_acceptor::local_stream_acceptor(capy::execution_context& ctx) | |
| 61 | 253x | : io_object(handle( | |
| 62 | ctx, | ||
| 63 | detail::use_backend_service< | ||
| 64 | detail::local_stream_acceptor_service_of, | ||
| 65 | 253x | detail::local_stream_acceptor_service>(ctx))) | |
| 66 | 506x | , ctx_(ctx) | |
| 67 | { | ||
| 68 | 253x | } | |
| 69 | |||
| 70 | 33x | local_stream_acceptor::local_stream_acceptor( | |
| 71 | 33x | capy::execution_context& ctx, corosio::local_endpoint ep, int backlog) | |
| 72 | 33x | : local_stream_acceptor(ctx) | |
| 73 | { | ||
| 74 | 33x | open_listener_or_throw(*this, ep, backlog); | |
| 75 | 33x | } | |
| 76 | |||
| 77 | 6x | local_stream_acceptor::local_stream_acceptor( | |
| 78 | handle h, | ||
| 79 | capy::execution_context& ctx, | ||
| 80 | corosio::local_endpoint ep, | ||
| 81 | 6x | int backlog) | |
| 82 | 6x | : local_stream_acceptor(std::move(h), ctx) | |
| 83 | { | ||
| 84 | 6x | open_listener_or_throw(*this, ep, backlog); | |
| 85 | 6x | } | |
| 86 | |||
| 87 | std::error_code | ||
| 88 | 164x | local_stream_acceptor::open() noexcept | |
| 89 | { | ||
| 90 | 164x | if (is_open()) | |
| 91 | 3x | return {}; | |
| 92 | auto& svc = | ||
| 93 | 161x | static_cast<detail::local_stream_acceptor_service&>(h_.service()); | |
| 94 | 161x | auto ec = svc.open_acceptor_socket( | |
| 95 | 161x | static_cast<local_stream_acceptor::implementation&>(*h_.get()), AF_UNIX, | |
| 96 | SOCK_STREAM, 0); | ||
| 97 | 161x | return ec; | |
| 98 | } | ||
| 99 | |||
| 100 | std::error_code | ||
| 101 | 18x | local_stream_acceptor::assign(native_handle_type fd) noexcept | |
| 102 | { | ||
| 103 | 18x | if (is_open()) | |
| 104 | 9x | return make_error_code(error::already_open); | |
| 105 | auto& svc = | ||
| 106 | 9x | static_cast<detail::local_stream_acceptor_service&>(h_.service()); | |
| 107 | 9x | auto ec = svc.assign_socket( | |
| 108 | 9x | static_cast<local_stream_acceptor::implementation&>(*h_.get()), fd); | |
| 109 | 9x | return ec; | |
| 110 | } | ||
| 111 | |||
| 112 | native_handle_type | ||
| 113 | 30x | local_stream_acceptor::native_handle() const noexcept | |
| 114 | { | ||
| 115 | 30x | if (!is_open()) | |
| 116 | { | ||
| 117 | #if BOOST_COROSIO_HAS_IOCP | ||
| 118 | return static_cast<native_handle_type>(~0ull); // INVALID_SOCKET | ||
| 119 | #else | ||
| 120 | 6x | return -1; | |
| 121 | #endif | ||
| 122 | } | ||
| 123 | 24x | return get().native_handle(); | |
| 124 | } | ||
| 125 | |||
| 126 | std::error_code | ||
| 127 | 139x | local_stream_acceptor::bind( | |
| 128 | corosio::local_endpoint ep, bind_option opt) noexcept | ||
| 129 | { | ||
| 130 | 139x | if (!is_open()) | |
| 131 | 3x | return make_error_code(std::errc::bad_file_descriptor); | |
| 132 | |||
| 133 | 136x | if (opt == bind_option::unlink_existing && !ep.empty() && !ep.is_abstract()) | |
| 134 | { | ||
| 135 | // Best-effort removal; missing file is fine. | ||
| 136 | 6x | auto p = ep.path(); | |
| 137 | char buf[local_endpoint::max_path_length + 1]; | ||
| 138 | 6x | std::memcpy(buf, p.data(), p.size()); | |
| 139 | 6x | buf[p.size()] = '\0'; | |
| 140 | #if BOOST_COROSIO_POSIX | ||
| 141 | 6x | ::unlink(buf); | |
| 142 | #else | ||
| 143 | ::DeleteFileA(buf); | ||
| 144 | #endif | ||
| 145 | } | ||
| 146 | |||
| 147 | auto& svc = | ||
| 148 | 136x | static_cast<detail::local_stream_acceptor_service&>(h_.service()); | |
| 149 | 136x | return svc.bind_acceptor( | |
| 150 | 272x | static_cast<local_stream_acceptor::implementation&>(*h_.get()), ep); | |
| 151 | } | ||
| 152 | |||
| 153 | std::error_code | ||
| 154 | 114x | local_stream_acceptor::listen(int backlog) noexcept | |
| 155 | { | ||
| 156 | 114x | if (!is_open()) | |
| 157 | 3x | return make_error_code(std::errc::bad_file_descriptor); | |
| 158 | auto& svc = | ||
| 159 | 111x | static_cast<detail::local_stream_acceptor_service&>(h_.service()); | |
| 160 | 111x | return svc.listen_acceptor( | |
| 161 | 111x | static_cast<local_stream_acceptor::implementation&>(*h_.get()), | |
| 162 | 111x | backlog); | |
| 163 | } | ||
| 164 | |||
| 165 | void | ||
| 166 | 317x | local_stream_acceptor::close() noexcept | |
| 167 | { | ||
| 168 | 317x | if (!is_open()) | |
| 169 | 172x | return; | |
| 170 | 145x | h_.service().close(h_); | |
| 171 | } | ||
| 172 | |||
| 173 | capy::results<native_handle_type> | ||
| 174 | 15x | local_stream_acceptor::release() noexcept | |
| 175 | { | ||
| 176 | 15x | if (!is_open()) | |
| 177 | return { | ||
| 178 | 6x | make_error_code(std::errc::bad_file_descriptor), | |
| 179 | 3x | detail::invalid_native_handle}; | |
| 180 | 12x | return {std::error_code{}, get().release_socket()}; | |
| 181 | } | ||
| 182 | |||
| 183 | void | ||
| 184 | 12x | local_stream_acceptor::cancel() noexcept | |
| 185 | { | ||
| 186 | 12x | if (!is_open()) | |
| 187 | 3x | return; | |
| 188 | 9x | get().cancel(); | |
| 189 | } | ||
| 190 | |||
| 191 | local_endpoint | ||
| 192 | 21x | local_stream_acceptor::local_endpoint() const noexcept | |
| 193 | { | ||
| 194 | 21x | if (!is_open()) | |
| 195 | 3x | return corosio::local_endpoint{}; | |
| 196 | 18x | return get().local_endpoint(); | |
| 197 | } | ||
| 198 | |||
| 199 | void | ||
| 200 | 12x | local_stream_acceptor::discard_peer( | |
| 201 | local_stream_acceptor& acc, io_object::implementation* impl) noexcept | ||
| 202 | { | ||
| 203 | // Its own handle closes and releases the peer, as the socket the | ||
| 204 | // awaiter would have received does. | ||
| 205 | handle h( | ||
| 206 | acc.ctx_, | ||
| 207 | detail::use_backend_service< | ||
| 208 | detail::local_stream_service_of, | ||
| 209 | 12x | detail::local_stream_service>(acc.ctx_), | |
| 210 | 12x | impl); | |
| 211 | 12x | } | |
| 212 | |||
| 213 | } // namespace boost::corosio | ||
| 214 |