Class: HTTP2Next::FrameBuffer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFrameBuffer

Returns a new instance of FrameBuffer.



106
107
108
109
# File 'lib/http/2/next/flow_buffer.rb', line 106

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

Instance Attribute Details

#bytesizeObject (readonly)

Returns the value of attribute bytesize.



104
105
106
# File 'lib/http/2/next/flow_buffer.rb', line 104

def bytesize
  @bytesize
end

Instance Method Details

#<<(frame) ⇒ Object



111
112
113
114
# File 'lib/http/2/next/flow_buffer.rb', line 111

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

#empty?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/http/2/next/flow_buffer.rb', line 116

def empty?
  @bytesize == 0
end

#retrieve(window_size) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/http/2/next/flow_buffer.rb', line 120

def retrieve(window_size)
  return if @buffer.empty?

  frame = @buffer.first

  frame_size = frame[:payload].bytesize
  end_stream = frame[:flags].include? :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 == 0 && end_stream)

  @buffer.shift

  if frame_size > window_size
    payload = frame.delete(:payload)
    chunk   = frame.dup

    # Split frame so that it fits in the window
    # TODO: consider padding!
    frame_bytes = payload.byteslice(0, window_size)
    payload = payload.byteslice(window_size..-1)

    frame[:payload] = frame_bytes
    frame[:length] = frame_bytes.bytesize
    chunk[:payload] = payload
    chunk[:length]  = payload.bytesize

    # if no longer last frame in sequence...
    frame[:flags] -= [:end_stream] if end_stream

    @buffer.unshift(chunk)
    @bytesize -= window_size
  else
    @bytesize -= frame_size
  end
  frame
end