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