src/corosio/src/local_stream_socket.cpp

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