Class: Bones::RPC::Adapter::Msgpack::Ruby::Decoder

Inherits:
Object
  • Object
show all
Defined in:
lib/bones/rpc/adapter/msgpack/ruby.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer) ⇒ Decoder

Returns a new instance of Decoder.



15
16
17
# File 'lib/bones/rpc/adapter/msgpack/ruby.rb', line 15

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

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



13
14
15
# File 'lib/bones/rpc/adapter/msgpack/ruby.rb', line 13

def buffer
  @buffer
end

Instance Method Details

#consume_array(size) ⇒ Object



100
101
102
# File 'lib/bones/rpc/adapter/msgpack/ruby.rb', line 100

def consume_array(size)
  Array.new(size) { consume_next }
end

#consume_byteObject



60
61
62
# File 'lib/bones/rpc/adapter/msgpack/ruby.rb', line 60

def consume_byte
  buffer.getbyte
end

#consume_doubleObject



89
90
91
92
# File 'lib/bones/rpc/adapter/msgpack/ruby.rb', line 89

def consume_double
  d, = buffer.read(8).unpack(DOUBLE_FMT)
  d
end

#consume_floatObject



84
85
86
87
# File 'lib/bones/rpc/adapter/msgpack/ruby.rb', line 84

def consume_float
  f, = buffer.read(4).unpack(FLOAT_FMT)
  f
end

#consume_int16Object



64
65
66
# File 'lib/bones/rpc/adapter/msgpack/ruby.rb', line 64

def consume_int16
  (consume_byte << 8) | consume_byte
end

#consume_int32Object



68
69
70
# File 'lib/bones/rpc/adapter/msgpack/ruby.rb', line 68

def consume_int32
  (consume_byte << 24) | (consume_byte << 16) | (consume_byte << 8) | consume_byte
end

#consume_int64Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/bones/rpc/adapter/msgpack/ruby.rb', line 72

def consume_int64
  n  = (consume_byte << 56)
  n |= (consume_byte << 48)
  n |= (consume_byte << 40)
  n |= (consume_byte << 32)
  n |= (consume_byte << 24)
  n |= (consume_byte << 16)
  n |= (consume_byte << 8)
  n |=  consume_byte
  n
end

#consume_nextObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/bones/rpc/adapter/msgpack/ruby.rb', line 104

def consume_next
  b = consume_byte
  if (method = DECODINGS[b])
    method.call(self)
  elsif b <= 0b01111111
    b
  elsif b & 0b11100000 == 0b11100000
    b - 0x100
  elsif b & 0b11100000 == 0b10100000
    size = b & 0b00011111
    consume_string(size)
  elsif b & 0b11110000 == 0b10010000
    size = b & 0b00001111
    consume_array(size)
  elsif b & 0b11110000 == 0b10000000
    size = b & 0b00001111
    Hash[*consume_array(size * 2)]
  end
end

#consume_string(size, encoding = Encoding::UTF_8) ⇒ Object



94
95
96
97
98
# File 'lib/bones/rpc/adapter/msgpack/ruby.rb', line 94

def consume_string(size, encoding=Encoding::UTF_8)
  s = buffer.read(size)
  s.force_encoding(encoding)
  s
end

#nextObject Also known as: read



19
20
21
22
23
24
25
# File 'lib/bones/rpc/adapter/msgpack/ruby.rb', line 19

def next
  buffer.transaction do
    consume_next
  end
rescue TypeError
  raise EOFError
end