Class: FFI::MsgPack::Unpacker

Inherits:
Struct
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ffi/msgpack/unpacker.rb

Constant Summary collapse

CHUNK_SIZE =

Default chunk-size to expand the buffer by

1024
DEFAULT_SIZE =

The default size of the unpacker buffer

CHUNK_SIZE * 4

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*arguments) ⇒ Unpacker

Initializes a new unpacker object.



35
36
37
38
39
40
# File 'lib/ffi/msgpack/unpacker.rb', line 35

def initialize(*arguments)
  super(*arguments)

  @chunk_size = CHUNK_SIZE
  @stream = nil
end

Instance Attribute Details

#chunk_sizeObject

The chunk-size to expand the buffer by



18
19
20
# File 'lib/ffi/msgpack/unpacker.rb', line 18

def chunk_size
  @chunk_size
end

#streamObject

The optional stream to read packed data from



21
22
23
# File 'lib/ffi/msgpack/unpacker.rb', line 21

def stream
  @stream
end

Class Method Details

.create(size = DEFAULT_SIZE) ⇒ Unpacker

Creates a new unpacker object.

Parameters:

  • size (Integer) (defaults to: DEFAULT_SIZE)

    The buffer size of the unpacker.

Returns:



51
52
53
# File 'lib/ffi/msgpack/unpacker.rb', line 51

def Unpacker.create(size=DEFAULT_SIZE)
  Unpacker.new(MsgPack.msgpack_unpacker_new(size))
end

.release(ptr) ⇒ Object

Destroys a previously allocated unpacker object.

Parameters:

  • ptr (FFI::Pointer)

    The pointer to the allocated unpacker.



61
62
63
# File 'lib/ffi/msgpack/unpacker.rb', line 61

def Unpacker.release(ptr)
  MsgPack.msgpack_unpacker_free(ptr)
end

Instance Method Details

#<<(packed) ⇒ Unpacker

Writes packed data into the buffer of the unpacker.

Parameters:

  • packed (String)

    The packed data.

Returns:



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ffi/msgpack/unpacker.rb', line 74

def <<(packed)
  # make sure we have space in the buffer
  reserve_buffer(packed.length)

  # copy in the bytes
  self[:buffer].put_bytes(buffer_offset,packed)

  # advace the buffer position
  buffer_consumed!(packed.length)
  return self
end

#buffer_capacityInteger (protected)

The remaining space of the buffer.

Returns:

  • (Integer)

    The number of bytes free in the buffer.



207
208
209
# File 'lib/ffi/msgpack/unpacker.rb', line 207

def buffer_capacity
  self[:free]
end

#buffer_consumed!(size) ⇒ nil (protected)

Consumes space in the buffer.

Parameters:

  • size (Integer)

    The number of bytes to be consumed.

Returns:

  • (nil)


219
220
221
222
223
224
# File 'lib/ffi/msgpack/unpacker.rb', line 219

def buffer_consumed!(size)
  self[:used] += size
  self[:free] -= size

  return nil
end

#buffer_offsetInteger (protected)

The offset to empty space in the buffer.

Returns:

  • (Integer)

    The number of bytes within the buffer.



197
198
199
# File 'lib/ffi/msgpack/unpacker.rb', line 197

def buffer_offset
  self[:used]
end

#each {|obj| ... } ⇒ Enumerator, Unpacker

Enumerates over each Msg Object from the buffer in the unpacker.

Yields:

  • (obj)

    The given block will be passed each unpacked Ruby Object, from the buffer of the unpacker.

Yield Parameters:

Returns:

  • (Enumerator, Unpacker)

    If no block is given, an enumerator will be returned.



164
165
166
167
168
169
170
# File 'lib/ffi/msgpack/unpacker.rb', line 164

def each
  return enum_for(:each) unless block_given?

  each_object do |obj|
    yield obj.to_ruby
  end
end

#each_object {|obj| ... } ⇒ Unpacker

Enumerates over each Msg Object from the buffer in the unpacker.

If #stream is set, packed data will be read from it, when the buffer of the unpacker is fully unpacked.

Yields:

  • (obj)

    The given block will be passed each Msg Object.

Yield Parameters:

  • obj (MsgObject)

    An unpacked Msg Object.

Returns:



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/ffi/msgpack/unpacker.rb', line 123

def each_object
  loop do
    ret = MsgPack.msgpack_unpacker_execute(self)

    if ret > 0
      # copy out the next Msg Object and release it's zone
      obj = MsgPack.msgpack_unpacker_data(self)
      zone = MsgPack.msgpack_unpacker_release_zone(self)

      # reset the unpacker
      MsgPack.msgpack_unpacker_reset(self)

      yield obj

      # free the zone now that we are done with it
      MsgPack.msgpack_zone_free(zone)
    elsif ret < 0
      raise(ParseError,"a parse error occurred",caller)
    else
      unless (@stream && read(@stream))
        break
      end
    end
  end

  return self
end

#message_sizeInteger (protected)

The size of the unparsed message in the buffer.

Returns:

  • (Integer)

    The number of bytes that are unparsed.



232
233
234
# File 'lib/ffi/msgpack/unpacker.rb', line 232

def message_size
  self[:parsed] - self[:off] + self[:used]
end

#parsed_sizeInteger (protected)

The number of bytes that have been parsed in the buffer.

Returns:

  • (Integer)

    The number of parsed bytes.



242
243
244
# File 'lib/ffi/msgpack/unpacker.rb', line 242

def parsed_size
  self[:parsed]
end

#read(io) ⇒ Boolean

Reads packed data from a stream into the buffer of the unpacker.

Parameters:

  • io (IO)

    The stream to read the packed data from.

Returns:

  • (Boolean)

    Specifies whether data was read from the stream, or if the stream is empty.



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ffi/msgpack/unpacker.rb', line 96

def read(io)
  reserve_buffer(@chunk_size)
  result = io.read(buffer_capacity)

  unless (result.nil? || result.empty?)
    self << result
    return true
  else
    return false
  end
end

#reserve_buffer(size) ⇒ Boolean (protected)

Reserves space in the buffer.

Parameters:

  • size (Integer)

    The number of bytes to reserve.

Returns:

  • (Boolean)

    Specifies whether the size has been successfully reserved.



183
184
185
186
187
188
189
# File 'lib/ffi/msgpack/unpacker.rb', line 183

def reserve_buffer(size)
  if self[:free] >= size
    true
  else
    MsgPack.msgpack_unpacker_expand_buffer(self,size)
  end
end