Module: Igniter::Store::WireProtocol
- Included in:
- Codecs::CompactDelta, Codecs::JsonCrc32, FileBackend, NativeFileBackendSnapshotSupport, NetworkBackend, NetworkBackend::Subscription, SegmentedFileBackend, StoreServer, TCPAdapter
- Defined in:
- lib/igniter/store/wire_protocol.rb
Overview
Shared CRC32-framed binary encoding for WAL files and network transport.
Frame layout:
[4 bytes BE uint32: body_len][body_len bytes: body][4 bytes BE uint32: CRC32(body)]
A frame with a mismatched CRC or truncated body signals corruption / connection loss — the caller should stop reading.
Constant Summary collapse
- FRAME_HEADER_SIZE =
4- FRAME_CRC_SIZE =
4
Instance Method Summary collapse
- #encode_frame(body) ⇒ Object
-
#read_frame(io) ⇒ Object
Reads one frame from
io.
Instance Method Details
#encode_frame(body) ⇒ Object
18 19 20 21 |
# File 'lib/igniter/store/wire_protocol.rb', line 18 def encode_frame(body) body_b = body.b [body_b.bytesize].pack("N") << body_b << [Zlib.crc32(body)].pack("N") end |
#read_frame(io) ⇒ Object
Reads one frame from io. Returns the body String on success, nil on truncation or CRC mismatch.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/igniter/store/wire_protocol.rb', line 25 def read_frame(io) header = io.read(FRAME_HEADER_SIZE) return nil if header.nil? || header.bytesize < FRAME_HEADER_SIZE len = header.unpack1("N") body = io.read(len) return nil if body.nil? || body.bytesize < len crc_bytes = io.read(FRAME_CRC_SIZE) return nil if crc_bytes.nil? || crc_bytes.bytesize < FRAME_CRC_SIZE return nil unless Zlib.crc32(body) == crc_bytes.unpack1("N") body end |