27#include <condition_variable>
117 [[nodiscard]]
bool write_async(
const std::uint8_t* data, std::size_t size,
127 return source_.token();
135 struct PendingWrite {
136 std::vector<std::uint8_t> data;
137 std::size_t written = 0;
143 void dispatch(std::function<
void()> task);
145 void drain_queue_as_closed();
147 std::unique_ptr<Stream> stream_;
149 CancelSource source_;
156 mutable std::mutex mutex_;
157 std::condition_variable cv_;
158 std::vector<PendingWrite> write_queue_;
159 std::size_t pending_bytes_ = 0;
160 bool running_ =
false;
161 bool close_fired_ =
false;
162 std::thread writer_thread_;
163 std::thread reader_thread_;
AsyncStreamOptions Options
Definition async_stream.hpp:87
CancelToken cancellation_token() const
Cancellation token shared with this stream's worker.
Definition async_stream.hpp:126
Stream * stream()
Underlying Stream, for advanced callers.
Definition async_stream.hpp:132
void on_data(AsyncDataCallback cb)
Attach callbacks.
std::size_t pending_write_bytes() const
Bytes currently queued for writing.
void on_close(AsyncCloseCallback cb)
void start()
Start the worker thread. No-op if already running.
bool write_async(const std::uint8_t *data, std::size_t size, AsyncWriteCallback callback={})
Queue an async write.
void stop()
Stop the worker thread and wait for it to exit.
void cancel()
Request cancellation.
AsyncStream(const AsyncStream &)=delete
AsyncStream & operator=(const AsyncStream &)=delete
void on_error(AsyncErrorCallback cb)
AsyncStream(std::unique_ptr< Stream > stream, Options options={})
void on_drain(AsyncCloseCallback cb)
fires when pending writes hit 0
Base class for all Pulp byte streams.
Definition stream.hpp:62
Definition generated_event_source.hpp:33
std::function< void(const std::uint8_t *data, std::size_t size)> AsyncDataCallback
Incoming-data callback.
Definition async_stream.hpp:52
StreamError
High-level error classification for Stream operations.
Definition stream.hpp:31
std::function< void(std::function< void()>)> AsyncExecutor
Executor hook: a function that schedules work on some thread.
Definition async_stream.hpp:42
std::function< void()> AsyncCloseCallback
Close callback.
Definition async_stream.hpp:62
std::function< void(std::size_t bytes, StreamError error)> AsyncWriteCallback
Completion callback for write_async.
Definition async_stream.hpp:48
std::function< void(StreamError error)> AsyncErrorCallback
Error callback.
Definition async_stream.hpp:57
Definition async_stream.hpp:64
std::size_t read_chunk
Read chunk size (bytes) used by the background worker.
Definition async_stream.hpp:66
AsyncExecutor executor
Optional executor used to dispatch completion/data/error callbacks onto the caller's thread.
Definition async_stream.hpp:82
bool auto_read
When true, the worker reads from the stream eagerly and fires on_data callbacks.
Definition async_stream.hpp:76
std::size_t write_high_water
High-water mark for pending write bytes.
Definition async_stream.hpp:72