Class: Terminalwire::V2::Server::Rack::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/terminalwire/v2/server/rack.rb

Overview

Incremental parser: feed raw socket chunks, yield [opcode, payload] per message (reassembling fragments, unmasking — client frames are masked).

Defined Under Namespace

Classes: Cursor

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



182
183
184
185
186
# File 'lib/terminalwire/v2/server/rack.rb', line 182

def initialize
  @buf = "".b
  @frag = "".b
  @frag_opcode = nil
end

Instance Method Details

#push(chunk) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/terminalwire/v2/server/rack.rb', line 188

def push(chunk)
  @buf << chunk.b
  while (frame = next_frame)
    fin, opcode, payload = frame
    case opcode
    when 0x0 # continuation
      @frag << payload
      (yield(@frag_opcode, @frag); @frag = "".b; @frag_opcode = nil) if fin
    when 0x1, 0x2 # text / binary
      if fin then yield(opcode, payload) else @frag_opcode = opcode; @frag = payload.b end
    else # control: 0x8 close, 0x9 ping, 0xA pong
      yield(opcode, payload)
    end
  end
end