TLA Line data Source code
1 : //
2 : // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
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/capy
8 : //
9 :
10 : #ifndef BOOST_CAPY_WRITE_HPP
11 : #define BOOST_CAPY_WRITE_HPP
12 :
13 : #include <boost/capy/detail/config.hpp>
14 : #include <boost/capy/io_task.hpp>
15 : #include <boost/capy/buffers.hpp>
16 : #include <boost/capy/buffers/buffer_slice.hpp>
17 : #include <boost/capy/concept/write_stream.hpp>
18 : #include <system_error>
19 :
20 : #include <cstddef>
21 :
22 : namespace boost {
23 : namespace capy {
24 :
25 : /** Write an entire buffer sequence to a stream.
26 :
27 : @par Await-effects
28 :
29 : Writes the contents of `buffers` to `stream` via awaiting
30 : `stream.write_some` with consecutive portions of data from `buffers`
31 : until:
32 :
33 : @li either the full content of @c buffers is processed,
34 : @li or a contingency occurs.
35 :
36 : If `buffer_size(buffers) == 0` then no awaiting `stream.write_some`
37 : is performed. This is not a contingency.
38 :
39 :
40 : @par Await-returns
41 :
42 : An object of type `io_result<std::size_t>` destructuring as `[ec, n]`.
43 :
44 : Upon a contingency, `n` represents the number of bytes written
45 : so far.
46 :
47 : Otherwise `n` represents the number of bytes written.
48 :
49 : Contingencies:
50 :
51 : @li The first contingency reported from
52 : awaiting @c stream.write_some .
53 :
54 : Notable conditions:
55 :
56 : @li @c cond::canceled — Operation was cancelled,
57 : @li @c std::errc::broken_pipe — Peer closed connection.
58 :
59 :
60 : @par Await-postcondition
61 :
62 : `ec || n == buffer_size(buffers)`.
63 :
64 :
65 : @param stream The stream to write to. If the lifetime of `stream` ends
66 : before the coroutine finishes, the behavior is undefined.
67 :
68 : @param buffers The buffer sequence to write. If the lifetime of the buffer
69 : sequence represented by `buffers` ends
70 : before the coroutine finishes, the behavior is undefined.
71 :
72 : @par Remarks
73 :
74 : Supports _IoAwaitable cancellation_.
75 :
76 : @par Example
77 :
78 : @code
79 : capy::task<> send_response(capy::WriteStream auto& stream, std::string_view body)
80 : {
81 : auto [ec, n] = co_await capy::write(stream, capy::make_buffer(body));
82 : if (ec)
83 : throw std::system_error(ec);
84 :
85 : // All bytes written successfully
86 : }
87 : @endcode
88 :
89 : @see WriteStream, ConstBufferSequence, IoAwaitable, io_result, cond.
90 : */
91 : template <WriteStream S, ConstBufferSequence CB>
92 HIT 50 : auto write(S& stream, CB buffers) -> io_task<std::size_t>
93 : {
94 : auto consuming = buffer_slice(buffers);
95 : std::size_t const total_size = buffer_size(buffers);
96 : std::size_t total_written = 0;
97 :
98 : while(total_written < total_size)
99 : {
100 : auto [ec, n] = co_await stream.write_some(consuming.data());
101 : consuming.remove_prefix(n);
102 : total_written += n;
103 : if(ec)
104 : co_return {ec, total_written};
105 : }
106 :
107 : co_return {{}, total_written};
108 100 : }
109 :
110 : } // namespace capy
111 : } // namespace boost
112 :
113 : #endif
|