Class: HTTP2::FrameBuffer

Inherits:
Object
  • Object
show all
Includes:
BufferUtils
Defined in:
lib/http/2/flow_buffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BufferUtils

#append_str, #read_str, #read_uint32

Constructor Details

#initializeFrameBuffer

Returns a new instance of FrameBuffer.



122
123
124
125
# File 'lib/http/2/flow_buffer.rb', line 122

def initialize
  @buffer = []
  @bytesize = 0
end

Instance Attribute Details

#bytesizeObject (readonly)

Returns the value of attribute bytesize.



120
121
122
# File 'lib/http/2/flow_buffer.rb', line 120

def bytesize
  @bytesize
end

Instance Method Details

#<<(frame) ⇒ Object



127
128
129
130
# File 'lib/http/2/flow_buffer.rb', line 127

def <<(frame)
  @buffer << frame
  @bytesize += frame[:payload].bytesize
end

#clearObject



132
133
134
135
# File 'lib/http/2/flow_buffer.rb', line 132

def clear
  @buffer.clear
  @bytesize = 0
end

#empty?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/http/2/flow_buffer.rb', line 137

def empty?
  @buffer.empty?
end

#retrieve(window_size) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/http/2/flow_buffer.rb', line 141

def retrieve(window_size)
  frame = @buffer.first or return

  frame_size = frame[:payload].bytesize
  end_stream = frame[:flags].anybits?(END_STREAM)

  # Frames with zero length with the END_STREAM flag set (that
  # is, an empty DATA frame) MAY be sent if there is no available space
  # in either flow control window.
  return if window_size <= 0 && !(frame_size.zero? && end_stream)

  if frame_size > window_size
    chunk = frame.dup

    # Split frame so that it fits in the window
    # TODO: consider padding!

    chunk[:payload] = read_str(frame[:payload], window_size) # mutates frame[:payload]
    chunk[:length]  = window_size
    frame[:length] = frame_size - window_size

    # if no longer last frame in sequence...
    chunk[:flags] ^= END_STREAM if end_stream

    @bytesize -= window_size
    chunk
  else
    @bytesize -= frame_size
    @buffer.shift
  end
end