| 1 |
|
|
1 |
|
|
| 2 |
|
// Copyright (c) 2026 Steve Gerbino
|
2 |
|
// Copyright (c) 2026 Steve Gerbino
|
| 3 |
|
|
3 |
|
|
| 4 |
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
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)
|
5 |
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
| 6 |
|
|
6 |
|
|
| 7 |
|
// Official repository: https://github.com/cppalliance/corosio
|
7 |
|
// Official repository: https://github.com/cppalliance/corosio
|
| 8 |
|
|
8 |
|
|
| 9 |
|
|
9 |
|
|
| 10 |
|
#ifndef BOOST_COROSIO_IO_IO_READ_STREAM_HPP
|
10 |
|
#ifndef BOOST_COROSIO_IO_IO_READ_STREAM_HPP
|
| 11 |
|
#define BOOST_COROSIO_IO_IO_READ_STREAM_HPP
|
11 |
|
#define BOOST_COROSIO_IO_IO_READ_STREAM_HPP
|
| 12 |
|
|
12 |
|
|
| 13 |
|
#include <boost/corosio/detail/config.hpp>
|
13 |
|
#include <boost/corosio/detail/config.hpp>
|
| 14 |
|
#include <boost/corosio/io/io_object.hpp>
|
14 |
|
#include <boost/corosio/io/io_object.hpp>
|
| 15 |
|
#include <boost/corosio/io_buffer_param.hpp>
|
15 |
|
#include <boost/corosio/io_buffer_param.hpp>
|
| 16 |
|
#include <boost/capy/io_result.hpp>
|
16 |
|
#include <boost/capy/io_result.hpp>
|
| 17 |
|
#include <boost/capy/ex/executor_ref.hpp>
|
17 |
|
#include <boost/capy/ex/executor_ref.hpp>
|
| 18 |
|
#include <boost/capy/ex/io_env.hpp>
|
18 |
|
#include <boost/capy/ex/io_env.hpp>
|
| 19 |
|
|
19 |
|
|
| 20 |
|
|
20 |
|
|
| 21 |
|
|
21 |
|
|
| 22 |
|
|
22 |
|
|
| 23 |
|
|
23 |
|
|
| 24 |
|
|
24 |
|
|
| 25 |
|
namespace boost::corosio {
|
25 |
|
namespace boost::corosio {
|
| 26 |
|
|
26 |
|
|
| 27 |
|
/** Abstract base for streams that support async reads.
|
27 |
|
/** Abstract base for streams that support async reads.
|
| 28 |
|
|
28 |
|
|
| 29 |
|
Provides the `read_some` operation via a pure virtual
|
29 |
|
Provides the `read_some` operation via a pure virtual
|
| 30 |
|
`do_read_some` dispatch point. Concrete classes override
|
30 |
|
`do_read_some` dispatch point. Concrete classes override
|
| 31 |
|
`do_read_some` to route through their implementation.
|
31 |
|
`do_read_some` to route through their implementation.
|
| 32 |
|
|
32 |
|
|
| 33 |
|
Uses virtual inheritance from @ref io_object so that
|
33 |
|
Uses virtual inheritance from @ref io_object so that
|
| 34 |
|
@ref io_stream can combine this with @ref io_write_stream
|
34 |
|
@ref io_stream can combine this with @ref io_write_stream
|
| 35 |
|
without duplicating the `io_object` base.
|
35 |
|
without duplicating the `io_object` base.
|
| 36 |
|
|
36 |
|
|
| 37 |
|
|
37 |
|
|
| 38 |
|
|
38 |
|
|
| 39 |
|
|
39 |
|
|
| 40 |
|
|
40 |
|
|
| 41 |
|
@see io_write_stream, io_stream, io_object
|
41 |
|
@see io_write_stream, io_stream, io_object
|
| 42 |
|
|
42 |
|
|
| 43 |
|
class BOOST_COROSIO_DECL io_read_stream : virtual public io_object
|
43 |
|
class BOOST_COROSIO_DECL io_read_stream : virtual public io_object
|
| 44 |
|
|
44 |
|
|
| 45 |
|
|
45 |
|
|
| 46 |
|
/// Awaitable for async read operations.
|
46 |
|
/// Awaitable for async read operations.
|
| 47 |
|
template<class MutableBufferSequence>
|
47 |
|
template<class MutableBufferSequence>
|
| 48 |
|
struct read_some_awaitable
|
48 |
|
struct read_some_awaitable
|
| 49 |
|
|
49 |
|
|
| 50 |
|
|
50 |
|
|
| 51 |
|
MutableBufferSequence buffers_;
|
51 |
|
MutableBufferSequence buffers_;
|
| 52 |
|
|
52 |
|
|
| 53 |
|
mutable std::error_code ec_;
|
53 |
|
mutable std::error_code ec_;
|
| 54 |
|
mutable std::size_t bytes_transferred_ = 0;
|
54 |
|
mutable std::size_t bytes_transferred_ = 0;
|
| 55 |
|
|
55 |
|
|
| 56 |
|
|
56 |
|
|
| 57 |
|
io_read_stream& ios, MutableBufferSequence buffers) noexcept
|
57 |
|
io_read_stream& ios, MutableBufferSequence buffers) noexcept
|
| 58 |
|
|
58 |
|
|
| 59 |
|
, buffers_(std::move(buffers))
|
59 |
|
, buffers_(std::move(buffers))
|
| 60 |
|
|
60 |
|
|
| 61 |
|
|
61 |
|
|
| 62 |
|
|
62 |
|
|
| 63 |
|
bool await_ready() const noexcept
|
63 |
|
bool await_ready() const noexcept
|
| 64 |
|
|
64 |
|
|
| 65 |
|
return token_.stop_requested();
|
65 |
|
return token_.stop_requested();
|
| 66 |
|
|
66 |
|
|
| 67 |
|
|
67 |
|
|
| 68 |
|
capy::io_result<std::size_t> await_resume() const noexcept
|
68 |
|
capy::io_result<std::size_t> await_resume() const noexcept
|
| 69 |
|
|
69 |
|
|
| 70 |
|
if (token_.stop_requested())
|
70 |
|
if (token_.stop_requested())
|
| 71 |
|
return {make_error_code(std::errc::operation_canceled), 0};
|
71 |
|
return {make_error_code(std::errc::operation_canceled), 0};
|
| 72 |
|
return {ec_, bytes_transferred_};
|
72 |
|
return {ec_, bytes_transferred_};
|
| 73 |
|
|
73 |
|
|
| 74 |
|
|
74 |
|
|
| 75 |
|
auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
|
75 |
|
auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env)
|
| 76 |
|
-> std::coroutine_handle<>
|
76 |
|
-> std::coroutine_handle<>
|
| 77 |
|
|
77 |
|
|
| 78 |
|
token_ = env->stop_token;
|
78 |
|
token_ = env->stop_token;
|
| 79 |
|
return ios_.do_read_some(
|
79 |
|
return ios_.do_read_some(
|
| 80 |
|
h, env->executor, buffers_, token_, &ec_, &bytes_transferred_);
|
80 |
|
h, env->executor, buffers_, token_, &ec_, &bytes_transferred_);
|
| 81 |
|
|
81 |
|
|
| 82 |
|
|
82 |
|
|
| 83 |
|
|
83 |
|
|
| 84 |
|
/** Dispatch a read through the concrete implementation.
|
84 |
|
/** Dispatch a read through the concrete implementation.
|
| 85 |
|
|
85 |
|
|
| 86 |
|
@param h Coroutine handle to resume on completion.
|
86 |
|
@param h Coroutine handle to resume on completion.
|
| 87 |
|
@param ex Executor for dispatching the completion.
|
87 |
|
@param ex Executor for dispatching the completion.
|
| 88 |
|
@param buffers Target buffer sequence.
|
88 |
|
@param buffers Target buffer sequence.
|
| 89 |
|
@param token Stop token for cancellation.
|
89 |
|
@param token Stop token for cancellation.
|
| 90 |
|
@param ec Output error code.
|
90 |
|
@param ec Output error code.
|
| 91 |
|
@param bytes Output bytes transferred.
|
91 |
|
@param bytes Output bytes transferred.
|
| 92 |
|
|
92 |
|
|
| 93 |
|
@return Coroutine handle to resume immediately.
|
93 |
|
@return Coroutine handle to resume immediately.
|
| 94 |
|
|
94 |
|
|
| 95 |
|
virtual std::coroutine_handle<> do_read_some(
|
95 |
|
virtual std::coroutine_handle<> do_read_some(
|
| 96 |
|
|
96 |
|
|
| 97 |
|
|
97 |
|
|
| 98 |
|
|
98 |
|
|
| 99 |
|
|
99 |
|
|
| 100 |
|
|
100 |
|
|
| 101 |
|
|
101 |
|
|
| 102 |
|
|
102 |
|
|
| 103 |
|
io_read_stream() noexcept = default;
|
103 |
|
io_read_stream() noexcept = default;
|
| 104 |
|
|
104 |
|
|
| 105 |
|
/// Construct from a handle.
|
105 |
|
/// Construct from a handle.
|
| 106 |
|
explicit io_read_stream(handle h) noexcept : io_object(std::move(h)) {}
|
106 |
|
explicit io_read_stream(handle h) noexcept : io_object(std::move(h)) {}
|
| 107 |
|
|
107 |
|
|
| 108 |
|
|
108 |
|
|
| 109 |
|
/** Asynchronously read data from the stream.
|
109 |
|
/** Asynchronously read data from the stream.
|
| 110 |
|
|
110 |
|
|
| 111 |
|
Suspends the calling coroutine and initiates a kernel-level
|
111 |
|
Suspends the calling coroutine and initiates a kernel-level
|
| 112 |
|
read. The coroutine resumes when at least one byte is read,
|
112 |
|
read. The coroutine resumes when at least one byte is read,
|
| 113 |
|
an error occurs, or the operation is cancelled.
|
113 |
|
an error occurs, or the operation is cancelled.
|
| 114 |
|
|
114 |
|
|
|
|
|
115 |
+ |
This stream must outlive the returned awaitable. The memory
|
|
|
|
116 |
+ |
referenced by @p buffers must remain valid until the operation
|
|
|
|
117 |
+ |
|
|
|
|
118 |
+ |
|
| 115 |
|
@param buffers The buffer sequence to read data into.
|
119 |
|
@param buffers The buffer sequence to read data into.
|
| 116 |
|
|
120 |
|
|
| 117 |
|
@return An awaitable yielding `(error_code, std::size_t)`.
|
121 |
|
@return An awaitable yielding `(error_code, std::size_t)`.
|
| 118 |
|
|
122 |
|
|
| 119 |
|
@see io_stream::write_some
|
123 |
|
@see io_stream::write_some
|
| 120 |
|
|
124 |
|
|
| 121 |
|
template<capy::MutableBufferSequence MB>
|
125 |
|
template<capy::MutableBufferSequence MB>
|
| 122 |
|
auto read_some(MB const& buffers)
|
126 |
|
auto read_some(MB const& buffers)
|
| 123 |
|
|
127 |
|
|
| 124 |
|
return read_some_awaitable<MB>(*this, buffers);
|
128 |
|
return read_some_awaitable<MB>(*this, buffers);
|
| 125 |
|
|
129 |
|
|
| 126 |
|
|
130 |
|
|
| 127 |
|
|
131 |
|
|
| 128 |
|
} // namespace boost::corosio
|
132 |
|
} // namespace boost::corosio
|
| 129 |
|
|
133 |
|
|
| 130 |
|
|
134 |
|
|