clj-artnet.impl.protocol.codec.compiler

Spec-driven codec compiler for Art-Net 4.

This namespace compiles declarative packet specifications from clj-artnet.impl.protocol.codec.spec into high-performance ByteBuffer operations.

Key features: - Zero-allocation reads via flyweight pattern - Primitive type hints (^long, ^bytes) in hot paths - Direct ByteBuffer access without intermediate allocations - Generated encoders/decoders from spec data

compile-decoder

(compile-decoder spec op-keyword)

Compile a spec into a decoder function. Returns (fn ^ByteBuffer buf -> map).

compile-encoder

(compile-encoder spec)

Compile a spec into an encoder function. Returns (fn ^ByteBuffer buf packet -> ByteBuffer).

compiled-decoders

Map of opcode keyword -> compiled decoder fn

compiled-encoders

Map of opcode keyword -> compiled encoder fn

decode-by-spec

(decode-by-spec buf spec)

Decode a packet from a buffer using the given spec. Returns a map with all field values keyed by the field name.

decode-packet

(decode-packet buf)

Decode a packet from a buffer using compiled decoder. Returns nil if opcode is unknown. For packets with variable-length payloads (ArtDmx, ArtNzs, ArtRdm), includes :data as ByteBuffer slice.

encode-by-spec!

(encode-by-spec! buf spec packet)

Encode a packet map to buffer using the given spec. Buffer must be cleared and sized appropriately before calling.

encode-packet!

(encode-packet! buf {:keys [op], :as packet})

Encode a packet map to buffer using a compiled encoder. The :op key determines which encoder to use.

opcode->keyword

Map of numeric opcode -> keyword

packet-size

(packet-size op-keyword)

Get the expected size in bytes for a packet type. Returns nil if the packet type is unknown.

read-bytes

(read-bytes buf offset length)

Read n bytes at offset. Returns a vector of octets.

read-field

(read-field buf field offset)

Read a single field from the buffer according to its spec.

read-fixed-string

(read-fixed-string buf offset max-length)

Read null-terminated ASCII string at offset with max length.

read-ipv4

(read-ipv4 buf offset)

Read a 4-byte IP address at offset. Returns a vector of 4 octets.

read-opcode

(read-opcode buf)

Read the opcode from an Art-Net packet buffer.

read-u16-be

(read-u16-be buf offset)

Read unsigned 16-bit big-endian at offset. Returns long.

read-u16-le

(read-u16-le buf offset)

Read unsigned 16-bit little-endian at offset. Returns long.

read-u8

(read-u8 buf offset)

Read unsigned byte at offset. Returns long.

read-uid

(read-uid buf offset)

Read 6-byte RDM UID at offset. Returns a vector of 6 octets.

slice-payload

(slice-payload buf offset length)

Create a read-only ByteBuffer slice of payload data at offset with a given length. Uses flyweight pattern - no data copying. Returns nil if the length is 0.

valid-artnet-header?

(valid-artnet-header? buf)

Check if buffer starts with valid Art-Net header.

write-bytes!

(write-bytes! buf data length)

Write byte sequence at current position, padding to length with zeros.

write-field!

(write-field! buf field value)

Write a single field to the buffer, according to its spec.

write-fixed-string!

(write-fixed-string! buf s length)

Write null-terminated ASCII string at current position, padding to length.

write-ipv4!

(write-ipv4! buf ip)

Write 4-byte IP address at current position.

write-u16-be!

(write-u16-be! buf value)

Write unsigned 16-bit big-endian at the current position.

write-u16-le!

(write-u16-le! buf value)

Write unsigned 16-bit little-endian at the current position.

write-u8!

(write-u8! buf value)

Write unsigned byte at the current position.

write-uid!

(write-uid! buf uid)

Write 6-byte RDM UID at the current position.