src/corosio/src/local_stream_acceptor.cpp

100.0% Lines (82/82) 100.0% List of functions (12/12) 59.1% Branches (39/66)
local_stream_acceptor.cpp
f(x) Functions (12)
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 #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 192x local_stream_acceptor::~local_stream_acceptor()
32 192x {
33 106x close();
34 192x }
35
36 172x local_stream_acceptor::local_stream_acceptor(capy::execution_context& ctx)
37 86x : io_object(create_handle<detail::local_stream_acceptor_service>(ctx))
38 86x , ctx_(ctx)
39 172x {
40 172x }
41
42 8x local_stream_acceptor::local_stream_acceptor(
43 capy::execution_context& ctx, corosio::local_endpoint ep, int backlog)
44 8x : local_stream_acceptor(ctx)
45 {
46
2/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 6 times.
8x if (auto ec = open())
47
1/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2x detail::throw_system_error(ec, "local_stream_acceptor");
48
2/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 4 times.
6x if (auto ec = bind(ep))
49
1/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2x detail::throw_system_error(ec, "local_stream_acceptor");
50
2/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
4x if (auto ec = listen(backlog))
51
1/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2x detail::throw_system_error(ec, "local_stream_acceptor");
52 8x }
53
54 std::error_code
55 84x local_stream_acceptor::open(local_stream proto) noexcept
56 {
57
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 82 times.
84x if (is_open())
58 2x return {};
59 82x auto& svc =
60 82x static_cast<detail::local_stream_acceptor_service&>(h_.service());
61
2/4
✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 82 times.
✗ Branch 3 not taken.
164x auto ec = svc.open_acceptor_socket(
62 82x static_cast<local_stream_acceptor::implementation&>(*h_.get()),
63 82x proto.family(), proto.type(), proto.protocol());
64 82x return ec;
65 84x }
66
67 std::error_code
68 10x local_stream_acceptor::assign(native_handle_type fd) noexcept
69 {
70 10x auto& svc =
71 10x static_cast<detail::local_stream_acceptor_service&>(h_.service());
72
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
20x auto ec = svc.assign_socket(
73 10x static_cast<local_stream_acceptor::implementation&>(*h_.get()), fd);
74 10x return ec;
75 }
76
77 native_handle_type
78 12x local_stream_acceptor::native_handle() const noexcept
79 {
80
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 8 times.
12x 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 8x return get().native_handle();
89 12x }
90
91 std::error_code
92 74x local_stream_acceptor::bind(
93 corosio::local_endpoint ep, bind_option opt) noexcept
94 {
95
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 72 times.
74x if (!is_open())
96 2x return make_error_code(std::errc::bad_file_descriptor);
97
98
4/6
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 68 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
72x if (opt == bind_option::unlink_existing && !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
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4x ::unlink(buf);
107 #else
108 ::DeleteFileA(buf);
109 #endif
110 4x }
111
112 72x auto& svc =
113 72x static_cast<detail::local_stream_acceptor_service&>(h_.service());
114
2/4
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
✗ Branch 3 not taken.
144x return svc.bind_acceptor(
115 72x static_cast<local_stream_acceptor::implementation&>(*h_.get()), ep);
116 74x }
117
118 std::error_code
119 59x local_stream_acceptor::listen(int backlog) noexcept
120 {
121
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 57 times.
59x if (!is_open())
122 2x return make_error_code(std::errc::bad_file_descriptor);
123 57x auto& svc =
124 57x static_cast<detail::local_stream_acceptor_service&>(h_.service());
125
2/4
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 57 times.
✗ Branch 3 not taken.
114x return svc.listen_acceptor(
126 57x static_cast<local_stream_acceptor::implementation&>(*h_.get()),
127 57x backlog);
128 59x }
129
130 void
131 117x local_stream_acceptor::close() noexcept
132 {
133
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 43 times.
117x if (!is_open())
134 43x return;
135
1/2
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
74x h_.service().close(h_);
136 117x }
137
138 native_handle_type
139 12x local_stream_acceptor::release()
140 {
141
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
12x if (!is_open())
142 2x detail::throw_system_error(
143 2x make_error_code(std::errc::bad_file_descriptor),
144 "local_stream_acceptor::release");
145 10x return get().release_socket();
146 }
147
148 void
149 8x local_stream_acceptor::cancel() noexcept
150 {
151
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8x if (!is_open())
152 2x return;
153 6x get().cancel();
154 8x }
155
156 local_endpoint
157 12x local_stream_acceptor::local_endpoint() const noexcept
158 {
159
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
12x if (!is_open())
160 2x return corosio::local_endpoint{};
161 10x return get().local_endpoint();
162 12x }
163
164 } // namespace boost::corosio
165