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