Sha256: 10b969144e62e1b8c7fb999679ad40e4a9c1e8370d3368c1294c12f0475aad5d

Contents?: true

Size: 1.7 KB

Versions: 6

Compression:

Stored size: 1.7 KB

Contents

module ASIR
  class Transport
    # !SLIDE
    # Payload IO for Transport
    #
    # Framing
    # * Header line containing the number of bytes in the payload.
    # * The payload bytes.
    # * Blank line.
    # * Footer.
    module PayloadIO
      class UnexpectedResponse < Error;
        attr_accessor :expected, :received
      end

      HEADER = "# asir_payload_size: "
      FOOTER = "\n# asir_payload_end"

      def _write payload, stream, state
        stream.write HEADER
        stream.puts payload.size
        stream.write payload
        stream.puts FOOTER
        stream.flush
        stream
      end

      def _read stream, state
        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, consume = nil
        ok = false
        until ok
          line = stream.readline
          _log { "_read_line_and_expect! #{stream} #{line.inspect}" }
          ok = consume && consume.match(line) ? false : true
        end
        unless match = regexp.match(line)
          _log { "_read_line_and_expect! #{stream} #{regexp.inspect} !~ #{line.inspect}" }
          exc = UnexpectedResponse.new("expected #{regexp.inspect}, received #{line.inspect}")
          exc.expected = regexp
          exc.received = line
          raise exc
        end
        match
      end

      # !SLIDE pause
      def close
        if @stream
          _before_close! stream if respond_to?(:_before_close!)
          @stream.close
        end
      ensure
        @stream = nil unless Channel === @stream
      end

      # !SLIDE resume
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
asir-1.2.11 lib/asir/transport/payload_io.rb
asir-1.2.10 lib/asir/transport/payload_io.rb
asir-1.2.9 lib/asir/transport/payload_io.rb
asir-1.2.8 lib/asir/transport/payload_io.rb
asir-1.2.7 lib/asir/transport/payload_io.rb
asir-1.2.6 lib/asir/transport/payload_io.rb