Module: NewBridge::Framing

Defined in:
lib/new_bridge/framing.rb

Overview

Length-prefixed frames: 4-byte little-endian uint32 payload length, then payload.

Defined Under Namespace

Classes: Error, TruncatedFrame

Class Method Summary collapse

Class Method Details

.read_frame(io) ⇒ Object

Raises:



19
20
21
22
23
24
25
# File 'lib/new_bridge/framing.rb', line 19

def read_frame(io)
  len_bytes = read_exact(io, 4)
  len = len_bytes.unpack1('V')
  raise Error, "invalid frame length #{len}" if len > 256 * 1024 * 1024 # 256 MiB cap for phase 0

  read_exact(io, len)
end

.write_frame(io, payload) ⇒ Object

Raises:



11
12
13
14
15
16
17
# File 'lib/new_bridge/framing.rb', line 11

def write_frame(io, payload)
  raise Error, 'payload must be String' unless payload.is_a?(String)

  io.write([payload.bytesize].pack('V'))
  io.write(payload)
  io.flush
end