AMF是Action Message Format协议的简称,AMF协议是Adobe公司自己的协议,主要用于数据交互和远程过程调用,在功能上相当于WebService,但是AMF与WebService中的XML不同的是AMF是二进制数据,而XML是文本数据,AMF的传输效率比XML高。AMF使用HTTP方式传输,目前主要是用于ActionScript中,即实现Flex和Server之间的通信。
我是做.Net开发的,按理说和AMF没有什么关系,主要是最近在耍一款网页游戏:纵横天下,该游戏就是采用Flex做的客户端,为了能够做出该游戏的挂机程序,所以才开始学习研究AMF的。闲话休说,言归正传,下面继续说AMF协议,在例子中我使用的AMF数据就是从纵横天下中抓取的AMF数据。
AMF目前有两种版本,AMF0和AMF3,他们在数据类型的定义上有细微不同。关于AMF的官方文档参见。
Type
Byte code |
Notes |
Number 0×00 Boolean 0×01 String 0×02 Object 0×03 MovieClip 0×04 Not available in Remoting Null 0×05 Undefined 0×06 Reference 0×07 MixedArray 0×08 EndOfObject 0×09 See Object Array 0x0a Date 0x0b LongString 0x0c Unsupported 0x0dRecordset 0x0eRemoting,server-to-client only
0x0fTypedObject (Class instance) 0×10AMF3 data 0×11Sent by Flash player 9+
以上表列出了每种数据类型的表示方法,这样看并不容易理解,下面我就主要讲解一下常用的一些格式:
40 10 00 00 00 00 00 00就表示的是一个double数4.0,在C#中可以使用如下代码读取该数据:
1.Boolean对应的是.net中的bool类型,数据使用1字节表示,和C语言差不多,使用00表示false,使用01表示true。比如十六进制 01就表示true。
2.String相当于.net中的string类型,String所占用的空间有1个类型标识字节和2个表示字符串UTF8长度的字节加上字符串UTF8格式的内容组成。比如十六进制
73 68 61 6E 67 67 75 61表示的就是字符串,该字符串长8字节,字符串内容为73 68 61 6E 67 67 75 61,对应的就是“shanggua”。在C#中要读取字符串则使用:
3.Object在.net中对应的就是Hashtable,内容由UTF8字符串作为Key,其他AMF类型作为Value,该对象由3个字节:00 00 09来表示结束。C#中读取该对象使用如下方法:
<div class="level1">
<div style="border-right: #cccccc 1px solid; padding-right: 5px; padding-left: 4px; font-size: 13px; border-top: #cccccc 1px solid; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; word-break: break-all; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee">
<img align="top" src="/Images/OutliningIndicators/None.gif" alt=""><span style="color: #0000ff">private<span style="color: #000000"> Hashtable ReadUntypedObject()<img id="Codehighlighter1_44_329_Open_Image" onclick="code_collapse_toggle(this);" align="top" src="/Images/OutliningIndicators/ExpandedBlockStart.gif" alt=""><img style="display: none" id="Codehighlighter1_44_329_Closed_Image" onclick="code_collapse_toggle(this);" align="top" src="/Images/OutliningIndicators/ContractedBlock.gif" alt=""> <span style="border-right: #808080 1px solid; border-top: #808080 1px solid; border-left: #808080 1px solid; border-bottom: #808080 1px solid; display: none; background-color: #ffffff" id="Codehighlighter1_44_329_Closed_Text"><img src="https://www.jb51.cc/res/2019/02-14/22/a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif" alt=""><span id="Codehighlighter1_44_329_Open_Text"><span style="color: #000000">{<img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""> Hashtable hash <span style="color: #000000">=<span style="color: #000000"> <span style="color: #0000ff">new<span style="color: #000000"> Hashtable();<img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""> <span style="color: #0000ff">string<span style="color: #000000"> key <span style="color: #000000">=<span style="color: #000000"> ReadShortString();<img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""> <span style="color: #0000ff">for<span style="color: #000000"> (<span style="color: #0000ff">byte<span style="color: #000000"> type <span style="color: #000000">=<span style="color: #000000"> ReadByte(); type <span style="color: #000000">!=<span style="color: #000000"> <span style="color: #800080">9<span style="color: #000000">; type <span style="color: #000000">=<span style="color: #000000"> ReadByte())<img id="Codehighlighter1_207_299_Open_Image" onclick="code_collapse_toggle(this);" align="top" src="/Images/OutliningIndicators/ExpandedSubBlockStart.gif" alt=""><img style="display: none" id="Codehighlighter1_207_299_Closed_Image" onclick="code_collapse_toggle(this);" align="top" src="/Images/OutliningIndicators/ContractedSubBlock.gif" alt=""> <span style="border-right: #808080 1px solid; border-top: #808080 1px solid; border-left: #808080 1px solid; border-bottom: #808080 1px solid; display: none; background-color: #ffffff" id="Codehighlighter1_207_299_Closed_Text"><img src="https://www.jb51.cc/res/2019/02-14/22/a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif" alt=""><span id="Codehighlighter1_207_299_Open_Text"><span style="color: #000000">{<img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""> hash.Add(key, ReadData(type));<img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""> key <span style="color: #000000">=<span style="color: #000000"> ReadShortString();<img align="top" src="/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt=""> }<span style="color: #000000"><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""> <span style="color: #0000ff">return<span style="color: #000000"> hash;<img align="top" src="/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt=""> }<span style="color: #000000"> <img align="top" src="/Images/OutliningIndicators/None.gif" alt=""><img align="top" src="/Images/OutliningIndicators/None.gif" alt="">
5.Null就是空对象,该对象只占用一个字节,那就是Null对象标识0x05。
<div style="border-right: #cccccc 1px solid; padding-right: 5px; padding-left: 4px; font-size: 13px; border-top: #cccccc 1px solid; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; word-break: break-all; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee">
<img align="top" src="/Images/OutliningIndicators/None.gif" alt=""><span style="color: #0000ff">private<span style="color: #000000"> Hashtable ReadDictionary()<img id="Codehighlighter1_41_397_Open_Image" onclick="code_collapse_toggle(this);" align="top" src="/Images/OutliningIndicators/ExpandedBlockStart.gif" alt=""><img style="display: none" id="Codehighlighter1_41_397_Closed_Image" onclick="code_collapse_toggle(this);" align="top" src="/Images/OutliningIndicators/ContractedBlock.gif" alt=""> <span style="border-right: #808080 1px solid; border-top: #808080 1px solid; border-left: #808080 1px solid; border-bottom: #808080 1px solid; display: none; background-color: #ffffff" id="Codehighlighter1_41_397_Closed_Text"><img src="https://www.jb51.cc/res/2019/02-14/22/a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif" alt=""><span id="Codehighlighter1_41_397_Open_Text"><span style="color: #000000">{<img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""> <span style="color: #0000ff">int<span style="color: #000000"> size <span style="color: #000000">=<span style="color: #000000"> ReadInt32();<img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""> Hashtable hash <span style="color: #000000">=<span style="color: #000000"> <span style="color: #0000ff">new<span style="color: #000000"> Hashtable(size);<img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""> <span style="color: #0000ff">string<span style="color: #000000"> key <span style="color: #000000">=<span style="color: #000000"> ReadShortString();<img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""> <span style="color: #0000ff">for<span style="color: #000000"> (<span style="color: #0000ff">byte<span style="color: #000000"> type <span style="color: #000000">=<span style="color: #000000"> ReadByte(); type <span style="color: #000000">!=<span style="color: #000000"> <span style="color: #800080">9<span style="color: #000000">; type <span style="color: #000000">=<span style="color: #000000"> ReadByte())<img id="Codehighlighter1_241_367_Open_Image" onclick="code_collapse_toggle(this);" align="top" src="/Images/OutliningIndicators/ExpandedSubBlockStart.gif" alt=""><img style="display: none" id="Codehighlighter1_241_367_Closed_Image" onclick="code_collapse_toggle(this);" align="top" src="/Images/OutliningIndicators/ContractedSubBlock.gif" alt=""> <span style="border-right: #808080 1px solid; border-top: #808080 1px solid; border-left: #808080 1px solid; border-bottom: #808080 1px solid; display: none; background-color: #ffffff" id="Codehighlighter1_241_367_Closed_Text"><img src="https://www.jb51.cc/res/2019/02-14/22/a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif" alt=""><span id="Codehighlighter1_241_367_Open_Text"><span style="color: #000000">{<img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""> <span style="color: #0000ff">object<span style="color: #000000"> value <span style="color: #000000">=<span style="color: #000000"> ReadData(type);<img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""> hash.Add(key, value);<img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""> key <span style="color: #000000">=<span style="color: #000000"> ReadShortString();<img align="top" src="/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" alt=""> }<span style="color: #000000"><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""> <span style="color: #0000ff">return<span style="color: #000000"> hash;<img align="top" src="/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt=""> }<span style="color: #000000">
10.Array对应的就是.net中的ArrayList对象,该对象首先使用32位整数定义了ArralyList的长度,然后是密集的跟着ArrayList中的对象,读取该对象使用如下函数:
11.Date对应.net中的DateTime数据类型,Date在类型标识符0x0B后使用double来表示从1970/1/1到表示的时间所经过的毫秒数,然后再跟一个ushort的16位无符号整数表示时区。读取Date类型的C#代码为:
<div style="border-right: #cccccc 1px solid; padding-right: 5px; padding-left: 4px; font-size: 13px; border-top: #cccccc 1px solid; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; word-break: break-all; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee">
<img align="top" src="/Images/OutliningIndicators/None.gif" alt=""><span style="color: #0000ff">private<span style="color: #000000"> DateTime ReadDate()<img id="Codehighlighter1_33_265_Open_Image" onclick="code_collapse_toggle(this);" align="top" src="/Images/OutliningIndicators/ExpandedBlockStart.gif" alt=""><img style="display: none" id="Codehighlighter1_33_265_Closed_Image" onclick="code_collapse_toggle(this);" align="top" src="/Images/OutliningIndicators/ContractedBlock.gif" alt=""> <span style="border-right: #808080 1px solid; border-top: #808080 1px solid; border-left: #808080 1px solid; border-bottom: #808080 1px solid; display: none; background-color: #ffffff" id="Codehighlighter1_33_265_Closed_Text"><img src="https://www.jb51.cc/res/2019/02-14/22/a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif" alt=""><span id="Codehighlighter1_33_265_Open_Text"><span style="color: #000000">{<img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""> <span style="color: #0000ff">double<span style="color: #000000"> ms <span style="color: #000000">=<span style="color: #000000"> ReadDouble();<img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""> DateTime BaseDate <span style="color: #000000">=<span style="color: #000000"> <span style="color: #0000ff">new<span style="color: #000000"> DateTime(<span style="color: #800080">1970<span style="color: #000000">, <span style="color: #800080">1<span style="color: #000000">, <span style="color: #800080">1<span style="color: #000000">);<img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""> DateTime date <span style="color: #000000">=<span style="color: #000000"> BaseDate.AddMilliseconds(ms); <img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""> ReadUInt16(); <span style="color: #008000">//<span style="color: #008000">get's the timezone <span style="color: #008000"><img align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""><span style="color: #000000"> <span style="color: #0000ff">return<span style="color: #000000"> date;<img align="top" src="/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt=""> }<span style="color: #000000">
12.LongString对应的也是string类型,不过和2对应的String不同的是这里使用32位整数来表示字符串的UTF8长度,而String使用的是16位。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。