src/corosio/src/local_stream_acceptor.cpp

100.0% Lines (68/68) 100.0% List of functions (12/12) 93.5% Branches (29/31)
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 91x local_stream_acceptor::~local_stream_acceptor()
32 {
33 91x close();
34 91x }
35
36 82x local_stream_acceptor::local_stream_acceptor(capy::execution_context& ctx)
37 82x : io_object(create_handle<detail::local_stream_acceptor_service>(ctx))
38
1/1
✓ Branch 2 → 3 taken 82 times.
82x , ctx_(ctx)
39 {
40 82x }
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
2/2
✓ Branch 5 → 6 taken 1 time.
✓ Branch 5 → 7 taken 3 times.
4x if (auto ec = open())
47 1x detail::throw_system_error(ec, "local_stream_acceptor");
48
2/2
✓ Branch 9 → 10 taken 1 time.
✓ Branch 9 → 11 taken 2 times.
3x if (auto ec = bind(ep))
49 1x detail::throw_system_error(ec, "local_stream_acceptor");
50
2/2
✓ Branch 13 → 14 taken 1 time.
✓ Branch 13 → 15 taken 1 time.
2x if (auto ec = listen(backlog))
51 1x detail::throw_system_error(ec, "local_stream_acceptor");
52 4x }
53
54 std::error_code
55 62x local_stream_acceptor::open(local_stream proto) noexcept
56 {
57
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 61 times.
62x if (is_open())
58 1x return {};
59 auto& svc =
60 61x static_cast<detail::local_stream_acceptor_service&>(h_.service());
61 122x auto ec = svc.open_acceptor_socket(
62 61x static_cast<local_stream_acceptor::implementation&>(*h_.get()),
63 proto.family(), proto.type(), proto.protocol());
64 61x return ec;
65 }
66
67 std::error_code
68 24x local_stream_acceptor::assign(native_handle_type fd) noexcept
69 {
70 auto& svc =
71 24x static_cast<detail::local_stream_acceptor_service&>(h_.service());
72 24x auto ec = svc.assign_socket(
73 24x static_cast<local_stream_acceptor::implementation&>(*h_.get()), fd);
74 24x return ec;
75 }
76
77 native_handle_type
78 8x local_stream_acceptor::native_handle() const noexcept
79 {
80
2/2
✓ Branch 3 → 4 taken 3 times.
✓ Branch 3 → 5 taken 5 times.
8x if (!is_open())
81 {
82 #if BOOST_COROSIO_HAS_IOCP
83 3x return static_cast<native_handle_type>(~0ull); // INVALID_SOCKET
84 #else
85 return -1;
86 #endif
87 }
88 5x return get().native_handle();
89 }
90
91 std::error_code
92 46x local_stream_acceptor::bind(
93 corosio::local_endpoint ep, bind_option opt) noexcept
94 {
95
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 45 times.
46x if (!is_open())
96 1x return make_error_code(std::errc::bad_file_descriptor);
97
98
6/8
✓ Branch 5 → 6 taken 7 times.
✓ Branch 5 → 11 taken 38 times.
✓ Branch 7 → 8 taken 7 times.
✗ Branch 7 → 11 not taken.
✓ Branch 9 → 10 taken 7 times.
✗ Branch 9 → 11 not taken.
✓ Branch 12 → 13 taken 7 times.
✓ Branch 12 → 19 taken 38 times.
45x if (opt == bind_option::unlink_existing && !ep.empty() && !ep.is_abstract())
99 {
100 // Best-effort removal; missing file is fine.
101 7x auto p = ep.path();
102 char buf[local_endpoint::max_path_length + 1];
103 7x std::memcpy(buf, p.data(), p.size());
104 7x buf[p.size()] = '\0';
105 #if BOOST_COROSIO_POSIX
106 ::unlink(buf);
107 #else
108 7x ::DeleteFileA(buf);
109 #endif
110 }
111
112 auto& svc =
113 45x static_cast<detail::local_stream_acceptor_service&>(h_.service());
114 45x return svc.bind_acceptor(
115 90x static_cast<local_stream_acceptor::implementation&>(*h_.get()), ep);
116 }
117
118 std::error_code
119 37x local_stream_acceptor::listen(int backlog) noexcept
120 {
121
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 36 times.
37x if (!is_open())
122 1x return make_error_code(std::errc::bad_file_descriptor);
123 auto& svc =
124 36x static_cast<detail::local_stream_acceptor_service&>(h_.service());
125 36x return svc.listen_acceptor(
126 36x static_cast<local_stream_acceptor::implementation&>(*h_.get()),
127 36x backlog);
128 }
129
130 void
131 99x local_stream_acceptor::close() noexcept
132 {
133
2/2
✓ Branch 3 → 4 taken 53 times.
✓ Branch 3 → 5 taken 46 times.
99x if (!is_open())
134 53x return;
135 46x h_.service().close(h_);
136 }
137
138 native_handle_type
139 7x local_stream_acceptor::release()
140 {
141
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 6 taken 6 times.
7x if (!is_open())
142 1x detail::throw_system_error(
143 1x make_error_code(std::errc::bad_file_descriptor),
144 "local_stream_acceptor::release");
145 6x return get().release_socket();
146 }
147
148 void
149 4x local_stream_acceptor::cancel() noexcept
150 {
151
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 3 times.
4x if (!is_open())
152 1x return;
153 3x get().cancel();
154 }
155
156 local_endpoint
157 6x local_stream_acceptor::local_endpoint() const noexcept
158 {
159
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 5 times.
6x if (!is_open())
160 1x return corosio::local_endpoint{};
161 5x return get().local_endpoint();
162 }
163
164 } // namespace boost::corosio
165