Sha256: 428944d65176f44c8f34db9047f9ec0c4b5212a5c64caf8f3a22087a47927431

Contents?: true

Size: 1.8 KB

Versions: 28

Compression:

Stored size: 1.8 KB

Contents

module GorgonBunny
  # @private
  module Framing
    ENCODINGS_SUPPORTED = defined? Encoding
    HEADER_SLICE = (0..6).freeze
    DATA_SLICE = (7..-1).freeze
    PAYLOAD_SLICE = (0..-2).freeze

    # @private
    module String
      class Frame < GorgonAMQ::Protocol::Frame
        def self.decode(string)
          header              = string[HEADER_SLICE]
          type, channel, size = self.decode_header(header)
          data                = string[DATA_SLICE]
          payload             = data[PAYLOAD_SLICE]
          frame_end           = data[-1, 1]

          frame_end.force_encoding(GorgonAMQ::Protocol::Frame::FINAL_OCTET.encoding) if ENCODINGS_SUPPORTED

          # 1) the size is miscalculated
          if payload.bytesize != size
            raise BadLengthError.new(size, payload.bytesize)
          end

          # 2) the size is OK, but the string doesn't end with FINAL_OCTET
          raise NoFinalOctetError.new if frame_end != GorgonAMQ::Protocol::Frame::FINAL_OCTET

          self.new(type, payload, channel)
        end
      end
    end # String


    # @private
    module IO
      class Frame < GorgonAMQ::Protocol::Frame
        def self.decode(io)
          header = io.read(7)
          type, channel, size = self.decode_header(header)
          data = io.read_fully(size + 1)
          payload, frame_end = data[PAYLOAD_SLICE], data[-1, 1]

          # 1) the size is miscalculated
          if payload.bytesize != size
            raise BadLengthError.new(size, payload.bytesize)
          end

          # 2) the size is OK, but the string doesn't end with FINAL_OCTET
          raise NoFinalOctetError.new if frame_end != GorgonAMQ::Protocol::Frame::FINAL_OCTET
          self.new(type, payload, channel)
        end # self.from
      end # Frame
    end # IO
  end # Framing
end # GorgonBunny

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
gorgon-0.6.5 lib/gorgon_bunny/lib/gorgon_bunny/framing.rb
gorgon-0.6.4 lib/gorgon_bunny/lib/gorgon_bunny/framing.rb
gorgon-0.6.3 lib/gorgon_bunny/lib/gorgon_bunny/framing.rb
gorgon-0.6.2 lib/gorgon_bunny/lib/gorgon_bunny/framing.rb
gorgon-0.6.1 lib/gorgon_bunny/lib/gorgon_bunny/framing.rb
gorgon-0.6.0 lib/gorgon_bunny/lib/gorgon_bunny/framing.rb
gorgon-0.6.0.rc2 lib/gorgon_bunny/lib/gorgon_bunny/framing.rb
gorgon-0.6.0.rc1 lib/gorgon_bunny/lib/gorgon_bunny/framing.rb