Class: FFI::MsgPack::Packer

Inherits:
Struct
  • Object
show all
Defined in:
lib/ffi/msgpack/packer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bufferObject

The optional buffer to write packed Msg Objects into.



12
13
14
# File 'lib/ffi/msgpack/packer.rb', line 12

def buffer
  @buffer
end

#totalObject

The total length of the buffer



15
16
17
# File 'lib/ffi/msgpack/packer.rb', line 15

def total
  @total
end

Class Method Details

.create(buffer = nil) {|packed(,length)| ... } ⇒ Packer

Creates a new packer.

Parameters:

  • buffer (#<<) (defaults to: nil)

    Optional buffer to append packed Msg Objects to.

Yields:

  • (packed(,length))

    If a block is given, it will be used as the callback to write the packed data.

Yield Parameters:

  • packed (String)

    The packed bytes representing a Msg Object.

  • length (Integer)

    The length of the packed data.

Returns:

  • (Packer)

    The new packer.



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/ffi/msgpack/packer.rb', line 36

def Packer.create(buffer=nil,&block)
  packer = Packer.new()

  # zero the memory
  packer[:data] = nil
  packer[:callback] = nil

  # zero the total
  packer.total = 0

  if block
    # disable the buffer
    packer.buffer = nil

    # custom callback
    packer.callback(&block)
  else
    # set the buffer
    packer.buffer = (buffer || '')

    # setup the default callback
    packer.callback do |packed,length|
      packer.buffer << packed
    end
  end

  return packer
end

Instance Method Details

#<<(value) ⇒ Packer

Packs a Ruby object.

Parameters:

Returns:



119
120
121
122
# File 'lib/ffi/msgpack/packer.rb', line 119

def <<(value)
  pack(MsgObject.new_object(value))
  return self
end

#callback {|packed(,length)| ... } ⇒ Proc

Sets the write callback for the packer.

Yields:

  • (packed(,length))

    If a block is given, it will be used as the callback to write the packed data.

Yield Parameters:

  • packed (String)

    The packed bytes representing a Msg Object.

  • length (Integer)

    The length of the packed data.

Returns:

  • (Proc)

    The new callback.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ffi/msgpack/packer.rb', line 81

def callback(&block)
  self[:callback] = Proc.new do |data_ptr,packed_ptr,length|
    @total += length

    packed = packed_ptr.get_bytes(0,length)

    if block.arity == 2
      block.call(packed,length)
    else
      block.call(packed)
    end
    
    0 # return 0 to indicate success
  end
end

#inspectString

Inspects the packer.

Returns:

  • (String)

    The inspected packer.



141
142
143
144
145
146
147
148
149
# File 'lib/ffi/msgpack/packer.rb', line 141

def inspect
  addr = ('0x%x' % self.pointer.address)

  if @buffer
    "#<#{self.class}:#{addr}: #{@buffer.inspect}>"
  else
    "#<#{self.class}:#{addr}>"
  end
end

#pack(msg) ⇒ Integer

Packs a Msg Object.

Parameters:

  • msg (MsgObject)

    The Msg Object to pack.

Returns:

  • (Integer)

    Returns 0 on a successful write, and -1 if an error occurred.



106
107
108
# File 'lib/ffi/msgpack/packer.rb', line 106

def pack(msg)
  MsgPack.msgpack_pack_object(self,msg)
end

#to_sString?

The contents of the buffer.

Returns:

  • (String, nil)

    The contents of the buffer, or nil if only a callback is being used to write the packed data.



131
132
133
# File 'lib/ffi/msgpack/packer.rb', line 131

def to_s
  @buffer.to_s if @buffer
end