Class: PiAgent::Framer
- Inherits:
-
Object
- Object
- PiAgent::Framer
- Defined in:
- lib/pi_agent/framer.rb
Overview
Strict LF-only line framer for the pi RPC protocol.
The protocol explicitly forbids generic line readers (Ruby ‘readline`, Node `readline`) because they also split on U+2028 / U+2029, which are valid inside JSON strings.
Feed bytes; yield complete lines with any trailing CR stripped. Empty lines are dropped (the protocol uses single LFs between records).
Constant Summary collapse
- LF =
"\n"
Instance Method Summary collapse
- #buffered? ⇒ Boolean
- #feed(bytes) ⇒ Object
-
#initialize ⇒ Framer
constructor
A new instance of Framer.
Constructor Details
#initialize ⇒ Framer
Returns a new instance of Framer.
15 16 17 |
# File 'lib/pi_agent/framer.rb', line 15 def initialize @buffer = String.new(encoding: Encoding::BINARY) end |
Instance Method Details
#buffered? ⇒ Boolean
31 32 33 |
# File 'lib/pi_agent/framer.rb', line 31 def buffered? !@buffer.empty? end |
#feed(bytes) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/pi_agent/framer.rb', line 19 def feed(bytes) @buffer << bytes.b while (idx = @buffer.index(LF)) line = @buffer.byteslice(0, idx) @buffer = @buffer.byteslice(idx + 1, @buffer.bytesize - idx - 1) || String.new(encoding: Encoding::BINARY) line.chomp!("\r") next if line.empty? yield line.force_encoding(Encoding::UTF_8) end end |