src/corosio/src/local_stream_socket.cpp

55.2% Lines (37/67) 64.3% List of functions (9/14)
local_stream_socket.cpp
f(x) Functions (14)
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_socket.hpp>
15 #include <boost/corosio/detail/except.hpp>
16 #include <boost/corosio/detail/local_stream_service.hpp>
17
18 #include <sys/ioctl.h>
19
20 namespace boost::corosio {
21
22 52x local_stream_socket::~local_stream_socket()
23 {
24 52x close();
25 52x }
26
27 30x local_stream_socket::local_stream_socket(capy::execution_context& ctx)
28 30x : io_object(create_handle<detail::local_stream_service>(ctx))
29 {
30 30x }
31
32 void
33 8x local_stream_socket::open(local_stream proto)
34 {
35 8x if (is_open())
36 return;
37 8x open_for_family(proto.family(), proto.type(), proto.protocol());
38 }
39
40 void
41 8x local_stream_socket::open_for_family(int family, int type, int protocol)
42 {
43 8x auto& svc = static_cast<detail::local_stream_service&>(h_.service());
44 8x std::error_code ec = svc.open_socket(
45 8x static_cast<local_stream_socket::implementation&>(*h_.get()),
46 family, type, protocol);
47 8x if (ec)
48 detail::throw_system_error(ec, "local_stream_socket::open");
49 8x }
50
51 void
52 54x local_stream_socket::close()
53 {
54 54x if (!is_open())
55 28x return;
56 26x h_.service().close(h_);
57 }
58
59 void
60 local_stream_socket::cancel()
61 {
62 if (!is_open())
63 return;
64 get().cancel();
65 }
66
67 void
68 local_stream_socket::shutdown(shutdown_type what)
69 {
70 if (is_open())
71 {
72 // Best-effort: errors like ENOTCONN are expected and unhelpful
73 [[maybe_unused]] auto ec = get().shutdown(what);
74 }
75 }
76
77 void
78 local_stream_socket::shutdown(shutdown_type what, std::error_code& ec) noexcept
79 {
80 ec = {};
81 if (is_open())
82 ec = get().shutdown(what);
83 }
84
85 void
86 16x local_stream_socket::assign(int fd)
87 {
88 16x if (is_open())
89 detail::throw_logic_error("assign: socket already open");
90 16x auto& svc = static_cast<detail::local_stream_service&>(h_.service());
91 16x std::error_code ec = svc.assign_socket(
92 16x static_cast<local_stream_socket::implementation&>(*h_.get()), fd);
93 16x if (ec)
94 detail::throw_system_error(ec, "local_stream_socket::assign");
95 16x }
96
97 native_handle_type
98 4x local_stream_socket::native_handle() const noexcept
99 {
100 4x if (!is_open())
101 return -1;
102 4x return get().native_handle();
103 }
104
105 native_handle_type
106 2x local_stream_socket::release()
107 {
108 2x if (!is_open())
109 detail::throw_logic_error("release: socket not open");
110 2x return get().release_socket();
111 }
112
113 std::size_t
114 4x local_stream_socket::available() const
115 {
116 4x if (!is_open())
117 detail::throw_logic_error("available: socket not open");
118 4x int value = 0;
119 4x if (::ioctl(native_handle(), FIONREAD, &value) < 0)
120 detail::throw_system_error(
121 std::error_code(errno, std::system_category()),
122 "local_stream_socket::available");
123 4x return static_cast<std::size_t>(value);
124 }
125
126 local_endpoint
127 local_stream_socket::local_endpoint() const noexcept
128 {
129 if (!is_open())
130 return corosio::local_endpoint{};
131 return get().local_endpoint();
132 }
133
134 local_endpoint
135 local_stream_socket::remote_endpoint() const noexcept
136 {
137 if (!is_open())
138 return corosio::local_endpoint{};
139 return get().remote_endpoint();
140 }
141
142 } // namespace boost::corosio
143
144 #endif // BOOST_COROSIO_POSIX
145