src/corosio/src/local_stream_socket.cpp
98.6% Lines (73 / 74)
77.8% Branches (28 / 36)
100.0% Functions (13 / 13)
Functions (13)
Function
Calls
Lines
Branches
Blocks
boost::corosio::local_stream_socket::~local_stream_socket()
:33
758x
100.0%
–
100.0%
boost::corosio::local_stream_socket::local_stream_socket(boost::capy::execution_context&)
:38
342x
100.0%
–
100.0%
boost::corosio::local_stream_socket::open()
:48
53x
80.0%
50.0%
75.0%
boost::corosio::local_stream_socket::open_for_family(int, int, int)
:56
53x
100.0%
50.0%
66.0%
boost::corosio::local_stream_socket::close()
:67
422x
100.0%
75.0%
80.0%
boost::corosio::local_stream_socket::cancel()
:75
10x
100.0%
100.0%
100.0%
boost::corosio::local_stream_socket::shutdown(boost::corosio::shutdown_type)
:83
6x
100.0%
100.0%
100.0%
boost::corosio::local_stream_socket::assign(int)
:91
176x
100.0%
66.7%
83.0%
boost::corosio::local_stream_socket::native_handle() const
:102
32x
100.0%
100.0%
100.0%
boost::corosio::local_stream_socket::release()
:114
8x
100.0%
100.0%
100.0%
boost::corosio::local_stream_socket::available() const
:124
8x
100.0%
75.0%
88.0%
boost::corosio::local_stream_socket::local_endpoint() const
:145
6x
100.0%
100.0%
100.0%
boost::corosio::local_stream_socket::remote_endpoint() const
:153
6x
100.0%
100.0%
100.0%
| 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 | 758x | local_stream_socket::~local_stream_socket() | ||
| 34 | 758x | { | ||
| 35 | 404x | close(); | ||
| 36 | 758x | } | ||
| 37 | ||||
| 38 | 684x | local_stream_socket::local_stream_socket(capy::execution_context& ctx) | ||
| 39 | 342x | : io_object(handle( | ||
| 40 | 342x | ctx, | ||
| 41 | 342x | detail::use_backend_service< | ||
| 42 | detail::local_stream_service_of, | |||
| 43 | 342x | detail::local_stream_service>(ctx))) | ||
| 44 | 684x | { | ||
| 45 | 342x | } | ||
| 46 | ||||
| 47 | std::error_code | |||
| 48 | 53x | local_stream_socket::open() noexcept | ||
| 49 | { | |||
| 50 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
|
53x | if (is_open()) | |
| 51 | ✗ | return {}; | ||
| 52 | 53x | return open_for_family(AF_UNIX, SOCK_STREAM, 0); | ||
| 53 | 53x | } | ||
| 54 | ||||
| 55 | std::error_code | |||
| 56 | 53x | local_stream_socket::open_for_family( | ||
| 57 | int family, int type, int protocol) noexcept | |||
| 58 | { | |||
| 59 | 53x | auto& svc = static_cast<detail::local_stream_service&>(h_.service()); | ||
| 60 |
2/4✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 53 times.
✗ Branch 3 not taken.
|
106x | std::error_code ec = svc.open_socket( | |
| 61 | 53x | static_cast<local_stream_socket::implementation&>(*h_.get()), family, | ||
| 62 | 53x | type, protocol); | ||
| 63 | 53x | return ec; | ||
| 64 | } | |||
| 65 | ||||
| 66 | void | |||
| 67 | 422x | local_stream_socket::close() noexcept | ||
| 68 | { | |||
| 69 |
2/2✓ Branch 0 taken 231 times.
✓ Branch 1 taken 191 times.
|
422x | if (!is_open()) | |
| 70 | 191x | return; | ||
| 71 |
1/2✓ Branch 0 taken 231 times.
✗ Branch 1 not taken.
|
231x | h_.service().close(h_); | |
| 72 | 422x | } | ||
| 73 | ||||
| 74 | void | |||
| 75 | 10x | local_stream_socket::cancel() noexcept | ||
| 76 | { | |||
| 77 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
|
10x | if (!is_open()) | |
| 78 | 2x | return; | ||
| 79 | 8x | get().cancel(); | ||
| 80 | 10x | } | ||
| 81 | ||||
| 82 | std::error_code | |||
| 83 | 6x | local_stream_socket::shutdown(shutdown_type what) noexcept | ||
| 84 | { | |||
| 85 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6x | if (!is_open()) | |
| 86 | 2x | return make_error_code(std::errc::bad_file_descriptor); | ||
| 87 | 4x | return get().shutdown(what); | ||
| 88 | 6x | } | ||
| 89 | ||||
| 90 | std::error_code | |||
| 91 | 176x | local_stream_socket::assign(native_handle_type fd) noexcept | ||
| 92 | { | |||
| 93 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 168 times.
|
176x | if (is_open()) | |
| 94 | 8x | return make_error_code(error::already_open); | ||
| 95 | 168x | auto& svc = static_cast<detail::local_stream_service&>(h_.service()); | ||
| 96 |
2/4✓ Branch 0 taken 168 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 168 times.
✗ Branch 3 not taken.
|
336x | std::error_code ec = svc.assign_socket( | |
| 97 | 168x | static_cast<local_stream_socket::implementation&>(*h_.get()), fd); | ||
| 98 | 168x | return ec; | ||
| 99 | 176x | } | ||
| 100 | ||||
| 101 | native_handle_type | |||
| 102 | 32x | local_stream_socket::native_handle() const noexcept | ||
| 103 | { | |||
| 104 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 30 times.
|
32x | if (!is_open()) | |
| 105 | #if BOOST_COROSIO_HAS_IOCP | |||
| 106 | return ~native_handle_type(0); | |||
| 107 | #else | |||
| 108 | 2x | return -1; | ||
| 109 | #endif | |||
| 110 | 30x | return get().native_handle(); | ||
| 111 | 32x | } | ||
| 112 | ||||
| 113 | capy::results<native_handle_type> | |||
| 114 | 8x | local_stream_socket::release() noexcept | ||
| 115 | { | |||
| 116 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
|
8x | if (!is_open()) | |
| 117 | 2x | return { | ||
| 118 | 2x | make_error_code(std::errc::bad_file_descriptor), | ||
| 119 | detail::invalid_native_handle}; | |||
| 120 | 6x | return {std::error_code{}, get().release_socket()}; | ||
| 121 | 8x | } | ||
| 122 | ||||
| 123 | capy::results<std::size_t> | |||
| 124 | 8x | local_stream_socket::available() const noexcept | ||
| 125 | { | |||
| 126 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
|
8x | if (!is_open()) | |
| 127 | 2x | return {make_error_code(std::errc::bad_file_descriptor), 0}; | ||
| 128 | #if BOOST_COROSIO_HAS_IOCP | |||
| 129 | u_long value = 0; | |||
| 130 | if (::ioctlsocket(static_cast<SOCKET>(native_handle()), FIONREAD, &value) != | |||
| 131 | 0) | |||
| 132 | return { | |||
| 133 | detail::make_err(static_cast<unsigned long>(::WSAGetLastError())), | |||
| 134 | 0}; | |||
| 135 | return {std::error_code{}, static_cast<std::size_t>(value)}; | |||
| 136 | #else | |||
| 137 | 6x | int value = 0; | ||
| 138 |
3/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 4 times.
|
6x | if (::ioctl(native_handle(), FIONREAD, &value) < 0) | |
| 139 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2x | return {detail::make_err(errno), 0}; | |
| 140 | 4x | return {std::error_code{}, static_cast<std::size_t>(value)}; | ||
| 141 | #endif | |||
| 142 | 8x | } | ||
| 143 | ||||
| 144 | local_endpoint | |||
| 145 | 6x | local_stream_socket::local_endpoint() const noexcept | ||
| 146 | { | |||
| 147 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6x | if (!is_open()) | |
| 148 | 2x | return corosio::local_endpoint{}; | ||
| 149 | 4x | return get().local_endpoint(); | ||
| 150 | 6x | } | ||
| 151 | ||||
| 152 | local_endpoint | |||
| 153 | 6x | local_stream_socket::remote_endpoint() const noexcept | ||
| 154 | { | |||
| 155 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6x | if (!is_open()) | |
| 156 | 2x | return corosio::local_endpoint{}; | ||
| 157 | 4x | return get().remote_endpoint(); | ||
| 158 | 6x | } | ||
| 159 | ||||
| 160 | } // namespace boost::corosio | |||
| 161 | ||||
| 162 | #endif // BOOST_COROSIO_POSIX || BOOST_COROSIO_HAS_IOCP | |||
| 163 |