AMF原始数据解析
AMF数据格式,参照https://en.wikipedia.org/wiki/Action_Message_Format
amf-packet-structure
Length | Name | Type | Default |
16 bits | version | uimsbf | 0 or 3 |
16 bits | header-count | uimsbf | 0 |
header-count*56+ bits | header-type-structure | binary | free form |
16 bits | message-count | uimsbf | 1 |
message-count*64+ bits | message-type-structure | binary | free form |
header-type-structure
Length | Name | Type | Default |
16 bits | header-name-length | uimsbf | 0 |
header-name-length*8 bits | header-name-string | UTF-8 | empty |
8 bits | must-understand | uimsbf | 0 |
32 bits | header-length | simsbf | variable |
header-length*8 bits | AMF0 or AMF3 | binary | free form |
message-type-structure
Length | Name | Type | Default |
16 bits | target-uri-length | uimsbf | variable |
target-uri-length*8 bits | target-uri-string | UTF-8 | variable |
16 bits | response-uri-length | uimsbf | 2 |
response-uri-length*8 bits | response-uri-string | UTF-8 | "/1" |
32 bits | message-length | simsbf | variable |
message-length*8 bits | AMF0 or AMF3 | binary | free form |
收到数据后,需要根据amf-packet-structure,对包体进行解析,解析完包头后,知道type为binary的header或message,在使用Amf0Input进行解析,在Amf0Input内部会根据数据类型切换到Amf3Input进行解析。
在解析的过程中,有可能出现header-length 或者message-lenght为-1,0xffffffff,的情况,如果出现了该情况,则不能简单的间输入数据拷贝已知的长度然后,交由AMF进行处理,事实上是直接将DataInputStream传给Amf0Input即可。
在使用的过程中,由于需要让AMF支持允许的类,需要调用
void ClassDeserializationValidator.addAllowClassPattern(String classNamePattern)
进行处理。
AMF可以针对未知的数据类型,产生键值对,只需要指定
SerializationContext context = SerializationContext.getSerializationContext();
context.createASObjectForMissingType = true;
该配置生效后,即可创建第三方的数据类型。
同时,在发送response时,AMF数据,默认将flex.messaging.messages.AcknowledgeMessageExt
当作了别名DSK进行了处理。
参考文档:
https://support.smartbear.com/loadcomplete/docs/general-info/supported/protocols/amf.html#Specifics
Specifics of AMF Support
Flash or Flex objects can implement the flash.utils.IExternalizable interface and fully control the way an object is serialized. LoadComplete supports only some of these objects, namely:
- flex.messaging.io.ArrayCollection
- flex.messaging.io.ObjectProxy
- flex.messaging.io.ArrayList
- DSK (shorthand version of flex.messaging.messages.AcknowledgeMessageExt)
As for other objects implementing the IExternalizable interface, LoadComplete does not parse them and does not support modifying data in them. In the Parameters tab it displays these objects as objects of the [Binary data] type.
所以需要使用flex的别名机制,从而让AMF能处理DSK类
void ClassAliasRegistry.registerAlias(String alias, String className)
ClassAliasRegistry.getRegistry().registerAlias("DSK", "flex.messaging.messages.AcknowledgeMessageExt");