charcoal-dev/buffers
Laravel/PHP package providing buffer utilities for handling and transforming data streams in memory. Useful for queueing, chunking, and processing data efficiently with a simple API, supporting common buffer operations for custom workflows.
The TLV codec provides a structured way to encode and decode hierarchical binary data. It uses an Envelope containing multiple Frames, where each frame consists of several Parameters.
To use the TLV codec, you must define a protocol by implementing the ProtocolDefinitionInterface. This defines how IDs
are mapped to protocols, frames, and parameter types.
use Charcoal\Buffers\Contracts\Codecs\TLV\ProtocolDefinitionInterface;
class MyProtocolDefinition implements ProtocolDefinitionInterface {
// Implement mapping methods...
}
Encoding involves building an Envelope structure and passing it to the TlvBinaryCodec:
use Charcoal\Buffers\Codecs\TLV\TlvBinaryCodec;
use Charcoal\Buffers\Codecs\TLV\Envelope;
use Charcoal\Buffers\Codecs\TLV\Frame;
use Charcoal\Buffers\Codecs\TLV\Param;
$envelope = new Envelope(
MyProtocol::Ping,
new Frame(
MyFrame::Request,
new Param(MyParam::Timestamp, time()),
new Param(MyParam::Payload, "Hello")
)
);
$binary = TlvBinaryCodec::encode($envelope);
Decoding converts raw bytes back into an Envelope object using your protocol definition:
$bytes = "... binary data ...";
$protocol = new MyProtocolDefinition();
$envelope = TlvBinaryCodec::decode($protocol, $bytes);
echo $envelope->protocol->name; // "Ping"
$firstFrame = $envelope->frames[0];
$firstParam = $firstFrame->params[0];
echo $firstParam->value;
How can I help you explore Laravel packages today?