100.00% Lines (10/10) 100.00% Functions (4/4)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2 - // Copyright (c) 2026 Michael Vandeberg  
3   // Copyright (c) 2026 Steve Gerbino 2   // Copyright (c) 2026 Steve Gerbino
4   // 3   //
5   // Distributed under the Boost Software License, Version 1.0. (See accompanying 4   // 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) 5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7   // 6   //
8   // Official repository: https://github.com/cppalliance/corosio 7   // Official repository: https://github.com/cppalliance/corosio
9   // 8   //
10   9  
11   #ifndef BOOST_COROSIO_IO_IO_READ_STREAM_HPP 10   #ifndef BOOST_COROSIO_IO_IO_READ_STREAM_HPP
12   #define BOOST_COROSIO_IO_IO_READ_STREAM_HPP 11   #define BOOST_COROSIO_IO_IO_READ_STREAM_HPP
13   12  
14   #include <boost/corosio/detail/config.hpp> 13   #include <boost/corosio/detail/config.hpp>
15   #include <boost/corosio/detail/op_base.hpp> 14   #include <boost/corosio/detail/op_base.hpp>
16   #include <boost/corosio/io/io_object.hpp> 15   #include <boost/corosio/io/io_object.hpp>
17   #include <boost/corosio/detail/buffer_param.hpp> 16   #include <boost/corosio/detail/buffer_param.hpp>
18   #include <boost/capy/results.hpp> 17   #include <boost/capy/results.hpp>
19   #include <boost/capy/ex/executor_ref.hpp> 18   #include <boost/capy/ex/executor_ref.hpp>
20   #include <boost/capy/ex/await_context.hpp> 19   #include <boost/capy/ex/await_context.hpp>
21   20  
22   #include <boost/capy/continuation.hpp> 21   #include <boost/capy/continuation.hpp>
23   22  
24   #include <coroutine> 23   #include <coroutine>
25   #include <cstddef> 24   #include <cstddef>
26   #include <stop_token> 25   #include <stop_token>
27   #include <system_error> 26   #include <system_error>
28   27  
29   namespace boost::corosio { 28   namespace boost::corosio {
30   29  
31   /** Reads bytes from a stream asynchronously. 30   /** Reads bytes from a stream asynchronously.
32   31  
33   Provides the `read_some` operation via a pure virtual 32   Provides the `read_some` operation via a pure virtual
34   `do_read_some` dispatch point. Concrete classes override 33   `do_read_some` dispatch point. Concrete classes override
35   `do_read_some` to route through their implementation. 34   `do_read_some` to route through their implementation.
36   35  
37   Uses virtual inheritance from @ref io_object so that 36   Uses virtual inheritance from @ref io_object so that
38   @ref io_stream can combine this with @ref io_write_stream 37   @ref io_stream can combine this with @ref io_write_stream
39   without duplicating the `io_object` base. 38   without duplicating the `io_object` base.
40   39  
41   @par Thread Safety 40   @par Thread Safety
42   Distinct objects: Safe. 41   Distinct objects: Safe.
43   Shared objects: Unsafe. 42   Shared objects: Unsafe.
44   43  
45 - @see @ref io_write_stream, @ref io_stream, @ref io_object 44 + @see io_write_stream, io_stream, io_object
46   */ 45   */
47   class BOOST_COROSIO_DECL io_read_stream : virtual public io_object 46   class BOOST_COROSIO_DECL io_read_stream : virtual public io_object
48   { 47   {
49   protected: 48   protected:
50   /// Awaitable for async read operations. 49   /// Awaitable for async read operations.
51   template<class MutableBufferSequence> 50   template<class MutableBufferSequence>
52   struct read_some_awaitable 51   struct read_some_awaitable
53   : detail::bytes_op_base<read_some_awaitable<MutableBufferSequence>> 52   : detail::bytes_op_base<read_some_awaitable<MutableBufferSequence>>
54   { 53   {
55   private: 54   private:
56   friend io_read_stream; 55   friend io_read_stream;
57   56  
HITCBC 58   218035 read_some_awaitable( 57   231074 read_some_awaitable(
59   io_read_stream& ios, MutableBufferSequence buffers) noexcept 58   io_read_stream& ios, MutableBufferSequence buffers) noexcept
HITCBC 60   218035 : ios_(ios) 59   231074 : ios_(ios)
HITCBC 61   218035 , buffers_(std::move(buffers)) 60   231074 , buffers_(std::move(buffers))
62   { 61   {
HITCBC 63   218035 } 62   231074 }
64   63  
65   friend detail::bytes_op_base< 64   friend detail::bytes_op_base<
66   read_some_awaitable<MutableBufferSequence>>; 65   read_some_awaitable<MutableBufferSequence>>;
67   io_read_stream& ios_; 66   io_read_stream& ios_;
68   MutableBufferSequence buffers_; 67   MutableBufferSequence buffers_;
69   68  
70   std::coroutine_handle<> 69   std::coroutine_handle<>
HITCBC 71   218002 dispatch(capy::continuation& cont, capy::executor_ref ex) const 70   231052 dispatch(capy::continuation& cont, capy::executor_ref ex) const
72   { 71   {
HITCBC 73   436004 return ios_.do_read_some( 72   462104 return ios_.do_read_some(
HITCBC 74   654006 cont, ex, buffers_, this->token_, &this->ec_, &this->bytes_); 73   693156 cont, ex, buffers_, this->token_, &this->ec_, &this->bytes_);
75   } 74   }
76   }; 75   };
77   76  
78   /** Dispatch a read through the concrete implementation. 77   /** Dispatch a read through the concrete implementation.
79   78  
80   @param cont Continuation to resume on completion; it lives in 79   @param cont Continuation to resume on completion; it lives in
81   the awaiting frame until then. 80   the awaiting frame until then.
82   @param ex Executor for dispatching the completion. 81   @param ex Executor for dispatching the completion.
83   @param buffers Target buffer sequence. 82   @param buffers Target buffer sequence.
84   @param token Stop token for cancellation. 83   @param token Stop token for cancellation.
85   @param ec Output error code. 84   @param ec Output error code.
86   @param bytes Output bytes transferred. 85   @param bytes Output bytes transferred.
87   86  
88   @return Coroutine handle to resume immediately. 87   @return Coroutine handle to resume immediately.
89   */ 88   */
90   virtual std::coroutine_handle<> do_read_some( 89   virtual std::coroutine_handle<> do_read_some(
91   capy::continuation& cont, 90   capy::continuation& cont,
92   capy::executor_ref ex, 91   capy::executor_ref ex,
93   buffer_param buffers, 92   buffer_param buffers,
94   std::stop_token token, 93   std::stop_token token,
95   std::error_code* ec, 94   std::error_code* ec,
96   std::size_t* bytes) = 0; 95   std::size_t* bytes) = 0;
97   96  
98   /// Default construct; the handle is supplied through @ref io_object. 97   /// Default construct; the handle is supplied through @ref io_object.
HITCBC 99   26470 io_read_stream() noexcept = default; 98   26058 io_read_stream() noexcept = default;
100   99  
101   /// Move construct; the handle moves with @ref io_object. 100   /// Move construct; the handle moves with @ref io_object.
102   io_read_stream(io_read_stream&&) noexcept = default; 101   io_read_stream(io_read_stream&&) noexcept = default;
103   /// Move assignment is disabled; reseating a live stream is not supported. 102   /// Move assignment is disabled; reseating a live stream is not supported.
104   io_read_stream& operator=(io_read_stream&&) noexcept = delete; 103   io_read_stream& operator=(io_read_stream&&) noexcept = delete;
105   /// Copy construction is disabled; the handle is uniquely owned. 104   /// Copy construction is disabled; the handle is uniquely owned.
106   io_read_stream(io_read_stream const&) = delete; 105   io_read_stream(io_read_stream const&) = delete;
107   /// Copy assignment is disabled; the handle is uniquely owned. 106   /// Copy assignment is disabled; the handle is uniquely owned.
108   io_read_stream& operator=(io_read_stream const&) = delete; 107   io_read_stream& operator=(io_read_stream const&) = delete;
109   108  
110   public: 109   public:
111   /** Asynchronously read data from the stream. 110   /** Asynchronously read data from the stream.
112   111  
113   Suspends the calling coroutine and initiates a kernel-level 112   Suspends the calling coroutine and initiates a kernel-level
114   read. The coroutine resumes when at least one byte is read, 113   read. The coroutine resumes when at least one byte is read,
115   an error occurs, or the operation is cancelled. 114   an error occurs, or the operation is cancelled.
116   115  
117   This stream must outlive the returned awaitable. The memory 116   This stream must outlive the returned awaitable. The memory
118   referenced by @p buffers must remain valid until the operation 117   referenced by @p buffers must remain valid until the operation
119   completes. 118   completes.
120   119  
121   A closed stream completes with `errc::bad_file_descriptor`. 120   A closed stream completes with `errc::bad_file_descriptor`.
122   121  
123   @param buffers The buffer sequence to read data into. 122   @param buffers The buffer sequence to read data into.
124   123  
125   @return An awaitable yielding `(error_code, std::size_t)`. 124   @return An awaitable yielding `(error_code, std::size_t)`.
126   125  
127 - @see @ref io_stream::write_some 126 + @see io_stream::write_some
128   */ 127   */
129   template<capy::MutableBufferSequence MB> 128   template<capy::MutableBufferSequence MB>
HITCBC 130   218035 [[nodiscard]] auto read_some(MB const& buffers) 129   231074 [[nodiscard]] auto read_some(MB const& buffers)
131   { 130   {
HITCBC 132   218035 return read_some_awaitable<MB>(*this, buffers); 131   231074 return read_some_awaitable<MB>(*this, buffers);
133   } 132   }
134   }; 133   };
135   134  
136   } // namespace boost::corosio 135   } // namespace boost::corosio
137   136  
138   #endif 137   #endif