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

Constructor Details

#initialize(ptr = nil) ⇒ Packer

Creates a new FFI::MsgPack::Packer.

Parameters:

  • ptr (FFI::Pointer) (defaults to: nil)

    An optional pointer to an existing packer.

Since:

  • 0.2.0



73
74
75
76
77
78
79
80
81
# File 'lib/ffi/msgpack/packer.rb', line 73

def initialize(ptr=nil)
  if ptr
    super(ptr)
  else
    super()
  end

  @object = MsgObject.new
end

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:



137
138
139
140
141
142
# File 'lib/ffi/msgpack/packer.rb', line 137

def <<(value)
  @object.set!(value)

  pack(@object)
  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.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ffi/msgpack/packer.rb', line 99

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

    packed = packed_buffer.read_bytes(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.



161
162
163
164
165
166
167
168
169
# File 'lib/ffi/msgpack/packer.rb', line 161

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.



124
125
126
# File 'lib/ffi/msgpack/packer.rb', line 124

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.



151
152
153
# File 'lib/ffi/msgpack/packer.rb', line 151

def to_s
  @buffer.to_s if @buffer
end