src/corosio/src/local_stream_socket.cpp

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