trafficcophp/bytebuffer
TrafficCophp ByteBuffer is a small PHP library for reading and writing binary data. A convenient wrapper around pack()/unpack() with helpers for int8/int32 (e.g., big-endian) and building protocol messages for sockets.
Install via Composer: composer require trafficcophp/bytebuffer, then import TrafficCophp\ByteBuffer\ByteBuffer. Its core use case is constructing binary protocol frames—e.g., TCP socket messages with fixed headers and variable payloads. Start by allocating a buffer of exact size, then write scalar types (integers, strings) at explicit byte offsets. The README’s socket client example is the canonical first use: build a length-prefixed frame with header fields (writeInt32BE, writeInt8, writeString) and send via socket_write($socket, (string)$buffer, $buffer->length()).
write($data, $offset)), maintain an internal cursor or precompute field sizes (e.g., 4 + 1 + 4 + strlen($channel)) to avoid overwrites. Wrap frequent sequences (e.g., header write + payload write) in helper methods.clear() or reinitialize with new sizes.writeInt32BE() for network-byte-order fields (big-endian), writeInt32LE() for little-endian targets (e.g., Windows APIs), and verify against server expectations using bin2hex((string)$buffer).pack()/unpack() fatigue: When repeating complex pack('Nva*a*', ...) chains, extract the logic into a ByteBuffer-backed class (e.g., HttpRequestFrame) for cleaner maintainability.ByteBuffer to add a cursor (e.g., $buffer->writeInt32BE(123) → $buffer->writeInt32BE(123, $cursor++) with $cursor++).__toString() truncates to length(), but under-allocation causes silent overwrites. Log expected vs actual sizes during development.BE/LE is the #1 bug source. Document expected byte order per field in comments (e.g., // Length MUST be big-endian per spec v1.2).write($str) uses strlen($str), not mb_strlen()—be cautious with multibyte strings; encode first (e.g., utf8_encode() or mb_convert_encoding()).read*() methods and concatenation tests. Fork early to add critical features (e.g., concat(), readInt32BE($offset)) and lock to a commit in composer.json—never use dev-master.str_pad(dechex(ord($byte)), 2, '0', STR_PAD_LEFT) or bin2hex($buffer->slice($offset, $length)) to validate byte sequences against protocol specs—especially for multi-byte fields where offsets shift silently.How can I help you explore Laravel packages today?