CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
Consumer Functions

Functions

bool sbufCRegisterPush (StreamBuffer *sb, closure cnotify)
 
bool sbufCRegisterPushDirect (StreamBuffer *sb, closure cpush)
 
void sbufCUnregister (StreamBuffer *sb)
 
bool sbufCAttached (StreamBuffer *sb)
 
size_t sbufCAvail (StreamBuffer *sb)
 
bool sbufCRead (StreamBuffer *sb, uint8 *buf, size_t sz, size_t *bytesread)
 
bool sbufCPeek (StreamBuffer *sb, uint8 *buf, size_t off, size_t sz)
 
bool sbufCFeed (StreamBuffer *sb, size_t minsz)
 
bool sbufCSend (StreamBuffer *sb, sbufSendCB func, size_t sz, void *ctx)
 
bool sbufCSkip (StreamBuffer *sb, size_t bytes)
 

Detailed Description

Functions for the consumer side of stream buffer operations.

Function Documentation

◆ sbufCAttached()

bool sbufCAttached ( StreamBuffer *  sb)

Checks whether a consumer callback is currently attached.

Only push streams have one; this is always false in pull mode, where the consumer drives.

Parameters
sbThe stream buffer
Returns
true if a push consumer is registered

◆ sbufCAvail()

size_t sbufCAvail ( StreamBuffer *  sb)

size_t sbufCAvail(StreamBuffer *sb)

Returns how much data is currently buffered and ready to consume.

Parameters
sbThe stream buffer
Returns
Number of bytes available

◆ sbufCFeed()

bool sbufCFeed ( StreamBuffer *  sb,
size_t  minsz 
)

bool sbufCFeed(StreamBuffer *sb, size_t minsz)

For pull mode only - feeds the buffer until it has at least the requested bytes.

Similar to sbufCRead() but doesn't consume the data. Useful for peek-ahead operations. Keeps calling the producer until enough data is available, the stream ends, or the producer unregisters.

Parameters
sbThe stream buffer
minszMinimum bytes to ensure are buffered
Returns
true if request satisfied, false if the data ran out first

◆ sbufCPeek()

bool sbufCPeek ( StreamBuffer *  sb,
uint8 *  buf,
size_t  off,
size_t  sz 
)

bool sbufCPeek(StreamBuffer *sb, uint8 *buf, size_t off, size_t sz)

Peeks at data in the buffer without consuming it.

Only looks at data already in the buffer and never calls the producer; use sbufCFeed() first in pull mode.

Never short-reads; fails if insufficient data is available (check sbufCAvail first).

Parameters
sbThe stream buffer
bufBuffer to read into
offOffset from start of available data
szNumber of bytes to peek
Returns
true on success, false if not enough data available

◆ sbufCRead()

bool sbufCRead ( StreamBuffer *  sb,
uint8 *  buf,
size_t  sz,
size_t *  bytesread 
)

bool sbufCRead(StreamBuffer *sb, uint8 *buf, size_t sz, size_t *bytesread)

Reads data from the stream buffer.

Pull mode: Repeatedly calls the producer's callback to satisfy the request. Short-reads once the stream ends or the producer unregisters.

Push mode: Returns only buffered data. Fails if requesting more than available.

Parameters
sbThe stream buffer
bufBuffer to read into
szNumber of bytes to read
bytesreadOutput: actual number of bytes read
Returns
true if any data was read, false on error or no data available

◆ sbufCRegisterPush()

bool sbufCRegisterPush ( StreamBuffer *  sb,
closure  cnotify 
)

Registers a consumer to be notified of data, putting the buffer in push mode.

The producer drives from here on: the closure runs whenever data is available, and the consumer uses sbufCRead() or sbufCSend() to take as much of it as it wants. If the producer has already written something, the closure runs once immediately with the backlog.

Registration takes its own reference to the buffer and gives it back on unregister, so keep your own as well.

Takes ownership of the closure whether or not registration succeeds. It is destroyed when the consumer is unregistered, or right away if registration fails.

Parameters
sbThe stream buffer
cnotifyClosure created with closureCreateAs(sbufNotifyCB, ...)
Returns
true on success, false if a consumer is already attached, the stream has closed, or the buffer is already in pull or direct mode

◆ sbufCRegisterPushDirect()

bool sbufCRegisterPushDirect ( StreamBuffer *  sb,
closure  cpush 
)

Registers a consumer in direct push mode.

Data is handed to the closure as it is written and never buffered, so the consumer must take all of it every time. A direct buffer has no storage of its own: with no consumer attached there is nowhere for a write to go and it fails.

Takes ownership of the closure whether or not registration succeeds, as sbufCRegisterPush().

Parameters
sbThe stream buffer
cpushClosure created with closureCreateAs(sbufPushCB, ...)
Returns
true on success, false if a consumer is already attached, the stream has closed, or the buffer is already in pull mode

◆ sbufCSend()

bool sbufCSend ( StreamBuffer *  sb,
sbufSendCB  func,
size_t  sz,
void *  ctx 
)

bool sbufCSend(StreamBuffer *sb, sbufSendCB func, size_t sz, void *ctx)

Sends data from buffer to callback with zero-copy optimization.

The callback may be invoked multiple times with different chunks.

Push mode: More efficient than sbufCRead() as it avoids an extra copy by passing pointers to internal buffer directly to the callback.

Pull mode: Functions like sbufCRead(), calling producer to fill buffer before invoking the callback.

Parameters
sbThe stream buffer
funcSend callback to receive data
szMaximum bytes to send
ctxOptional user context passed to the callback
Returns
true on success

◆ sbufCSkip()

bool sbufCSkip ( StreamBuffer *  sb,
size_t  bytes 
)

bool sbufCSkip(StreamBuffer *sb, size_t bytes)

Skips over bytes in the buffer without reading them.

Can be used in conjunction with sbufCPeek() to peek ahead and then skip.

Parameters
sbThe stream buffer
bytesNumber of bytes to skip
Returns
true on success, false if not enough data available

◆ sbufCUnregister()

void sbufCUnregister ( StreamBuffer *  sb)

Detaches the consumer.

Empties the consumer slot, destroys its closure and gives back the reference the registration took. The stream is not closed: another consumer may register and will be handed everything that piled up in the meantime.

Call sbufPFlush() first when swapping consumers, so the bytes already written reach the one that is leaving rather than the one arriving.

Safe to call from inside the consumer's own callback. Does nothing if no consumer is attached.

Parameters
sbThe stream buffer