src/corosio/src/local_stream_acceptor.cpp

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