Module: RocketAMF

Defined in:
lib/rocketamf.rb,
lib/rocketamf/ext.rb,
lib/rocketamf/pure.rb,
lib/rocketamf/constants.rb,
lib/rocketamf/pure/serializer.rb,
lib/rocketamf/errors/amf_error.rb,
lib/rocketamf/types/typed_hash.rb,
lib/rocketamf/pure/deserializer.rb,
lib/rocketamf/mapping/mapping_set.rb,
lib/rocketamf/mapping/class_mapping.rb,
lib/rocketamf/pure/helpers/object_cache.rb,
lib/rocketamf/pure/helpers/string_cache.rb,
lib/rocketamf/errors/amf_error_incomplete.rb,
lib/rocketamf/pure/helpers/io_helper_base.rb,
lib/rocketamf/pure/helpers/io_helper_read.rb,
lib/rocketamf/pure/helpers/io_helper_write.rb,
ext/rocketamf_ext/rocketamf_ext.c

Overview

MrPin RocketAMF is a full featured AMF3 serializer and deserializer with support for bi-directional Flash to Ruby class mapping, custom serialization and mapping, remoting gateway helpers that follow AMF3 messaging specs, and a suite of specs to ensure adherence to the specification documents put out by Adobe. If the C components compile, then RocketAMF automatically takes advantage of them to provide a substantial performance benefit. In addition, RocketAMF is fully compatible with Ruby 2.0, 2.1.

Performance

RocketAMF provides native C extensions for serialization, deserialization, remoting, and class mapping. If your environment supports them, RocketAMF will automatically take advantage of the C serializer, deserializer, and remoting support. The C class mapper has some substantial performance optimizations that make it incompatible with the pure Ruby class mapper, and so it must be manually enabled. For more information see RocketAMF::ClassMapping. Below are some benchmarks I took using using a simple little benchmarking utility I whipped up, which can be found in the root of the repository.

todo:change benchmark

# 100000 objects
# Ruby 1.8
Testing native AMF3:
  minimum serialize time: 1.444652s
  minimum deserialize time: 0.879407s
Testing pure AMF3:
  minimum serialize time: 31.637864s
  minimum deserialize time: 14.773969s

Serialization & Deserialization

RocketAMF provides two main methods - serialize and deserialize. Deserialization takes a String or StringIO object and the AMF version if different from the default. Serialization takes any Ruby object and the version if different from the default. AMF3 not sending duplicate data.

Mapping Classes Between Flash and Ruby

RocketAMF provides a simple class mapping tool to facilitate serialization and deserialization of typed objects. Refer to the documentation of RocketAMF::ClassMapping for more details. If the provided class mapping tool is not sufficient for your needs, you also have the option to replace it with a class mapper of your own devising that matches the documented API.

Advanced Serialization (encode_amf and IExternalizable)

RocketAMF provides some additional functionality to support advanced serialization techniques. If you define an encode_amf method on your object, it will get called during serialization. It is passed a single argument, the serializer, and it can use the serializer stream, the serialize method, the write_array method, the write_object method, and the serializer version. Below is a simple example that uses write_object to customize the property hash that is used for serialization.

Example:

class TestObject
  def encode_amf ser
    ser.write_object self, @attributes
  end
end

If you plan on using the serialize method, make sure to pass in the current serializer version, or you could create a message that cannot be deserialized.

Example:

class VariableObject
  def encode_amf ser
      ser.serialize(false)
  end
end

If you wish to send and receive IExternalizable objects, you will need to implement encode_amf, read_external, and write_external. Below is an example of a ResultSet class that extends Array and serializes as an array collection. RocketAMF can automatically serialize arrays as ArrayCollection objects, so this is just an example of how you might implement an object that conforms to IExternalizable.

Example:

class ResultSet < Array
  def encode_amf ser
      # Serialize as an ArrayCollection object
      # It conforms to IExternalizable, does not have any dynamic properties,
      # and has no "sealed" members. See the AMF3 specs for more details about
      # object traits.
      ser.write_object self, nil, {
        :class_name => "flex.messaging.io.ArrayCollection",
        :externalizable => true,
        :dynamic => false,
        :members => []
      }
  end

  # Write self as array to stream
  def write_external ser
    ser.write_array(self)
  end

  # Read array out and replace data with deserialized array.
  def read_external des
    replace(des.read_object)
  end
