src/corosio/src/local_stream_acceptor.cpp

66.7% Lines (34/51) 66.7% List of functions (6/9)
local_stream_acceptor.cpp
f(x) Functions (9)
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/detail/platform.hpp>
11
12 #if BOOST_COROSIO_POSIX
13
14 #include <boost/corosio/local_stream_acceptor.hpp>
15 #include <boost/corosio/detail/except.hpp>
16 #include <boost/corosio/detail/local_stream_acceptor_service.hpp>
17
18 #include <unistd.h>
19
20 namespace boost::corosio {
21
22 12x local_stream_acceptor::~local_stream_acceptor()
23 {
24 12x close();
25 12x }
26
27 12x local_stream_acceptor::local_stream_acceptor(capy::execution_context& ctx)
28 12x : io_object(create_handle<detail::local_stream_acceptor_service>(ctx))
29 12x , ctx_(ctx)
30 {
31 12x }
32
33 void
34 12x local_stream_acceptor::open(local_stream proto)
35 {
36 12x if (is_open())
37 return;
38 auto& svc =
39 12x static_cast<detail::local_stream_acceptor_service&>(h_.service());
40 24x auto ec = svc.open_acceptor_socket(
41 12x static_cast<local_stream_acceptor::implementation&>(*h_.get()),
42 proto.family(), proto.type(), proto.protocol());
43 12x if (ec)
44 detail::throw_system_error(ec, "local_stream_acceptor::open");
45 }
46
47 std::error_code
48 12x local_stream_acceptor::bind(corosio::local_endpoint ep, bind_option opt)
49 {
50 12x if (!is_open())
51 detail::throw_logic_error("bind: acceptor not open");
52
53 16x if (opt == bind_option::unlink_existing &&
54 12x !ep.empty() && !ep.is_abstract())
55 {
56 // Best-effort removal; ENOENT is fine.
57 4x auto p = ep.path();
58 // path() is not null-terminated for the fixed buffer,
59 // so copy to a local array for unlink.
60 char buf[local_endpoint::max_path_length + 1];
61 4x std::memcpy(buf, p.data(), p.size());
62 4x buf[p.size()] = '\0';
63 4x ::unlink(buf);
64 }
65
66 auto& svc =
67 12x static_cast<detail::local_stream_acceptor_service&>(h_.service());
68 12x return svc.bind_acceptor(
69 12x static_cast<local_stream_acceptor::implementation&>(*h_.get()),
70 12x ep);
71 }
72
73 std::error_code
74 4x local_stream_acceptor::listen(int backlog)
75 {
76 4x if (!is_open())
77 detail::throw_logic_error("listen: acceptor not open");
78 auto& svc =
79 4x static_cast<detail::local_stream_acceptor_service&>(h_.service());
80 4x return svc.listen_acceptor(
81 4x static_cast<local_stream_acceptor::implementation&>(*h_.get()),
82 4x backlog);
83 }
84
85 void
86 12x local_stream_acceptor::close()
87 {
88 12x if (!is_open())
89 return;
90 12x h_.service().close(h_);
91 }
92
93 native_handle_type
94 local_stream_acceptor::release()
95 {
96 if (!is_open())
97 detail::throw_logic_error("release: acceptor not open");
98 return get().release_socket();
99 }
100
101 void
102 local_stream_acceptor::cancel()
103 {
104 if (!is_open())
105 return;
106 get().cancel();
107 }
108
109 local_endpoint
110 local_stream_acceptor::local_endpoint() const noexcept
111 {
112 if (!is_open())
113 return corosio::local_endpoint{};
114 return get().local_endpoint();
115 }
116
117 } // namespace boost::corosio
118
119 #endif // BOOST_COROSIO_POSIX
120