src/corosio/src/local_stream_socket.cpp

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