src/corosio/src/local_stream_socket.cpp
94.8% Lines (73/77)
100.0% List of functions (14/14)
86.7% Branches (26/30)
Functions (14)
Function
Calls
Lines
Branches
Blocks
boost::corosio::local_stream_socket::~local_stream_socket()
:26
228x
100.0%
50.0%
100.0%
boost::corosio::local_stream_socket::local_stream_socket(boost::capy::execution_context&)
:31
92x
100.0%
–
100.0%
boost::corosio::local_stream_socket::open(boost::corosio::local_stream)
:37
38x
80.0%
50.0%
75.0%
boost::corosio::local_stream_socket::open_for_family(int, int, int)
:45
38x
87.5%
50.0%
66.0%
boost::corosio::local_stream_socket::close()
:56
138x
100.0%
100.0%
100.0%
boost::corosio::local_stream_socket::cancel()
:64
4x
100.0%
100.0%
100.0%
boost::corosio::local_stream_socket::shutdown(boost::corosio::shutdown_type)
:72
4x
100.0%
100.0%
100.0%
boost::corosio::local_stream_socket::shutdown(boost::corosio::shutdown_type, std::__1::error_code&)
:82
4x
100.0%
100.0%
100.0%
boost::corosio::local_stream_socket::assign(int)
:90
44x
100.0%
100.0%
100.0%
boost::corosio::local_stream_socket::native_handle() const
:102
8x
100.0%
100.0%
100.0%
boost::corosio::local_stream_socket::release()
:114
4x
100.0%
100.0%
100.0%
boost::corosio::local_stream_socket::available() const
:122
6x
75.0%
75.0%
80.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/detail/except.hpp> | |||
| 16 | #include <boost/corosio/detail/local_stream_service.hpp> | |||
| 17 | ||||
| 18 | #if BOOST_COROSIO_POSIX | |||
| 19 | #include <sys/ioctl.h> | |||
| 20 | #elif BOOST_COROSIO_HAS_IOCP | |||
| 21 | #include <boost/corosio/native/detail/iocp/win_windows.hpp> | |||
| 22 | #endif | |||
| 23 | ||||
| 24 | namespace boost::corosio { | |||
| 25 | ||||
| 26 | 228x | local_stream_socket::~local_stream_socket() | ||
| 27 | 228x | { | ||
| 28 |
1/2✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
|
130x | close(); | |
| 29 | 228x | } | ||
| 30 | ||||
| 31 | 184x | local_stream_socket::local_stream_socket(capy::execution_context& ctx) | ||
| 32 | 92x | : io_object(create_handle<detail::local_stream_service>(ctx)) | ||
| 33 | 184x | { | ||
| 34 | 92x | } | ||
| 35 | ||||
| 36 | void | |||
| 37 | 38x | local_stream_socket::open(local_stream proto) | ||
| 38 | { | |||
| 39 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
|
38x | if (is_open()) | |
| 40 | ✗ | return; | ||
| 41 | 38x | open_for_family(proto.family(), proto.type(), proto.protocol()); | ||
| 42 | 38x | } | ||
| 43 | ||||
| 44 | void | |||
| 45 | 38x | local_stream_socket::open_for_family(int family, int type, int protocol) | ||
| 46 | { | |||
| 47 | 38x | auto& svc = static_cast<detail::local_stream_service&>(h_.service()); | ||
| 48 | 76x | std::error_code ec = svc.open_socket( | ||
| 49 | 38x | static_cast<local_stream_socket::implementation&>(*h_.get()), | ||
| 50 | 38x | family, type, protocol); | ||
| 51 |
1/2✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
|
38x | if (ec) | |
| 52 | ✗ | detail::throw_system_error(ec, "local_stream_socket::open"); | ||
| 53 | 38x | } | ||
| 54 | ||||
| 55 | void | |||
| 56 | 138x | local_stream_socket::close() | ||
| 57 | { | |||
| 58 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 92 times.
|
138x | if (!is_open()) | |
| 59 | 46x | return; | ||
| 60 | 92x | h_.service().close(h_); | ||
| 61 | 138x | } | ||
| 62 | ||||
| 63 | void | |||
| 64 | 4x | local_stream_socket::cancel() | ||
| 65 | { | |||
| 66 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4x | if (!is_open()) | |
| 67 | 2x | return; | ||
| 68 | 2x | get().cancel(); | ||
| 69 | 4x | } | ||
| 70 | ||||
| 71 | void | |||
| 72 | 4x | local_stream_socket::shutdown(shutdown_type what) | ||
| 73 | { | |||
| 74 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4x | if (is_open()) | |
| 75 | { | |||
| 76 | // Best-effort: errors like ENOTCONN are expected and unhelpful | |||
| 77 | 2x | [[maybe_unused]] auto ec = get().shutdown(what); | ||
| 78 | 2x | } | ||
| 79 | 4x | } | ||
| 80 | ||||
| 81 | void | |||
| 82 | 4x | local_stream_socket::shutdown(shutdown_type what, std::error_code& ec) noexcept | ||
| 83 | { | |||
| 84 | 4x | ec = {}; | ||
| 85 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4x | if (is_open()) | |
| 86 | 2x | ec = get().shutdown(what); | ||
| 87 | 4x | } | ||
| 88 | ||||
| 89 | void | |||
| 90 | 44x | local_stream_socket::assign(native_handle_type fd) | ||
| 91 | { | |||
| 92 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 2 times.
|
44x | if (is_open()) | |
| 93 | 2x | detail::throw_logic_error("assign: socket already open"); | ||
| 94 | 42x | auto& svc = static_cast<detail::local_stream_service&>(h_.service()); | ||
| 95 | 84x | std::error_code ec = svc.assign_socket( | ||
| 96 | 42x | static_cast<local_stream_socket::implementation&>(*h_.get()), fd); | ||
| 97 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 2 times.
|
42x | if (ec) | |
| 98 | 2x | detail::throw_system_error(ec, "local_stream_socket::assign"); | ||
| 99 | 40x | } | ||
| 100 | ||||
| 101 | native_handle_type | |||
| 102 | 8x | local_stream_socket::native_handle() const noexcept | ||
| 103 | { | |||
| 104 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
|
8x | if (!is_open()) | |
| 105 | #if BOOST_COROSIO_HAS_IOCP | |||
| 106 | return ~native_handle_type(0); | |||
| 107 | #else | |||
| 108 | 2x | return -1; | ||
| 109 | #endif | |||
| 110 | 6x | return get().native_handle(); | ||
| 111 | 8x | } | ||
| 112 | ||||
| 113 | native_handle_type | |||
| 114 | 4x | local_stream_socket::release() | ||
| 115 | { | |||
| 116 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4x | if (!is_open()) | |
| 117 | 2x | detail::throw_logic_error("release: socket not open"); | ||
| 118 | 2x | return get().release_socket(); | ||
| 119 | } | |||
| 120 | ||||
| 121 | std::size_t | |||
| 122 | 6x | local_stream_socket::available() const | ||
| 123 | { | |||
| 124 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
6x | if (!is_open()) | |
| 125 | 2x | detail::throw_logic_error("available: socket not open"); | ||
| 126 | #if BOOST_COROSIO_HAS_IOCP | |||
| 127 | u_long value = 0; | |||
| 128 | if (::ioctlsocket( | |||
| 129 | static_cast<SOCKET>(native_handle()), FIONREAD, &value) != 0) | |||
| 130 | detail::throw_system_error( | |||
| 131 | std::error_code(::WSAGetLastError(), std::system_category()), | |||
| 132 | "local_stream_socket::available"); | |||
| 133 | return static_cast<std::size_t>(value); | |||
| 134 | #else | |||
| 135 | 4x | int value = 0; | ||
| 136 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4x | if (::ioctl(native_handle(), FIONREAD, &value) < 0) | |
| 137 | ✗ | detail::throw_system_error( | ||
| 138 | ✗ | std::error_code(errno, std::system_category()), | ||
| 139 | "local_stream_socket::available"); | |||
| 140 | 4x | return static_cast<std::size_t>(value); | ||
| 141 | #endif | |||
| 142 | } | |||
| 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 |