class Plum::Frame
Constants
- FRAME_FLAGS
- FRAME_FLAGS_MAP
@!visibility private
- FRAME_TYPES
- FRAME_TYPES_INVERSE
@!visibility private
- SETTINGS_TYPE
Attributes
- Integer
-
Flags. 8-bit
- String
-
The payload. Value is frozen.
- Integer
-
Stream
Identifier. Unsigned 31-bit integer
- Integer
-
Frame
type. 8-bit
Public Class Methods
# File lib/plum/frame.rb, line 86 def initialize(type: nil, type_value: nil, flags: nil, flags_value: nil, stream_id: nil, payload: nil) @payload = payload || "" @length = @payload.bytesize @type_value = type_value or self.type = type @flags_value = flags_value or self.flags = flags @stream_id = stream_id or raise ArgumentError.new("stream_id is necessary") end
Parses a frame from given buffer. It changes given buffer. @param buffer [String] The buffer stored the data received from peer. Encoding must be Encoding::BINARY. @return [Frame, nil] The parsed frame or nil if the buffer is imcomplete.
# File lib/plum/frame.rb, line 157 def self.parse!(buffer) return nil if buffer.bytesize < 9 # header: 9 bytes length = buffer.uint24 return nil if buffer.bytesize < 9 + length cur = buffer.byteshift(9 + length) type_value, flags_value, r_sid = cur.byteslice(3, 6).unpack("CCN") # r = r_sid >> 31 # currently not used stream_id = r_sid # & ~(1 << 31) self.new(type_value: type_value, flags_value: flags_value, stream_id: stream_id, payload: cur.byteslice(9, length)).freeze end
Public Instance Methods
Assembles the frame into binary representation. @return [String] Binary representation of this frame.
# File lib/plum/frame.rb, line 142 def assemble [length / 0x100, length % 0x100, @type_value, @flags_value, @stream_id].pack("nCCCN") << @payload end
Returns the set flags on the frame. @return [Array<Symbol>] The flags.
# File lib/plum/frame.rb, line 114 def flags fs = FRAME_FLAGS[type] [0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80] .select {|v| @flags_value & v > 0 } .map {|val| fs && fs.key(val) || ("unknown_%02x" % val).to_sym } end
Sets the frame flags. @param values [Array<Symbol>] The flags.
# File lib/plum/frame.rb, line 123 def flags=(values) val = 0 FRAME_FLAGS_MAP.values_at(*values).each { |c| val |= c if c } @flags_value = val end
@private
# File lib/plum/frame.rb, line 150 def inspect "#<Plum::Frame:0x%04x} length=%d, type=%p, flags=%p, stream_id=0x%04x, payload=%p>" % [__id__, length, type, flags, stream_id, payload] end
Returns the length of payload. @return [Integer] The length.
# File lib/plum/frame.rb, line 96 def length @length end
Returns the type of the frame in Symbol. @return [Symbol] The type.
# File lib/plum/frame.rb, line 102 def type FRAME_TYPES_INVERSE[@type_value] || ("unknown_%02x" % @type_value).to_sym end
Sets the frame type. @param value [Symbol] The type.
# File lib/plum/frame.rb, line 108 def type=(value) @type_value = FRAME_TYPES[value] or raise ArgumentError.new("unknown frame type: #{value}") end