src/corosio/src/local_stream_acceptor.cpp

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