src/corosio/src/local_stream_socket.cpp

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