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