|
| 1 | +// |
| 2 | +// Copyright (c) 2026 Mohammad Nejati |
| 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/burl |
| 8 | +// |
| 9 | + |
| 10 | +#ifndef BOOST_BURL_TEST_DETAIL_BUFFER_CONNECTION_HPP |
| 11 | +#define BOOST_BURL_TEST_DETAIL_BUFFER_CONNECTION_HPP |
| 12 | + |
| 13 | +#include <boost/burl/detail/connection_pool.hpp> |
| 14 | + |
| 15 | +#include <boost/capy/buffers/buffer_copy.hpp> |
| 16 | +#include <boost/capy/buffers/make_buffer.hpp> |
| 17 | +#include <boost/capy/error.hpp> |
| 18 | +#include <boost/capy/io_task.hpp> |
| 19 | +#include <boost/capy/test/fuse.hpp> |
| 20 | +#include <boost/capy/buffers/buffer_slice.hpp> |
| 21 | + |
| 22 | +#include <cstddef> |
| 23 | +#include <string> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +namespace boost |
| 27 | +{ |
| 28 | +namespace burl |
| 29 | +{ |
| 30 | +namespace test |
| 31 | +{ |
| 32 | +namespace detail |
| 33 | +{ |
| 34 | + |
| 35 | +class buffer_connection final : public burl::detail::connection |
| 36 | +{ |
| 37 | + std::vector<std::string> chunks_; |
| 38 | + capy::test::fuse fuse_; |
| 39 | + std::size_t idx_ = 0; |
| 40 | + std::size_t pos_ = 0; |
| 41 | + |
| 42 | +public: |
| 43 | + buffer_connection( |
| 44 | + std::vector<std::string> chunks, |
| 45 | + capy::test::fuse fuse) |
| 46 | + : chunks_(std::move(chunks)) |
| 47 | + , fuse_(std::move(fuse)) |
| 48 | + { |
| 49 | + } |
| 50 | + |
| 51 | + bool |
| 52 | + is_open() const noexcept override |
| 53 | + { |
| 54 | + return true; |
| 55 | + } |
| 56 | + |
| 57 | + capy::io_task<> |
| 58 | + shutdown() override |
| 59 | + { |
| 60 | + co_return {}; |
| 61 | + } |
| 62 | + |
| 63 | +private: |
| 64 | + capy::io_task<std::size_t> |
| 65 | + do_read_some(std::span<capy::mutable_buffer const> bufs) override |
| 66 | + { |
| 67 | + if(auto ec = fuse_.maybe_fail()) |
| 68 | + co_return { ec, 0 }; |
| 69 | + |
| 70 | + if(idx_ >= chunks_.size()) |
| 71 | + co_return { capy::error::eof, 0 }; |
| 72 | + |
| 73 | + auto const b = capy::make_buffer(chunks_[idx_]); |
| 74 | + auto const n = capy::buffer_copy( |
| 75 | + bufs, capy::buffer_slice(b, pos_).data()); |
| 76 | + pos_ += n; |
| 77 | + if(pos_ == b.size()) |
| 78 | + { |
| 79 | + ++idx_; |
| 80 | + pos_ = 0; |
| 81 | + } |
| 82 | + co_return { {}, n }; |
| 83 | + } |
| 84 | + |
| 85 | + capy::io_task<std::size_t> |
| 86 | + do_write_some(std::span<capy::const_buffer const>) override |
| 87 | + { |
| 88 | + co_return { capy::error::eof, 0 }; |
| 89 | + } |
| 90 | +}; |
| 91 | + |
| 92 | +} // namespace detail |
| 93 | +} // namespace test |
| 94 | +} // namespace burl |
| 95 | +} // namespace boost |
| 96 | + |
| 97 | +#endif |
0 commit comments