Class: Spyder::WebSocketStreamingBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/spyder/web_socket_streaming_buffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&frame_callback) ⇒ WebSocketStreamingBuffer

Returns a new instance of WebSocketStreamingBuffer.



8
9
10
11
12
13
# File 'lib/spyder/web_socket_streaming_buffer.rb', line 8

def initialize(&frame_callback)
  @frame_callback = frame_callback
  @on_close = nil
  @buffer = []
  _reset
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



5
6
7
# File 'lib/spyder/web_socket_streaming_buffer.rb', line 5

def buffer
  @buffer
end

#on_closeObject

Returns the value of attribute on_close.



6
7
8
# File 'lib/spyder/web_socket_streaming_buffer.rb', line 6

def on_close
  @on_close
end

Instance Method Details

#feed(buf) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/spyder/web_socket_streaming_buffer.rb', line 15

def feed(buf)
  @buffer += buf.bytes

  # printable = buf.bytes.map{ |x| x.to_s(16).rjust(2, '0').upcase }.join(' ')
  # puts "Got #{buf.bytes.length} more bytes: [#{printable}]"

  iterations = 0
  loop do
    _flush

    iterations += 1
    if iterations > 1_000
      raise "Watchdog error: got stuck on a loop of #{iterations} iterations."
    end

    break if (@buffer.length == 0 && !@need_buffer_length) ||
      (@need_buffer_length && @buffer.length < @need_buffer_length)
  end
end