Class: Biryani::Frame::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/biryani/frame/data.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(end_stream, stream_id, data, padding) ⇒ Data

Returns a new instance of Data.

Parameters:

  • end_stream (Boolean)
  • stream_id (Integer)
  • data (String)
  • padding (String, nil)


10
11
12
13
14
15
16
# File 'lib/biryani/frame/data.rb', line 10

def initialize(end_stream, stream_id, data, padding)
  @f_type = FrameType::DATA
  @end_stream = end_stream
  @stream_id = stream_id
  @data = data
  @padding = padding
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/biryani/frame/data.rb', line 4

def data
  @data
end

#f_typeObject (readonly)

Returns the value of attribute f_type.



4
5
6
# File 'lib/biryani/frame/data.rb', line 4

def f_type
  @f_type
end

#paddingObject (readonly)

Returns the value of attribute padding.



4
5
6
# File 'lib/biryani/frame/data.rb', line 4

def padding
  @padding
end

#stream_idObject (readonly)

Returns the value of attribute stream_id.



4
5
6
# File 'lib/biryani/frame/data.rb', line 4

def stream_id
  @stream_id
end

Class Method Details

.read(s, flags, stream_id) ⇒ Data

Parameters:

  • s (String)
  • flags (Integer)
  • stream_id (Integer)

Returns:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/biryani/frame/data.rb', line 48

def self.read(s, flags, stream_id)
  padded = Frame.read_padded(flags)
  end_stream = Frame.read_end_stream(flags)

  if padded
    io = IO::Buffer.for(s)
    pad_length = io.get_value(:U8, 0)
    data_length = s.bytesize - pad_length - 1
    data = io.get_string(1, data_length)
    padding = io.get_string(1 + data_length)
    return ConnectionError.new(ErrorCode::PROTOCOL_ERROR, 'invalid frame') if padding.bytesize != pad_length
  else
    data = s
  end

  Data.new(end_stream, stream_id, data, padding)
end

Instance Method Details

#end_stream?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/biryani/frame/data.rb', line 24

def end_stream?
  @end_stream
end

#lengthInteger

Returns:

  • (Integer)


29
30
31
# File 'lib/biryani/frame/data.rb', line 29

def length
  @data.bytesize + (padded? ? 1 + @padding.bytesize : 0)
end

#padded?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/biryani/frame/data.rb', line 19

def padded?
  !@padding.nil?
end

#to_binary_sString

Returns:

  • (String)


34
35
36
37
38
39
40
41
# File 'lib/biryani/frame/data.rb', line 34

def to_binary_s
  payload_length = length
  flags = Frame.to_flags(padded: padded?, end_stream: end_stream?)
  pad_length = padded? ? @padding.bytesize.chr : ''
  padding = @padding || ''

  Frame.to_binary_s_header(payload_length, @f_type, flags, @stream_id) + pad_length + @data + padding
end