src/corosio/src/local_stream_acceptor.cpp
100.0% Lines (68 / 68)
100.0% Functions (12 / 12)
Functions (12)
Function
Calls
Lines
Blocks
boost::corosio::local_stream_acceptor::~local_stream_acceptor()
:36
112x
100.0%
100.0%
boost::corosio::local_stream_acceptor::local_stream_acceptor(boost::capy::execution_context&)
:41
92x
100.0%
100.0%
boost::corosio::local_stream_acceptor::local_stream_acceptor(boost::capy::execution_context&, boost::corosio::local_endpoint, int)
:47
14x
100.0%
100.0%
boost::corosio::local_stream_acceptor::open()
:60
90x
100.0%
100.0%
boost::corosio::local_stream_acceptor::assign(int)
:73
10x
100.0%
100.0%
boost::corosio::local_stream_acceptor::native_handle() const
:83
12x
100.0%
100.0%
boost::corosio::local_stream_acceptor::bind(boost::corosio::local_endpoint, boost::corosio::bind_option)
:97
77x
100.0%
100.0%
boost::corosio::local_stream_acceptor::listen(int)
:124
62x
100.0%
100.0%
boost::corosio::local_stream_acceptor::close()
:136
123x
100.0%
100.0%
boost::corosio::local_stream_acceptor::release()
:144
12x
100.0%
100.0%
boost::corosio::local_stream_acceptor::cancel()
:154
8x
100.0%
100.0%
boost::corosio::local_stream_acceptor::local_endpoint() const
:162
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/detail/except.hpp> | ||
| 12 | #include <boost/corosio/detail/platform.hpp> | ||
| 13 | #include <boost/corosio/detail/local_stream_acceptor_service.hpp> | ||
| 14 | |||
| 15 | #include <cstring> | ||
| 16 | |||
| 17 | #if BOOST_COROSIO_POSIX | ||
| 18 | #include <sys/socket.h> | ||
| 19 | #include <unistd.h> | ||
| 20 | #else | ||
| 21 | // Windows: AF_UNIX socket files are reparse points with tag | ||
| 22 | // IO_REPARSE_TAG_AF_UNIX. DeleteFileA reliably removes them; | ||
| 23 | // std::filesystem::remove via libstdc++/MS STL was observed to | ||
| 24 | // silently leave them in place on at least some Windows hosts, | ||
| 25 | // so call the Win32 API directly. | ||
| 26 | #define WIN32_LEAN_AND_MEAN | ||
| 27 | #include <winsock2.h> | ||
| 28 | #include <windows.h> | ||
| 29 | #ifndef AF_UNIX | ||
| 30 | #define AF_UNIX 1 | ||
| 31 | #endif | ||
| 32 | #endif | ||
| 33 | |||
| 34 | namespace boost::corosio { | ||
| 35 | |||
| 36 | 112x | local_stream_acceptor::~local_stream_acceptor() | |
| 37 | { | ||
| 38 | 112x | close(); | |
| 39 | 112x | } | |
| 40 | |||
| 41 | 92x | local_stream_acceptor::local_stream_acceptor(capy::execution_context& ctx) | |
| 42 | 92x | : io_object(create_handle<detail::local_stream_acceptor_service>(ctx)) | |
| 43 | 92x | , ctx_(ctx) | |
| 44 | { | ||
| 45 | 92x | } | |
| 46 | |||
| 47 | 14x | local_stream_acceptor::local_stream_acceptor( | |
| 48 | 14x | capy::execution_context& ctx, corosio::local_endpoint ep, int backlog) | |
| 49 | 14x | : local_stream_acceptor(ctx) | |
| 50 | { | ||
| 51 | 14x | if (auto ec = open()) | |
| 52 | 5x | detail::throw_system_error(ec, "local_stream_acceptor"); | |
| 53 | 9x | if (auto ec = bind(ep)) | |
| 54 | 2x | detail::throw_system_error(ec, "local_stream_acceptor"); | |
| 55 | 7x | if (auto ec = listen(backlog)) | |
| 56 | 5x | detail::throw_system_error(ec, "local_stream_acceptor"); | |
| 57 | 14x | } | |
| 58 | |||
| 59 | std::error_code | ||
| 60 | 90x | local_stream_acceptor::open() noexcept | |
| 61 | { | ||
| 62 | 90x | if (is_open()) | |
| 63 | 2x | return {}; | |
| 64 | auto& svc = | ||
| 65 | 88x | static_cast<detail::local_stream_acceptor_service&>(h_.service()); | |
| 66 | 88x | auto ec = svc.open_acceptor_socket( | |
| 67 | 88x | static_cast<local_stream_acceptor::implementation&>(*h_.get()), AF_UNIX, | |
| 68 | SOCK_STREAM, 0); | ||
| 69 | 88x | return ec; | |
| 70 | } | ||
| 71 | |||
| 72 | std::error_code | ||
| 73 | 10x | local_stream_acceptor::assign(native_handle_type fd) noexcept | |
| 74 | { | ||
| 75 | auto& svc = | ||
| 76 | 10x | static_cast<detail::local_stream_acceptor_service&>(h_.service()); | |
| 77 | 10x | auto ec = svc.assign_socket( | |
| 78 | 10x | static_cast<local_stream_acceptor::implementation&>(*h_.get()), fd); | |
| 79 | 10x | return ec; | |
| 80 | } | ||
| 81 | |||
| 82 | native_handle_type | ||
| 83 | 12x | local_stream_acceptor::native_handle() const noexcept | |
| 84 | { | ||
| 85 | 12x | if (!is_open()) | |
| 86 | { | ||
| 87 | #if BOOST_COROSIO_HAS_IOCP | ||
| 88 | return static_cast<native_handle_type>(~0ull); // INVALID_SOCKET | ||
| 89 | #else | ||
| 90 | 4x | return -1; | |
| 91 | #endif | ||
| 92 | } | ||
| 93 | 8x | return get().native_handle(); | |
| 94 | } | ||
| 95 | |||
| 96 | std::error_code | ||
| 97 | 77x | local_stream_acceptor::bind( | |
| 98 | corosio::local_endpoint ep, bind_option opt) noexcept | ||
| 99 | { | ||
| 100 | 77x | if (!is_open()) | |
| 101 | 2x | return make_error_code(std::errc::bad_file_descriptor); | |
| 102 | |||
| 103 | 75x | if (opt == bind_option::unlink_existing && !ep.empty() && !ep.is_abstract()) | |
| 104 | { | ||
| 105 | // Best-effort removal; missing file is fine. | ||
| 106 | 4x | auto p = ep.path(); | |
| 107 | char buf[local_endpoint::max_path_length + 1]; | ||
| 108 | 4x | std::memcpy(buf, p.data(), p.size()); | |
| 109 | 4x | buf[p.size()] = '\0'; | |
| 110 | #if BOOST_COROSIO_POSIX | ||
| 111 | 4x | ::unlink(buf); | |
| 112 | #else | ||
| 113 | ::DeleteFileA(buf); | ||
| 114 | #endif | ||
| 115 | } | ||
| 116 | |||
| 117 | auto& svc = | ||
| 118 | 75x | static_cast<detail::local_stream_acceptor_service&>(h_.service()); | |
| 119 | 75x | return svc.bind_acceptor( | |
| 120 | 150x | static_cast<local_stream_acceptor::implementation&>(*h_.get()), ep); | |
| 121 | } | ||
| 122 | |||
| 123 | std::error_code | ||
| 124 | 62x | local_stream_acceptor::listen(int backlog) noexcept | |
| 125 | { | ||
| 126 | 62x | if (!is_open()) | |
| 127 | 2x | return make_error_code(std::errc::bad_file_descriptor); | |
| 128 | auto& svc = | ||
| 129 | 60x | static_cast<detail::local_stream_acceptor_service&>(h_.service()); | |
| 130 | 60x | return svc.listen_acceptor( | |
| 131 | 60x | static_cast<local_stream_acceptor::implementation&>(*h_.get()), | |
| 132 | 60x | backlog); | |
| 133 | } | ||
| 134 | |||
| 135 | void | ||
| 136 | 123x | local_stream_acceptor::close() noexcept | |
| 137 | { | ||
| 138 | 123x | if (!is_open()) | |
| 139 | 46x | return; | |
| 140 | 77x | h_.service().close(h_); | |
| 141 | } | ||
| 142 | |||
| 143 | native_handle_type | ||
| 144 | 12x | local_stream_acceptor::release() | |
| 145 | { | ||
| 146 | 12x | if (!is_open()) | |
| 147 | 2x | detail::throw_system_error( | |
| 148 | 2x | make_error_code(std::errc::bad_file_descriptor), | |
| 149 | "local_stream_acceptor::release"); | ||
| 150 | 10x | return get().release_socket(); | |
| 151 | } | ||
| 152 | |||
| 153 | void | ||
| 154 | 8x | local_stream_acceptor::cancel() noexcept | |
| 155 | { | ||
| 156 | 8x | if (!is_open()) | |
| 157 | 2x | return; | |
| 158 | 6x | get().cancel(); | |
| 159 | } | ||
| 160 | |||
| 161 | local_endpoint | ||
| 162 | 12x | local_stream_acceptor::local_endpoint() const noexcept | |
| 163 | { | ||
| 164 | 12x | if (!is_open()) | |
| 165 | 2x | return corosio::local_endpoint{}; | |
| 166 | 10x | return get().local_endpoint(); | |
| 167 | } | ||
| 168 | |||
| 169 | } // namespace boost::corosio | ||
| 170 |