src/corosio/src/local_stream_socket.cpp

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