src/corosio/src/local_stream_acceptor.cpp

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