lib/asir/transport/payload_io.rb in asir-0.2.0 vs lib/asir/transport/payload_io.rb in asir-1.0.1
- old
+ new
@@ -2,28 +2,32 @@
class Transport
# !SLIDE
# Payload IO for Transport
#
# Framing
- # * Line containing the number of bytes in the payload.
+ # * Header line containing the number of bytes in the payload.
# * The payload bytes.
# * Blank line.
+ # * Footer.
module PayloadIO
class UnexpectedResponse < Error; end
- NEWLINE = "\n"
+ HEADER = "# asir_payload_size: "
+ FOOTER = "\n# asir_payload_end"
def _write payload, stream
+ stream.write HEADER
stream.puts payload.size
stream.write payload
- stream.write NEWLINE
+ stream.puts FOOTER
stream.flush
stream
end
def _read stream
- size = stream.readline.chomp.to_i
+ size = /\d+$/.match(stream.readline.chomp)[0].to_i # HEADER (size)
payload = stream.read(size)
+ stream.readline # FOOTER
stream.readline
payload
end
def _read_line_and_expect! stream, regexp