src/corosio/src/local_stream_socket.cpp

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