Class: Bones::RPC::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/bones/rpc/parser.rb,
lib/bones/rpc/parser/buffer.rb

Defined Under Namespace

Classes: Buffer

Constant Summary collapse

EXT8 =
[0xC7].pack('C').freeze
EXT16 =
[0xC8].pack('C').freeze
EXT32 =
[0xC9].pack('C').freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream, adapter) ⇒ Parser

Returns a new instance of Parser.


18
19
20
21
# File 'lib/bones/rpc/parser.rb', line 18

def initialize(stream, adapter)
  @stream = stream.force_encoding('BINARY')
  @adapter = Adapter.get(adapter)
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter


12
13
14
# File 'lib/bones/rpc/parser.rb', line 12

def adapter
  @adapter
end

#streamObject (readonly)

Returns the value of attribute stream


12
13
14
# File 'lib/bones/rpc/parser.rb', line 12

def stream
  @stream
end

Instance Method Details

#bufferObject


14
15
16
# File 'lib/bones/rpc/parser.rb', line 14

def buffer
  @buffer ||= Bones::RPC::Parser::Buffer.new(@stream)
end

#parserObject


23
24
25
# File 'lib/bones/rpc/parser.rb', line 23

def parser
  @parser ||= @adapter.parser(@stream)
end

#readObject


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bones/rpc/parser.rb', line 27

def read
  sync do
    b = buffer.getc
    buffer.ungetc(b)
    if b.nil?
      raise EOFError
    elsif b.start_with?(EXT8)
      buffer.skip(1)
      len, = buffer.read(1).unpack('C')
      type, = buffer.read(1).unpack('C')
      check_ext!(type)
      head, = buffer.read(1).unpack('C')
      data = buffer.read(len-1)
      parse_ext!(head, data)
    elsif b.start_with?(EXT16)
      buffer.skip(1)
      len, = buffer.read(2).unpack('n')
      type, = buffer.read(1).unpack('C')
      check_ext!(type)
      head, = buffer.read(1).unpack('C')
      data = buffer.read(len-1)
      parse_ext!(head, data)
    elsif b.start_with?(EXT32)
      buffer.skip(1)
      len, = buffer.read(4).unpack('N')
      type, = buffer.read(1).unpack('C')
      check_ext!(type)
      head, = buffer.read(1).unpack('C')
      data = buffer.read(len-1)
      parse_ext!(head, data)
    else
      object = parser.read
      buffer.seek(parser.unpacker_pos)
      map_from!(object)
    end
  end
end