Class: FFI::MsgPack::Unpacker
- Inherits:
-
Struct
- Object
- Struct
- FFI::MsgPack::Unpacker
- 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
-
#chunk_size ⇒ Object
The chunk-size to expand the buffer by.
-
#stream ⇒ Object
The optional stream to read packed data from.
Class Method Summary collapse
-
.create(size = DEFAULT_SIZE) ⇒ Unpacker
Creates a new unpacker object.
-
.release(ptr) ⇒ Object
Destroys a previously allocated unpacker object.
Instance Method Summary collapse
-
#<<(packed) ⇒ Unpacker
Writes packed data into the buffer of the unpacker.
-
#buffer_capacity ⇒ Integer
protected
The remaining space of the buffer.
-
#buffer_consumed!(size) ⇒ nil
protected
Consums space in the buffer.
-
#buffer_offset ⇒ Integer
protected
The offset to empty space in the buffer.
-
#each {|obj| ... } ⇒ Enumerator, Unpacker
Enumerates over each Msg Object from the buffer in the unpacker.
-
#each_object {|obj| ... } ⇒ Unpacker
Enumerates over each Msg Object from the buffer in the unpacker.
-
#initialize(*arguments) ⇒ Unpacker
constructor
Initializes a new unpacker object.
-
#message_size ⇒ Integer
protected
The size of the unparsed message in the buffer.
-
#parsed_size ⇒ Integer
protected
The number of bytes that have been parsed in the buffer.
-
#read(io) ⇒ Boolean
Reads packed data from a stream into the buffer of the unpacker.
-
#reserve_buffer(size) ⇒ Boolean
protected
Reserves space in the buffer.
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_size ⇒ Object
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 |
#stream ⇒ Object
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
Instance Method Details
#<<(packed) ⇒ Unpacker
Writes packed data into the buffer of the unpacker.
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_capacity ⇒ Integer (protected)
The remaining space of the buffer.
207 208 209 |
# File 'lib/ffi/msgpack/unpacker.rb', line 207 def buffer_capacity self[:free] end |
#buffer_consumed!(size) ⇒ nil (protected)
Consums space in the buffer.
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_offset ⇒ Integer (protected)
The offset to empty space in 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.
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.
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_size ⇒ Integer (protected)
The size of the unparsed message in the buffer.
232 233 234 |
# File 'lib/ffi/msgpack/unpacker.rb', line 232 def self[:parsed] - self[:off] + self[:used] end |
#parsed_size ⇒ Integer (protected)
The number of bytes that have been parsed in the buffer.
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.
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.
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.(self,size) end end |