NPRPC docs

class StreamReader

C++

template <typename T> class StreamReader : public StreamReaderBase

The receiving end of a stream of T: what a proxy's server_stream method returns, and what a servant's client_stream method receives.

Read it with a range-for (blocking), read_next() (blocking), or co_await in a coroutine:

while (auto item = co_await reader) {
  use(*item);
}

Flow control is automatic: the reader grants the producer more credits as it consumes. Destroying the reader cancels an unfinished stream.

include/nprpc/stream_reader.hpp:45

Constructors

StreamReader<T>(SessionContext &session, uint64_t stream_id, uint32_t producer_window = static_cast<uint32_t>(impl::StreamManager::kInitialWindowSize))

Created by generated code.

producer_window is the credit pool the remote producer is working with:

  • client-side readers advertise kDefaultReaderWindow via StreamInit.initial_credits, so stubs pass that value here;
  • server-side readers (client/bidi uploads) have no advertisement channel, so the producer sits on the legacy kInitialWindowSize; the default keeps the grant threshold below that window.

include/nprpc/stream_reader.hpp:57

StreamReader<T>(StreamReader<T> &&other) noexcept

Takes over other's stream; other no longer reads it.

include/nprpc/stream_reader.hpp:71

Methods

iterator begin()

Waits for the first item.

include/nprpc/stream_reader.hpp:139

void cancel()

Stops reading, and tells the producer to stop if it has not finished.

include/nprpc/stream_reader.hpp:290

iterator end()

The end of the stream.

include/nprpc/stream_reader.hpp:141

bool is_complete() const

Whether the producer finished and every item has been read.

include/nprpc/stream_reader.hpp:309

void on_chunk_received(flat_buffer fb) override

Called by the runtime when a chunk arrives.

include/nprpc/stream_reader.hpp:225

void on_complete() override

Called by the runtime when the producer finishes.

include/nprpc/stream_reader.hpp:246

void on_error(uint32_t error_code, flat_buffer error_data) override

Called by the runtime when the producer fails; readers then throw.

include/nprpc/stream_reader.hpp:267

std::optional<T> read_next()

Blocks until the next item and returns it, or std::nullopt once the stream is complete. Rethrows a stream error.

include/nprpc/stream_reader.hpp:164

Operators

auto operator co_await()

Waits without blocking a thread; yields the next item, or std::nullopt once the stream is complete. Rethrows a stream error.

include/nprpc/stream_reader.hpp:145

Types