end

Defined Under Namespace

Modules: Ext, Pure, Types Classes: AMFError, AMFErrorIncomplete, ClassMapping, MappingSet

Constant Summary collapse

CLASS_MAPPER =

todo: use c version

RocketAMF::ClassMapping
Deserializer =

:stopdoc: Import serializer/deserializer

RocketAMF::Pure::Deserializer
Serializer =
RocketAMF::Pure::Serializer
AMF0_NUMBER_MARKER =

AMF0 Type Markers

0x00
AMF0_BOOLEAN_MARKER =

“000”

0x01
AMF0_STRING_MARKER =

“001”

0x02
AMF0_OBJECT_MARKER =

“002”

0x03
AMF0_MOVIE_CLIP_MARKER =

“003”

0x04
AMF0_NULL_MARKER =

“004” # Unused

0x05
AMF0_UNDEFINED_MARKER =

“005”

0x06
AMF0_REFERENCE_MARKER =

“006”

0x07
AMF0_HASH_MARKER =

“a”

0x08
AMF0_OBJECT_END_MARKER =

“b”

0x09
AMF0_STRICT_ARRAY_MARKER =

“t”

0x0A
AMF0_DATE_MARKER =

“n”

0x0B
AMF0_LONG_STRING_MARKER =

“v”

0x0C
AMF0_UNSUPPORTED_MARKER =

“f”

0x0D
AMF0_RECORDSET_MARKER =

“r”

0x0E
AMF0_XML_MARKER =

“016” # Unused

0x0F
AMF0_TYPED_OBJECT_MARKER =

“017”

0x10
AMF0_AMF3_MARKER =

“020”

0x11
AMF3_UNDEFINED_MARKER =

AMF3 Type Markers

0x00
AMF3_NULL_MARKER =

“000”

0x01
AMF3_FALSE_MARKER =

“001”

0x02
AMF3_TRUE_MARKER =

“002”

0x03
AMF3_INTEGER_MARKER =

“003”

0x04
AMF3_DOUBLE_MARKER =

“004”

0x05
AMF3_STRING_MARKER =

“005”

0x06
AMF3_XML_DOC_MARKER =

“006”

0x07
AMF3_DATE_MARKER =

“a”

0x08
AMF3_ARRAY_MARKER =

“b”

0x09
AMF3_OBJECT_MARKER =

“t”

0x0A
AMF3_XML_MARKER =

“n”

0x0B
AMF3_BYTE_ARRAY_MARKER =

“v”

0x0C
AMF3_VECTOR_INT_MARKER =

“f”

0x0D
AMF3_VECTOR_UINT_MARKER =

“r”

0x0E
AMF3_VECTOR_DOUBLE_MARKER =

“016”

0x0F
AMF3_VECTOR_OBJECT_MARKER =

“017”

0x10
AMF3_DICT_MARKER =

“020”

0x11
AMF3_EMPTY_STRING =

Other AMF3 Markers

0x01
AMF3_CLOSE_DYNAMIC_OBJECT =
0x01
AMF3_CLOSE_DYNAMIC_ARRAY =
0x01
MAX_INTEGER =

Other Constants

268_435_455
MIN_INTEGER =
-268_435_456

Class Method Summary collapse

Class Method Details

.deserialize(source) ⇒ Object

Deserialize the AMF string source of the given AMF version into a Ruby data structure and return it. Creates an instance of RocketAMF::Deserializer with a new instance of RocketAMF::CLASS_MAPPER and calls deserialize on it with the given source, returning the result.



146
147
148
149
# File 'lib/rocketamf.rb', line 146

def self.deserialize(source)
  deserializer = RocketAMF::Deserializer.new(RocketAMF::CLASS_MAPPER.new)
  deserializer.deserialize(source)
end

.serialize(obj) ⇒ Object

Serialize the given Ruby data structure obj into an AMF stream using the given AMF version. Creates an instance of RocketAMF::Serializer with a new instance of RocketAMF::CLASS_MAPPER and calls serialize on it with the given object, returning the result.



155
156
157
158
# File 'lib/rocketamf.rb', line 155

def self.serialize(obj)
  serializer = RocketAMF::Serializer.new(RocketAMF::CLASS_MAPPER.new)
  serializer.serialize(obj)
end