NPRPC docs

class StreamWriter

C++

template <typename T> class StreamWriter : public StreamWriterBase

The sending end of a stream of T. It works in two ways:

  • As a coroutine, for a servant's server_stream method: co_yield each item and co_return when done. The runtime pulls items as the reader grants credits, so a slow reader slows the producer down.

    nprpc::StreamWriter<Post> feed(uint32_t limit) override {
      for (auto& post : latest(limit))
        co_yield post;
    }
    
  • Pushed, for the writer half of a client or bidi stream: call write() per item, then close() (or abort() on failure).

include/nprpc/stream_writer.hpp:40

Constructors

explicit StreamWriter<T>(handle_type h)

Adopts a coroutine; used by promise_type.

include/nprpc/stream_writer.hpp:87

StreamWriter<T>(SessionContext &session, uint64_t stream_id)

A pushed writer for stream stream_id on session; created by generated code.

include/nprpc/stream_writer.hpp:94

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

Takes over other's stream.

include/nprpc/stream_writer.hpp:102

Methods

void abort(uint32_t error_code = 1)

Ends a pushed stream with an error; the reader rethrows it.

include/nprpc/stream_writer.hpp:287

void cancel() override

Stops the stream: destroys an unfinished coroutine, or tells the reader a pushed stream was cancelled.

include/nprpc/stream_writer.hpp:224

void close()

Ends a pushed stream normally. Later writes are refused.

include/nprpc/stream_writer.hpp:278

bool is_done() const override

Whether the stream has finished.

include/nprpc/stream_writer.hpp:212

void probe_init()

Probe init-time exceptions for server streams with raises(...); called by the runtime.

StreamWriter uses suspend_always, so constructing the coroutine never runs the body. Servants that throw before the first co_yield would otherwise only fail after StreamInit Success was already sent.

Resumes once (or until first yield / completion). Rethrows any exception stored by unhandled_exception. If the body yields a value, it is buffered in the promise for the first real resume() after the Success reply. If the body completes empty, a deferred completion is sent on the first resume().

include/nprpc/stream_writer.hpp:130

void resume() override

Runs the coroutine to its next item and sends it; called by the runtime when the reader has credits.

include/nprpc/stream_writer.hpp:151

void set_manager(impl::StreamManager *manager, uint64_t stream_id)

Attaches a coroutine writer to its stream; called by the runtime.

include/nprpc/stream_writer.hpp:240

bool write(const T &value)

Writes one element. Returns false if it did not go out.

A refusal means the transport could not take the chunk — most often a shared-memory ring the consumer has stopped draining, which fills after a bounded number of undelivered messages and then refuses every one. The stream is closed at that point rather than carried on with: this is a reliable stream, so the consumer would otherwise be handed a sequence gap it cannot distinguish from reordering, and the producer would keep serialising into a ring that has no room for it.

Ignoring the result is safe but means never learning the consumer went away, which is what a periodic publisher (a compositor telling a shell what changed) wants to know soonest.

include/nprpc/stream_writer.hpp:261

bool write(T &&value)

Writes one element; see the const T& overload.

include/nprpc/stream_writer.hpp:272

Type aliases

using handle_type = std::coroutine_handle<promise_type>

The coroutine handle type.

include/nprpc/stream_writer.hpp:45

Types

struct promise_type

Coroutine machinery; not called directly.

include/nprpc/stream_writer.hpp:48