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 342x local_stream_socket::~local_stream_socket()
28 {
29 342x close();
30 342x }
31
32 284x local_stream_socket::local_stream_socket(capy::execution_context& ctx)
33 284x : io_object(create_handle<detail::local_stream_service>(ctx))
34 {
35 284x }
36
37 std::error_code
38 51x local_stream_socket::open(local_stream proto) noexcept
39 {
40 51x if (is_open())
41 return {};
42 51x return open_for_family(proto.family(), proto.type(), proto.protocol());
43 }
44
45 std::error_code
46 51x local_stream_socket::open_for_family(
47 int family, int type, int protocol) noexcept
48 {
49 51x auto& svc = static_cast<detail::local_stream_service&>(h_.service());
50 51x std::error_code ec = svc.open_socket(
51 51x static_cast<local_stream_socket::implementation&>(*h_.get()), family,
52 type, protocol);
53 51x return ec;
54 }
55
56 void
57 355x local_stream_socket::close() noexcept
58 {
59 355x if (!is_open())
60 133x return;
61 222x h_.service().close(h_);
62 }
63
64 void
65 8x local_stream_socket::cancel() noexcept
66 {
67 8x if (!is_open())
68 2x return;
69 6x get().cancel();
70 }
71
72 std::error_code
73 6x local_stream_socket::shutdown(shutdown_type what) noexcept
74 {
75 6x if (!is_open())
76 2x return make_error_code(std::errc::bad_file_descriptor);
77 4x return get().shutdown(what);
78 }
79
80 std::error_code
81 173x local_stream_socket::assign(native_handle_type fd) noexcept
82 {
83 173x auto& svc = static_cast<detail::local_stream_service&>(h_.service());
84 173x std::error_code ec = svc.assign_socket(
85 173x static_cast<local_stream_socket::implementation&>(*h_.get()), fd);
86 173x return ec;
87 }
88
89 native_handle_type
90 25x local_stream_socket::native_handle() const noexcept
91 {
92 25x if (!is_open())
93 #if BOOST_COROSIO_HAS_IOCP
94 return ~native_handle_type(0);
95 #else
96 2x return -1;
97 #endif
98 23x return get().native_handle();
99 }
100
101 native_handle_type
102 6x local_stream_socket::release()
103 {
104 6x if (!is_open())
105 2x detail::throw_system_error(
106 2x make_error_code(std::errc::bad_file_descriptor),
107 "local_stream_socket::release");
108 4x return get().release_socket();
109 }
110
111 std::size_t
112 11x local_stream_socket::available() const
113 {
114 11x if (!is_open())
115 2x detail::throw_system_error(
116 4x 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 9x int value = 0;
128 9x if (::ioctl(native_handle(), FIONREAD, &value) < 0)
129 5x detail::throw_system_error(
130 10x detail::make_err(errno), "local_stream_socket::available");
131 4x return static_cast<std::size_t>(value);
132 #endif
133 }
134
135 local_endpoint
136 6x local_stream_socket::local_endpoint() const noexcept
137 {
138 6x if (!is_open())
139 2x return corosio::local_endpoint{};
140 4x return get().local_endpoint();
141 }
142
143 local_endpoint
144 6x local_stream_socket::remote_endpoint() const noexcept
145 {
146 6x if (!is_open())
147 2x return corosio::local_endpoint{};
148 4x return get().remote_endpoint();
149 }
150
151 } // namespace boost::corosio
152
153 #endif // BOOST_COROSIO_POSIX || BOOST_COROSIO_HAS_IOCP
154