|
CX Framework
Cross-platform C utility framework
|
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) |
Functions for the consumer side of stream buffer operations.
| 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.
| sb | The stream buffer |
| size_t sbufCAvail | ( | StreamBuffer * | sb | ) |
size_t sbufCAvail(StreamBuffer *sb)
Returns how much data is currently buffered and ready to consume.
| sb | The stream buffer |
| 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.
| sb | The stream buffer |
| minsz | Minimum bytes to ensure are buffered |
| 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).
| sb | The stream buffer |
| buf | Buffer to read into |
| off | Offset from start of available data |
| sz | Number of bytes to peek |
| 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.
| sb | The stream buffer |
| buf | Buffer to read into |
| sz | Number of bytes to read |
| bytesread | Output: actual number of bytes read |
| 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.
| sb | The stream buffer |
| cnotify | Closure created with closureCreateAs(sbufNotifyCB, ...) |
| 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().
| sb | The stream buffer |
| cpush | Closure created with closureCreateAs(sbufPushCB, ...) |
| 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.
| sb | The stream buffer |
| func | Send callback to receive data |
| sz | Maximum bytes to send |
| ctx | Optional user context passed to the callback |
| 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.
| sb | The stream buffer |
| bytes | Number of bytes to skip |
| 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.
| sb | The stream buffer |