Class: GrpcKit::Transport::Packable::Unpacker

Inherits:
Object
  • Object
show all
Defined in:
lib/grpc_kit/transport/packable.rb

Constant Summary collapse

PREFIX_SIZE =

Compressed-Flag (1 byte) + Message-Length (4 Bytes)

5

Instance Method Summary collapse

Constructor Details

#initializeUnpacker

Returns a new instance of Unpacker.



30
31
32
# File 'lib/grpc_kit/transport/packable.rb', line 30

def initialize
  @data = +''.b
end

Instance Method Details

#data_exist?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/grpc_kit/transport/packable.rb', line 35

def data_exist?
  !@data.empty?
end

#feed(data) ⇒ void

This method returns an undefined value.

Parameters:

  • data (String)


41
42
43
# File 'lib/grpc_kit/transport/packable.rb', line 41

def feed(data)
  @data << data
end

#readnil, Array<Boolean, Integer, String>

Returns:

  • (nil, Array<Boolean, Integer, String>)


46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/grpc_kit/transport/packable.rb', line 46

def read
  return nil if @data.bytesize < PREFIX_SIZE

  prefix = @data.byteslice(0, PREFIX_SIZE)
  compressed, length = prefix.unpack('CN')

  return nil if (@data.bytesize-PREFIX_SIZE) < length

  d = @data.freeze
  data = d.byteslice(PREFIX_SIZE, length)
  @data = d.byteslice(PREFIX_SIZE + length, d.bytesize)
  [compressed == 1, length, data]
end