src/corosio/src/local_stream_socket.cpp

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