include/boost/corosio/native/native_local_datagram_socket.hpp
100.0% Lines (133/0/133)
100.0% List of functions (70/0/70)
Functions (70)
Function
Calls
Lines
Blocks
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::get_impl()
:76
16x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::get_impl()
:76
16x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_send_to_awaitable<boost::capy::const_buffer>::native_send_to_awaitable(boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>&, boost::capy::const_buffer, boost::corosio::local_endpoint, int)
:92
4x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_send_to_awaitable<boost::capy::const_buffer>::native_send_to_awaitable(boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>&, boost::capy::const_buffer, boost::corosio::local_endpoint, int)
:92
4x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_send_to_awaitable<boost::capy::const_buffer>::await_ready() const
:104
4x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_send_to_awaitable<boost::capy::const_buffer>::await_ready() const
:104
4x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_send_to_awaitable<boost::capy::const_buffer>::await_resume() const
:111
4x
100.0%
86.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_send_to_awaitable<boost::capy::const_buffer>::await_resume() const
:111
4x
100.0%
86.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_send_to_awaitable<boost::capy::const_buffer>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*)
:118
3x
100.0%
77.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_send_to_awaitable<boost::capy::const_buffer>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*)
:118
3x
100.0%
77.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_recv_from_awaitable<boost::capy::mutable_buffer>::native_recv_from_awaitable(boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>&, boost::capy::mutable_buffer, boost::corosio::local_endpoint&, int)
:139
3x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_recv_from_awaitable<boost::capy::mutable_buffer>::native_recv_from_awaitable(boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>&, boost::capy::mutable_buffer, boost::corosio::local_endpoint&, int)
:139
3x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_recv_from_awaitable<boost::capy::mutable_buffer>::await_ready() const
:151
3x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_recv_from_awaitable<boost::capy::mutable_buffer>::await_ready() const
:151
3x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_recv_from_awaitable<boost::capy::mutable_buffer>::await_resume() const
:158
3x
100.0%
86.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_recv_from_awaitable<boost::capy::mutable_buffer>::await_resume() const
:158
3x
100.0%
86.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_recv_from_awaitable<boost::capy::mutable_buffer>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*)
:165
2x
100.0%
77.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_recv_from_awaitable<boost::capy::mutable_buffer>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*)
:165
2x
100.0%
77.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_wait_awaitable::native_wait_awaitable(boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>&, boost::corosio::wait_type)
:182
2x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_wait_awaitable::native_wait_awaitable(boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>&, boost::corosio::wait_type)
:182
2x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_wait_awaitable::await_ready() const
:189
2x
100.0%
88.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_wait_awaitable::await_ready() const
:189
2x
100.0%
88.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_wait_awaitable::await_resume() const
:196
2x
100.0%
86.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_wait_awaitable::await_resume() const
:196
2x
100.0%
86.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_wait_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*)
:203
2x
100.0%
82.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_wait_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*)
:203
2x
100.0%
82.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_connect_awaitable::native_connect_awaitable(boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>&, boost::corosio::local_endpoint)
:218
5x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_connect_awaitable::native_connect_awaitable(boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>&, boost::corosio::local_endpoint)
:218
5x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_connect_awaitable::await_ready() const
:226
5x
100.0%
88.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_connect_awaitable::await_ready() const
:226
5x
100.0%
88.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_connect_awaitable::await_resume() const
:233
5x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_connect_awaitable::await_resume() const
:233
5x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_connect_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*)
:240
5x
100.0%
82.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_connect_awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*)
:240
5x
100.0%
82.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_send_awaitable<boost::capy::const_buffer>::native_send_awaitable(boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>&, boost::capy::const_buffer, int)
:259
3x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_send_awaitable<boost::capy::const_buffer>::native_send_awaitable(boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>&, boost::capy::const_buffer, int)
:259
3x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_send_awaitable<boost::capy::const_buffer>::await_ready() const
:269
3x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_send_awaitable<boost::capy::const_buffer>::await_ready() const
:269
3x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_send_awaitable<boost::capy::const_buffer>::await_resume() const
:276
3x
100.0%
86.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_send_awaitable<boost::capy::const_buffer>::await_resume() const
:276
3x
100.0%
86.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_send_awaitable<boost::capy::const_buffer>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*)
:283
2x
100.0%
77.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_send_awaitable<boost::capy::const_buffer>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*)
:283
2x
100.0%
77.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_recv_awaitable<boost::capy::mutable_buffer>::native_recv_awaitable(boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>&, boost::capy::mutable_buffer, int)
:303
3x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_recv_awaitable<boost::capy::mutable_buffer>::native_recv_awaitable(boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>&, boost::capy::mutable_buffer, int)
:303
3x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_recv_awaitable<boost::capy::mutable_buffer>::await_ready() const
:313
3x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_recv_awaitable<boost::capy::mutable_buffer>::await_ready() const
:313
3x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_recv_awaitable<boost::capy::mutable_buffer>::await_resume() const
:320
3x
100.0%
86.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_recv_awaitable<boost::capy::mutable_buffer>::await_resume() const
:320
3x
100.0%
86.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_recv_awaitable<boost::capy::mutable_buffer>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*)
:327
2x
100.0%
77.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_recv_awaitable<boost::capy::mutable_buffer>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*)
:327
2x
100.0%
77.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::native_local_datagram_socket(boost::capy::execution_context&)
:342
15x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::native_local_datagram_socket(boost::capy::execution_context&)
:342
15x
100.0%
100.0%
auto boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::send_to<boost::capy::const_buffer>(boost::capy::const_buffer const&, boost::corosio::local_endpoint, boost::corosio::message_flags)
:379
4x
100.0%
100.0%
auto boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::send_to<boost::capy::const_buffer>(boost::capy::const_buffer const&, boost::corosio::local_endpoint, boost::corosio::message_flags)
:379
4x
100.0%
100.0%
auto boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::send_to<boost::capy::const_buffer>(boost::capy::const_buffer const&, boost::corosio::local_endpoint)
:393
4x
100.0%
100.0%
auto boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::send_to<boost::capy::const_buffer>(boost::capy::const_buffer const&, boost::corosio::local_endpoint)
:393
4x
100.0%
100.0%
auto boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::recv_from<boost::capy::mutable_buffer>(boost::capy::mutable_buffer const&, boost::corosio::local_endpoint&, boost::corosio::message_flags)
:404
3x
100.0%
100.0%
auto boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::recv_from<boost::capy::mutable_buffer>(boost::capy::mutable_buffer const&, boost::corosio::local_endpoint&, boost::corosio::message_flags)
:404
3x
100.0%
100.0%
auto boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::recv_from<boost::capy::mutable_buffer>(boost::capy::mutable_buffer const&, boost::corosio::local_endpoint&)
:419
3x
100.0%
100.0%
auto boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::recv_from<boost::capy::mutable_buffer>(boost::capy::mutable_buffer const&, boost::corosio::local_endpoint&)
:419
3x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::connect(boost::corosio::local_endpoint)
:431
5x
100.0%
100.0%
boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::connect(boost::corosio::local_endpoint)
:431
5x
100.0%
100.0%
auto boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::send<boost::capy::const_buffer>(boost::capy::const_buffer const&, boost::corosio::message_flags)
:445
3x
100.0%
100.0%
auto boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::send<boost::capy::const_buffer>(boost::capy::const_buffer const&, boost::corosio::message_flags)
:445
3x
100.0%
100.0%
auto boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::send<boost::capy::const_buffer>(boost::capy::const_buffer const&)
:455
3x
100.0%
100.0%
auto boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::send<boost::capy::const_buffer>(boost::capy::const_buffer const&)
:455
3x
100.0%
100.0%
auto boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::recv<boost::capy::mutable_buffer>(boost::capy::mutable_buffer const&, boost::corosio::message_flags)
:466
3x
100.0%
100.0%
auto boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::recv<boost::capy::mutable_buffer>(boost::capy::mutable_buffer const&, boost::corosio::message_flags)
:466
3x
100.0%
100.0%
auto boost::corosio::native_local_datagram_socket<boost::corosio::epoll_t{}>::recv<boost::capy::mutable_buffer>(boost::capy::mutable_buffer const&)
:476
3x
100.0%
100.0%
auto boost::corosio::native_local_datagram_socket<boost::corosio::select_t{}>::recv<boost::capy::mutable_buffer>(boost::capy::mutable_buffer const&)
:476
3x
100.0%
100.0%
| Line | TLA | Hits | Source Code |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2026 Steve Gerbino | ||
| 3 | // Copyright (c) 2026 Michael Vandeberg | ||
| 4 | // | ||
| 5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
| 6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
| 7 | // | ||
| 8 | // Official repository: https://github.com/cppalliance/corosio | ||
| 9 | // | ||
| 10 | |||
| 11 | #ifndef BOOST_COROSIO_NATIVE_NATIVE_LOCAL_DATAGRAM_SOCKET_HPP | ||
| 12 | #define BOOST_COROSIO_NATIVE_NATIVE_LOCAL_DATAGRAM_SOCKET_HPP | ||
| 13 | |||
| 14 | #include <boost/corosio/detail/platform.hpp> | ||
| 15 | |||
| 16 | #if BOOST_COROSIO_POSIX | ||
| 17 | |||
| 18 | #include <boost/corosio/local_datagram_socket.hpp> | ||
| 19 | #include <boost/corosio/backend.hpp> | ||
| 20 | |||
| 21 | #ifndef BOOST_COROSIO_MRDOCS | ||
| 22 | #if BOOST_COROSIO_HAS_EPOLL | ||
| 23 | #include <boost/corosio/native/detail/epoll/epoll_types.hpp> | ||
| 24 | #endif | ||
| 25 | |||
| 26 | #if BOOST_COROSIO_HAS_SELECT | ||
| 27 | #include <boost/corosio/native/detail/select/select_types.hpp> | ||
| 28 | #endif | ||
| 29 | |||
| 30 | #if BOOST_COROSIO_HAS_KQUEUE | ||
| 31 | #include <boost/corosio/native/detail/kqueue/kqueue_types.hpp> | ||
| 32 | #endif | ||
| 33 | |||
| 34 | #if BOOST_COROSIO_HAS_URING | ||
| 35 | #include <boost/corosio/native/detail/uring/uring_types.hpp> | ||
| 36 | #endif | ||
| 37 | #endif // !BOOST_COROSIO_MRDOCS | ||
| 38 | |||
| 39 | namespace boost::corosio { | ||
| 40 | |||
| 41 | /** An asynchronous Unix datagram socket with devirtualized I/O. | ||
| 42 | |||
| 43 | This class template inherits from @ref local_datagram_socket | ||
| 44 | and shadows the async operations (`send_to`, `recv_from`, | ||
| 45 | `connect`, `send`, `recv`) with versions that call the backend | ||
| 46 | implementation directly, allowing the compiler to inline | ||
| 47 | through the entire call chain. | ||
| 48 | |||
| 49 | Non-async operations (`open`, `close`, `cancel`, `bind`, | ||
| 50 | socket options) remain unchanged and dispatch through the | ||
| 51 | compiled library. | ||
| 52 | |||
| 53 | A `native_local_datagram_socket` IS-A `local_datagram_socket` | ||
| 54 | and can be passed to any function expecting | ||
| 55 | `local_datagram_socket&`, in which case virtual dispatch is | ||
| 56 | used transparently. | ||
| 57 | |||
| 58 | @tparam Backend A backend tag value (e.g., `epoll`) whose type | ||
| 59 | provides the concrete implementation types. | ||
| 60 | |||
| 61 | @par Thread Safety | ||
| 62 | Same as @ref local_datagram_socket. | ||
| 63 | |||
| 64 | @par Example | ||
| 65 | @par !example open_bind_recv | ||
| 66 | |||
| 67 | @see local_datagram_socket, epoll_t, iocp_t | ||
| 68 | */ | ||
| 69 | template<auto Backend> | ||
| 70 | class native_local_datagram_socket : public local_datagram_socket | ||
| 71 | { | ||
| 72 | using backend_type = decltype(Backend); | ||
| 73 | using impl_type = typename backend_type::local_datagram_socket_type; | ||
| 74 | using service_type = typename backend_type::local_datagram_service_type; | ||
| 75 | |||
| 76 | 32x | impl_type& get_impl() noexcept | |
| 77 | { | ||
| 78 | 32x | return *static_cast<impl_type*>(h_.get()); | |
| 79 | } | ||
| 80 | |||
| 81 | template<class ConstBufferSequence> | ||
| 82 | struct native_send_to_awaitable | ||
| 83 | { | ||
| 84 | native_local_datagram_socket& self_; | ||
| 85 | ConstBufferSequence buffers_; | ||
| 86 | corosio::local_endpoint dest_; | ||
| 87 | int flags_; | ||
| 88 | std::stop_token token_; | ||
| 89 | mutable std::error_code ec_; | ||
| 90 | mutable std::size_t bytes_transferred_ = 0; | ||
| 91 | |||
| 92 | 8x | native_send_to_awaitable( | |
| 93 | native_local_datagram_socket& self, | ||
| 94 | ConstBufferSequence buffers, | ||
| 95 | corosio::local_endpoint dest, | ||
| 96 | int flags) noexcept | ||
| 97 | 8x | : self_(self) | |
| 98 | 8x | , buffers_(std::move(buffers)) | |
| 99 | 8x | , dest_(dest) | |
| 100 | 8x | , flags_(flags) | |
| 101 | { | ||
| 102 | 8x | } | |
| 103 | |||
| 104 | 8x | bool await_ready() const noexcept | |
| 105 | { | ||
| 106 | // A pre-set ec_ means the initiator failed before | ||
| 107 | // dispatch (e.g. a closed object). | ||
| 108 | 8x | return static_cast<bool>(ec_) || token_.stop_requested(); | |
| 109 | } | ||
| 110 | |||
| 111 | 8x | [[nodiscard]] capy::io_result<std::size_t> await_resume() const noexcept | |
| 112 | { | ||
| 113 | 8x | if (token_.stop_requested()) | |
| 114 | 2x | return {make_error_code(std::errc::operation_canceled), 0}; | |
| 115 | 6x | return {ec_, bytes_transferred_}; | |
| 116 | } | ||
| 117 | |||
| 118 | 6x | auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env) | |
| 119 | -> std::coroutine_handle<> | ||
| 120 | { | ||
| 121 | 6x | token_ = env->stop_token; | |
| 122 | 18x | return self_.get_impl().send_to( | |
| 123 | 6x | h, env->executor, buffers_, dest_, flags_, token_, &ec_, | |
| 124 | 12x | &bytes_transferred_); | |
| 125 | } | ||
| 126 | }; | ||
| 127 | |||
| 128 | template<class MutableBufferSequence> | ||
| 129 | struct native_recv_from_awaitable | ||
| 130 | { | ||
| 131 | native_local_datagram_socket& self_; | ||
| 132 | MutableBufferSequence buffers_; | ||
| 133 | corosio::local_endpoint& source_; | ||
| 134 | int flags_; | ||
| 135 | std::stop_token token_; | ||
| 136 | mutable std::error_code ec_; | ||
| 137 | mutable std::size_t bytes_transferred_ = 0; | ||
| 138 | |||
| 139 | 6x | native_recv_from_awaitable( | |
| 140 | native_local_datagram_socket& self, | ||
| 141 | MutableBufferSequence buffers, | ||
| 142 | corosio::local_endpoint& source, | ||
| 143 | int flags) noexcept | ||
| 144 | 6x | : self_(self) | |
| 145 | 6x | , buffers_(std::move(buffers)) | |
| 146 | 6x | , source_(source) | |
| 147 | 6x | , flags_(flags) | |
| 148 | { | ||
| 149 | 6x | } | |
| 150 | |||
| 151 | 6x | bool await_ready() const noexcept | |
| 152 | { | ||
| 153 | // A pre-set ec_ means the initiator failed before | ||
| 154 | // dispatch (e.g. a closed object). | ||
| 155 | 6x | return static_cast<bool>(ec_) || token_.stop_requested(); | |
| 156 | } | ||
| 157 | |||
| 158 | 6x | [[nodiscard]] capy::io_result<std::size_t> await_resume() const noexcept | |
| 159 | { | ||
| 160 | 6x | if (token_.stop_requested()) | |
| 161 | 2x | return {make_error_code(std::errc::operation_canceled), 0}; | |
| 162 | 4x | return {ec_, bytes_transferred_}; | |
| 163 | } | ||
| 164 | |||
| 165 | 4x | auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env) | |
| 166 | -> std::coroutine_handle<> | ||
| 167 | { | ||
| 168 | 4x | token_ = env->stop_token; | |
| 169 | 12x | return self_.get_impl().recv_from( | |
| 170 | 4x | h, env->executor, buffers_, &source_, flags_, token_, &ec_, | |
| 171 | 8x | &bytes_transferred_); | |
| 172 | } | ||
| 173 | }; | ||
| 174 | |||
| 175 | struct native_wait_awaitable | ||
| 176 | { | ||
| 177 | native_local_datagram_socket& self_; | ||
| 178 | wait_type w_; | ||
| 179 | std::stop_token token_; | ||
| 180 | mutable std::error_code ec_; | ||
| 181 | |||
| 182 | 4x | native_wait_awaitable( | |
| 183 | native_local_datagram_socket& self, wait_type w) noexcept | ||
| 184 | 4x | : self_(self) | |
| 185 | 4x | , w_(w) | |
| 186 | { | ||
| 187 | 4x | } | |
| 188 | |||
| 189 | 4x | bool await_ready() const noexcept | |
| 190 | { | ||
| 191 | // A pre-set ec_ means the initiator failed before | ||
| 192 | // dispatch (e.g. auto-open). | ||
| 193 | 4x | return static_cast<bool>(ec_) || token_.stop_requested(); | |
| 194 | } | ||
| 195 | |||
| 196 | 4x | [[nodiscard]] capy::io_result<> await_resume() const noexcept | |
| 197 | { | ||
| 198 | 4x | if (token_.stop_requested()) | |
| 199 | 2x | return {make_error_code(std::errc::operation_canceled)}; | |
| 200 | 2x | return {ec_}; | |
| 201 | } | ||
| 202 | |||
| 203 | 4x | auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env) | |
| 204 | -> std::coroutine_handle<> | ||
| 205 | { | ||
| 206 | 4x | token_ = env->stop_token; | |
| 207 | 4x | return self_.get_impl().wait(h, env->executor, w_, token_, &ec_); | |
| 208 | } | ||
| 209 | }; | ||
| 210 | |||
| 211 | struct native_connect_awaitable | ||
| 212 | { | ||
| 213 | native_local_datagram_socket& self_; | ||
| 214 | corosio::local_endpoint endpoint_; | ||
| 215 | std::stop_token token_; | ||
| 216 | mutable std::error_code ec_; | ||
| 217 | |||
| 218 | 10x | native_connect_awaitable( | |
| 219 | native_local_datagram_socket& self, | ||
| 220 | corosio::local_endpoint ep) noexcept | ||
| 221 | 10x | : self_(self) | |
| 222 | 10x | , endpoint_(ep) | |
| 223 | { | ||
| 224 | 10x | } | |
| 225 | |||
| 226 | 10x | bool await_ready() const noexcept | |
| 227 | { | ||
| 228 | // A pre-set ec_ means the initiator failed before | ||
| 229 | // dispatch (e.g. a closed object). | ||
| 230 | 10x | return static_cast<bool>(ec_) || token_.stop_requested(); | |
| 231 | } | ||
| 232 | |||
| 233 | 10x | [[nodiscard]] capy::io_result<> await_resume() const noexcept | |
| 234 | { | ||
| 235 | 10x | if (token_.stop_requested()) | |
| 236 | 2x | return {make_error_code(std::errc::operation_canceled)}; | |
| 237 | 8x | return {ec_}; | |
| 238 | } | ||
| 239 | |||
| 240 | 10x | auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env) | |
| 241 | -> std::coroutine_handle<> | ||
| 242 | { | ||
| 243 | 10x | token_ = env->stop_token; | |
| 244 | 30x | return self_.get_impl().connect( | |
| 245 | 30x | h, env->executor, endpoint_, token_, &ec_); | |
| 246 | } | ||
| 247 | }; | ||
| 248 | |||
| 249 | template<class ConstBufferSequence> | ||
| 250 | struct native_send_awaitable | ||
| 251 | { | ||
| 252 | native_local_datagram_socket& self_; | ||
| 253 | ConstBufferSequence buffers_; | ||
| 254 | int flags_; | ||
| 255 | std::stop_token token_; | ||
| 256 | mutable std::error_code ec_; | ||
| 257 | mutable std::size_t bytes_transferred_ = 0; | ||
| 258 | |||
| 259 | 6x | native_send_awaitable( | |
| 260 | native_local_datagram_socket& self, | ||
| 261 | ConstBufferSequence buffers, | ||
| 262 | int flags) noexcept | ||
| 263 | 6x | : self_(self) | |
| 264 | 6x | , buffers_(std::move(buffers)) | |
| 265 | 6x | , flags_(flags) | |
| 266 | { | ||
| 267 | 6x | } | |
| 268 | |||
| 269 | 6x | bool await_ready() const noexcept | |
| 270 | { | ||
| 271 | // A pre-set ec_ means the initiator failed before | ||
| 272 | // dispatch (e.g. a closed object). | ||
| 273 | 6x | return static_cast<bool>(ec_) || token_.stop_requested(); | |
| 274 | } | ||
| 275 | |||
| 276 | 6x | [[nodiscard]] capy::io_result<std::size_t> await_resume() const noexcept | |
| 277 | { | ||
| 278 | 6x | if (token_.stop_requested()) | |
| 279 | 2x | return {make_error_code(std::errc::operation_canceled), 0}; | |
| 280 | 4x | return {ec_, bytes_transferred_}; | |
| 281 | } | ||
| 282 | |||
| 283 | 4x | auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env) | |
| 284 | -> std::coroutine_handle<> | ||
| 285 | { | ||
| 286 | 4x | token_ = env->stop_token; | |
| 287 | 12x | return self_.get_impl().send( | |
| 288 | 4x | h, env->executor, buffers_, flags_, token_, &ec_, | |
| 289 | 8x | &bytes_transferred_); | |
| 290 | } | ||
| 291 | }; | ||
| 292 | |||
| 293 | template<class MutableBufferSequence> | ||
| 294 | struct native_recv_awaitable | ||
| 295 | { | ||
| 296 | native_local_datagram_socket& self_; | ||
| 297 | MutableBufferSequence buffers_; | ||
| 298 | int flags_; | ||
| 299 | std::stop_token token_; | ||
| 300 | mutable std::error_code ec_; | ||
| 301 | mutable std::size_t bytes_transferred_ = 0; | ||
| 302 | |||
| 303 | 6x | native_recv_awaitable( | |
| 304 | native_local_datagram_socket& self, | ||
| 305 | MutableBufferSequence buffers, | ||
| 306 | int flags) noexcept | ||
| 307 | 6x | : self_(self) | |
| 308 | 6x | , buffers_(std::move(buffers)) | |
| 309 | 6x | , flags_(flags) | |
| 310 | { | ||
| 311 | 6x | } | |
| 312 | |||
| 313 | 6x | bool await_ready() const noexcept | |
| 314 | { | ||
| 315 | // A pre-set ec_ means the initiator failed before | ||
| 316 | // dispatch (e.g. a closed object). | ||
| 317 | 6x | return static_cast<bool>(ec_) || token_.stop_requested(); | |
| 318 | } | ||
| 319 | |||
| 320 | 6x | [[nodiscard]] capy::io_result<std::size_t> await_resume() const noexcept | |
| 321 | { | ||
| 322 | 6x | if (token_.stop_requested()) | |
| 323 | 2x | return {make_error_code(std::errc::operation_canceled), 0}; | |
| 324 | 4x | return {ec_, bytes_transferred_}; | |
| 325 | } | ||
| 326 | |||
| 327 | 4x | auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env) | |
| 328 | -> std::coroutine_handle<> | ||
| 329 | { | ||
| 330 | 4x | token_ = env->stop_token; | |
| 331 | 12x | return self_.get_impl().recv( | |
| 332 | 4x | h, env->executor, buffers_, flags_, token_, &ec_, | |
| 333 | 8x | &bytes_transferred_); | |
| 334 | } | ||
| 335 | }; | ||
| 336 | |||
| 337 | public: | ||
| 338 | /** Construct a native socket from an execution context. | ||
| 339 | |||
| 340 | @param ctx The execution context that will own this socket. | ||
| 341 | */ | ||
| 342 | 30x | explicit native_local_datagram_socket(capy::execution_context& ctx) | |
| 343 | 30x | : local_datagram_socket(create_handle<service_type>(ctx)) | |
| 344 | { | ||
| 345 | 30x | } | |
| 346 | |||
| 347 | /** Construct a native socket from an executor. | ||
| 348 | |||
| 349 | @param ex The executor whose context will own the socket. | ||
| 350 | */ | ||
| 351 | template<class Ex> | ||
| 352 | requires(!std::same_as< | ||
| 353 | std::remove_cvref_t<Ex>, | ||
| 354 | native_local_datagram_socket>) && | ||
| 355 | capy::Executor<Ex> | ||
| 356 | explicit native_local_datagram_socket(Ex const& ex) | ||
| 357 | : native_local_datagram_socket(ex.context()) | ||
| 358 | { | ||
| 359 | } | ||
| 360 | |||
| 361 | /// Move construct. | ||
| 362 | native_local_datagram_socket(native_local_datagram_socket&&) noexcept = | ||
| 363 | default; | ||
| 364 | |||
| 365 | /// Move assign. | ||
| 366 | native_local_datagram_socket& | ||
| 367 | operator=(native_local_datagram_socket&&) noexcept = default; | ||
| 368 | |||
| 369 | native_local_datagram_socket(native_local_datagram_socket const&) = delete; | ||
| 370 | native_local_datagram_socket& | ||
| 371 | operator=(native_local_datagram_socket const&) = delete; | ||
| 372 | |||
| 373 | /** Send a datagram to the specified destination. | ||
| 374 | |||
| 375 | Calls the backend implementation directly, bypassing virtual | ||
| 376 | dispatch. Otherwise identical to @ref local_datagram_socket::send_to. | ||
| 377 | */ | ||
| 378 | template<capy::ConstBufferSequence CB> | ||
| 379 | 8x | [[nodiscard]] auto send_to( | |
| 380 | CB const& buffers, | ||
| 381 | corosio::local_endpoint dest, | ||
| 382 | corosio::message_flags flags) | ||
| 383 | { | ||
| 384 | 8x | native_send_to_awaitable<CB> aw( | |
| 385 | *this, buffers, dest, static_cast<int>(flags)); | ||
| 386 | 8x | if (!is_open()) | |
| 387 | 2x | aw.ec_ = make_error_code(std::errc::bad_file_descriptor); | |
| 388 | 8x | return aw; | |
| 389 | } | ||
| 390 | |||
| 391 | /// @overload | ||
| 392 | template<capy::ConstBufferSequence CB> | ||
| 393 | 8x | [[nodiscard]] auto send_to(CB const& buffers, corosio::local_endpoint dest) | |
| 394 | { | ||
| 395 | 8x | return send_to(buffers, dest, corosio::message_flags::none); | |
| 396 | } | ||
| 397 | |||
| 398 | /** Receive a datagram and capture the sender's endpoint. | ||
| 399 | |||
| 400 | Calls the backend implementation directly, bypassing virtual | ||
| 401 | dispatch. Otherwise identical to @ref local_datagram_socket::recv_from. | ||
| 402 | */ | ||
| 403 | template<capy::MutableBufferSequence MB> | ||
| 404 | 6x | [[nodiscard]] auto recv_from( | |
| 405 | MB const& buffers, | ||
| 406 | corosio::local_endpoint& source, | ||
| 407 | corosio::message_flags flags) | ||
| 408 | { | ||
| 409 | 6x | native_recv_from_awaitable<MB> aw( | |
| 410 | *this, buffers, source, static_cast<int>(flags)); | ||
| 411 | 6x | if (!is_open()) | |
| 412 | 2x | aw.ec_ = make_error_code(std::errc::bad_file_descriptor); | |
| 413 | 6x | return aw; | |
| 414 | } | ||
| 415 | |||
| 416 | /// @overload | ||
| 417 | template<capy::MutableBufferSequence MB> | ||
| 418 | [[nodiscard]] auto | ||
| 419 | 6x | recv_from(MB const& buffers, corosio::local_endpoint& source) | |
| 420 | { | ||
| 421 | 6x | return recv_from(buffers, source, corosio::message_flags::none); | |
| 422 | } | ||
| 423 | |||
| 424 | /** Asynchronously connect to set the default peer. | ||
| 425 | |||
| 426 | Calls the backend implementation directly, bypassing virtual | ||
| 427 | dispatch. Otherwise identical to @ref local_datagram_socket::connect. | ||
| 428 | |||
| 429 | If the socket is not already open, it is opened automatically. | ||
| 430 | */ | ||
| 431 | 10x | [[nodiscard]] auto connect(corosio::local_endpoint ep) | |
| 432 | { | ||
| 433 | 10x | native_connect_awaitable aw(*this, ep); | |
| 434 | 10x | if (!is_open()) | |
| 435 | 2x | aw.ec_ = open(); | |
| 436 | 10x | return aw; | |
| 437 | } | ||
| 438 | |||
| 439 | /** Send a datagram to the connected peer. | ||
| 440 | |||
| 441 | Calls the backend implementation directly, bypassing virtual | ||
| 442 | dispatch. Otherwise identical to @ref local_datagram_socket::send. | ||
| 443 | */ | ||
| 444 | template<capy::ConstBufferSequence CB> | ||
| 445 | 6x | [[nodiscard]] auto send(CB const& buffers, corosio::message_flags flags) | |
| 446 | { | ||
| 447 | 6x | native_send_awaitable<CB> aw(*this, buffers, static_cast<int>(flags)); | |
| 448 | 6x | if (!is_open()) | |
| 449 | 2x | aw.ec_ = make_error_code(std::errc::bad_file_descriptor); | |
| 450 | 6x | return aw; | |
| 451 | } | ||
| 452 | |||
| 453 | /// @overload | ||
| 454 | template<capy::ConstBufferSequence CB> | ||
| 455 | 6x | [[nodiscard]] auto send(CB const& buffers) | |
| 456 | { | ||
| 457 | 6x | return send(buffers, corosio::message_flags::none); | |
| 458 | } | ||
| 459 | |||
| 460 | /** Receive a datagram from the connected peer. | ||
| 461 | |||
| 462 | Calls the backend implementation directly, bypassing virtual | ||
| 463 | dispatch. Otherwise identical to @ref local_datagram_socket::recv. | ||
| 464 | */ | ||
| 465 | template<capy::MutableBufferSequence MB> | ||
| 466 | 6x | [[nodiscard]] auto recv(MB const& buffers, corosio::message_flags flags) | |
| 467 | { | ||
| 468 | 6x | native_recv_awaitable<MB> aw(*this, buffers, static_cast<int>(flags)); | |
| 469 | 6x | if (!is_open()) | |
| 470 | 2x | aw.ec_ = make_error_code(std::errc::bad_file_descriptor); | |
| 471 | 6x | return aw; | |
| 472 | } | ||
| 473 | |||
| 474 | /// @overload | ||
| 475 | template<capy::MutableBufferSequence MB> | ||
| 476 | 6x | [[nodiscard]] auto recv(MB const& buffers) | |
| 477 | { | ||
| 478 | 6x | return recv(buffers, corosio::message_flags::none); | |
| 479 | } | ||
| 480 | |||
| 481 | /** Asynchronously wait for the socket to be ready. | ||
| 482 | |||
| 483 | Calls the backend implementation directly, bypassing virtual | ||
| 484 | dispatch. Otherwise identical to @ref local_datagram_socket::wait. | ||
| 485 | |||
| 486 | @param w The wait direction (read, write, or error). | ||
| 487 | |||
| 488 | @return An awaitable yielding `io_result<>`. | ||
| 489 | */ | ||
| 490 | 4x | [[nodiscard]] auto wait(wait_type w) | |
| 491 | { | ||
| 492 | 4x | return native_wait_awaitable(*this, w); | |
| 493 | } | ||
| 494 | }; | ||
| 495 | |||
| 496 | } // namespace boost::corosio | ||
| 497 | |||
| 498 | #endif // BOOST_COROSIO_POSIX | ||
| 499 | |||
| 500 | #endif // BOOST_COROSIO_NATIVE_NATIVE_LOCAL_DATAGRAM_SOCKET_HPP | ||
| 501 |