6 #ifndef KAGOME_NETWORK_HELPERS_STREAM_READ_BUFFER_HPP 7 #define KAGOME_NETWORK_HELPERS_STREAM_READ_BUFFER_HPP 20 std::enable_shared_from_this<StreamReadBuffer> {
21 std::shared_ptr<std::vector<uint8_t>>
buffer;
27 buffer{std::make_shared<std::vector<uint8_t>>(capacity)} {}
40 void readFull(gsl::span<uint8_t> out,
size_t total, ReadCallbackFunc cb) {
44 [weak{weak_from_this()}, out, total, cb{std::move(cb)}](
45 outcome::result<size_t> _r)
mutable {
46 if (
auto self{weak.lock()}) {
48 return cb(_r.error());
50 const auto &r = _r.value();
51 const auto _r{gsl::narrow<ptrdiff_t>(r)};
52 BOOST_ASSERT(_r <= out.size());
53 if (_r == out.size()) {
58 self->readFull(out.subspan(r), total, std::move(cb));
63 void read(gsl::span<uint8_t> out,
size_t n, ReadCallbackFunc cb)
override {
64 BOOST_ASSERT(out.size() >= gsl::narrow<ptrdiff_t>(n));
74 auto n = std::min(gsl::narrow<size_t>(out.size()),
size());
77 std::copy_n(buffer->begin() +
begin, n, out.begin());
85 ReadCallbackFunc cb)
override {
86 BOOST_ASSERT(out.size() >= gsl::narrow<ptrdiff_t>(n));
89 return cb(out.size());
99 [weak{weak_from_this()}, out, cb{std::move(cb)}, buffer{buffer}](
100 outcome::result<size_t> _r)
mutable {
101 if (
auto self{weak.lock()}) {
102 if (_r.has_error()) {
103 return cb(_r.error());
105 const auto &r = _r.value();
110 cb(self->readFromBuffer(out));
123 constexpr
size_t kBuffer{1 << 16};
124 result.stream = std::make_shared<libp2p::connection::StreamReadBuffer>(
125 std::move(result.stream), kBuffer);
133 if (result.has_value()) {
139 #endif // KAGOME_NETWORK_HELPERS_STREAM_READ_BUFFER_HPP void streamReadBuffer(libp2p::StreamAndProtocolOrError &result)
void readSome(gsl::span< uint8_t > out, size_t n, ReadCallbackFunc cb) override
std::shared_ptr< std::vector< uint8_t > > buffer
std::shared_ptr< Stream > stream
void read(gsl::span< uint8_t > out, size_t n, ReadCallbackFunc cb) override
StreamReadBuffer(std::shared_ptr< Stream > stream, size_t capacity)
void readFull(gsl::span< uint8_t > out, size_t total, ReadCallbackFunc cb)
size_t readFromBuffer(gsl::span< uint8_t > out)