src/corosio/src/local_stream_acceptor.cpp

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