Sane C++ Libraries
C++ Platform Abstraction Libraries
SC::AsyncWritableStream Struct Reference

Async destination abstraction where bytes can be written to. More...

#include <AsyncStreams.h>

Inheritance diagram for SC::AsyncWritableStream:
SC::AsyncRequestWritableStream< AsyncFileWrite > SC::AsyncRequestWritableStream< AsyncSocketSend > SC::AsyncRequestWritableStream< AsyncRequestType > SC::AsyncTransformStream SC::WritableFileStream SC::WritableSocketStream SC::SyncZLibTransformStream

Classes

struct  Request
 

Public Member Functions

Result init (AsyncBuffersPool &buffersPool, Span< Request > requests)
 Emitted when no more data can be written. More...
 
Result write (AsyncBufferView::ID bufferID, Function< void(AsyncBufferView::ID)> cb={})
 Writes a buffer (that must be allocated by the AsyncBuffersPool passed in AsyncWritableStream) When the buffer it will be actually written, AsyncWritableStream::eventWritten will be raised and its reference count will be decreased. More...
 
Result write (Span< const char > data, Function< void(AsyncBufferView::ID)> cb={})
 Try requesting a buffer big enough and copy data into it. More...
 
template<size_t N>
Result write (const char(&str)[N])
 Write a C-string literal in the stream. More...
 
void end ()
 Ends the writable stream, waiting for all in-flight and queued writes to finish. More...
 
AsyncBuffersPoolgetBuffersPool ()
 Obtains the buffers pool to access its data. More...
 
void finishedWriting (AsyncBufferView::ID bufferID, Function< void(AsyncBufferView::ID)> &&cb, Result res)
 Signals that the given buffer (previously queued by write) has been fully written. More...
 
void resumeWriting ()
 Resumes writing queued requests for this stream. More...
 
Result unshift (AsyncBufferView::ID bufferID, Function< void(AsyncBufferView::ID)> &&cb)
 Puts back a buffer at the top of the write queue. More...
 
void emitError (Result error)
 Signals an async error received. More...
 
void tryAsync (Result potentialError)
 Will emit error if the passed in Result is false. More...
 
void stop ()
 

Public Attributes

Function< Result(AsyncBufferView::ID, Function< void(AsyncBufferView::ID)>)> asyncWrite
 Function that every stream must define to implement its custom write operation. More...
 
Event< MaxListeners, ResulteventError
 
Event< MaxListeners > eventDrain
 Emitted when an error occurs. More...
 
Event< MaxListeners > eventFinish
 Emitted when write queue is empty. More...
 
Function< bool()> canEndWritable
 Allows keeping a writable in ENDING state until it has finished flushing all pending data. More...
 

Static Public Attributes

static constexpr int MaxListeners = 8
 

Detailed Description

Async destination abstraction where bytes can be written to.

When buffers are pushed faster than the stream can handle, they will get queued. Queuing process happens with a linked list stored in the AsyncBufferView itself. As AsyncBufferView contains a fixed (at init) number of buffers, the queue is bounded by the fact that user will be unable to allocate buffers to write until at least one will be made available again (i.e. a write finishes). User can listen to AsyncWritableStream::eventWritten to know when a buffer is written (and its refcount decreased) or AsyncWritableStream::eventDrain when the queue is empty.

Member Function Documentation

◆ emitError()

void SC::AsyncWritableStream::emitError ( Result  error)

Signals an async error received.

◆ end()

void SC::AsyncWritableStream::end ( )

Ends the writable stream, waiting for all in-flight and queued writes to finish.

After this happens, AsyncWritableStream::eventFinished will be raised

◆ finishedWriting()

void SC::AsyncWritableStream::finishedWriting ( AsyncBufferView::ID  bufferID,
Function< void(AsyncBufferView::ID)> &&  cb,
Result  res 
)

Signals that the given buffer (previously queued by write) has been fully written.

◆ getBuffersPool()

AsyncBuffersPool & SC::AsyncWritableStream::getBuffersPool ( )

Obtains the buffers pool to access its data.

◆ init()

Result SC::AsyncWritableStream::init ( AsyncBuffersPool buffersPool,
Span< Request requests 
)

Emitted when no more data can be written.

Inits the writable stream

Parameters
buffersPoolAn instance of AsyncBuffersPool providing write buffers
requestsUser owned memory to hold a circular buffer for write requests

◆ resumeWriting()

void SC::AsyncWritableStream::resumeWriting ( )

Resumes writing queued requests for this stream.

◆ tryAsync()

void SC::AsyncWritableStream::tryAsync ( Result  potentialError)

Will emit error if the passed in Result is false.

◆ unshift()

Result SC::AsyncWritableStream::unshift ( AsyncBufferView::ID  bufferID,
Function< void(AsyncBufferView::ID)> &&  cb 
)

Puts back a buffer at the top of the write queue.

◆ write() [1/3]

Result SC::AsyncWritableStream::write ( AsyncBufferView::ID  bufferID,
Function< void(AsyncBufferView::ID)>  cb = {} 
)

Writes a buffer (that must be allocated by the AsyncBuffersPool passed in AsyncWritableStream) When the buffer it will be actually written, AsyncWritableStream::eventWritten will be raised and its reference count will be decreased.

Parameters
bufferIDBuffer allocated from the associated AsyncBuffersPool (AsyncWritableStream::getBuffersPool)
cbCallback that will be invoked when the write is finished
Returns
Invalid Result if write queue is full

◆ write() [2/3]

template<size_t N>
Result SC::AsyncWritableStream::write ( const char(&)  str[N])
inline

Write a C-string literal in the stream.

Returns
Invalid Result if write queue is full or if there are no available buffers in the pool

◆ write() [3/3]

Result SC::AsyncWritableStream::write ( Span< const char >  data,
Function< void(AsyncBufferView::ID)>  cb = {} 
)

Try requesting a buffer big enough and copy data into it.

Returns
Invalid Result if write queue is full or if there are no available buffers in the pool

Member Data Documentation

◆ asyncWrite

Function<Result(AsyncBufferView::ID, Function<void(AsyncBufferView::ID)>)> SC::AsyncWritableStream::asyncWrite

Function that every stream must define to implement its custom write operation.

◆ canEndWritable

Function<bool()> SC::AsyncWritableStream::canEndWritable

Allows keeping a writable in ENDING state until it has finished flushing all pending data.

If a writable stream redefines this function it should return true to allow transitioning to ENDED state and return false to keep staying in ENDING state.

◆ eventDrain

Event<MaxListeners> SC::AsyncWritableStream::eventDrain

Emitted when an error occurs.

◆ eventFinish

Event<MaxListeners> SC::AsyncWritableStream::eventFinish

Emitted when write queue is empty.


The documentation for this struct was generated from the following file: