AMF学习1数据类型

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位。

15.XML是使用类型标识符0x0F后直接跟LongString类型的字符串表示。

这里大部分代码我都是摘自AMF.net 一个开源的.net AMF序列化和反序列化的库,大家若有兴趣可以到 去下载。另外 这个英文网站也对AMF数据类型作了比较详细的介绍。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“WPF...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这...
Some samples are below for ASP.Net web form controls:(from http://www.visualize.uk.com/resources/asp
问题描述: 对于未定义为 System.String 的列,唯一有效的值是(引发异常)。 For columns not defined as System.String, the only vali
最近用到了CalendarExtender,结果不知道为什么发生了错位,如图在Google和百度上找了很久,中文的文章里面似乎只提到了如何本地化(就是显示中文的月份)以及怎么解决被下拉框挡住的问题,谈
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence changed a lot since 1.1. Here is the order: App
静态声明: &#39; Style=&quot;position: relative&quot; AppendDataBoundItems=&quot;True&quot;&gt; (无 或 空 或
以下内容是从网络上搜集资料,然后整理而来的。不当之处,请不吝指教。(The following were from network, and edited by myself. Thanks in a
Imports System Imports System.Reflection Namespace DotNetNuke &#39;*********************************
Ok so you have all seen them: “8 million tools for web development”, “5 gagillion tools that if you
以下内容来源于: http://blog.csdn.net/cuike519/archive/2005/09/27/490316.aspx 问:为什么Session在有些机器上偶尔会丢失? 答:可能和
以下文章提到可以用“http://localhost/MyWebApp/WebAdmin.axd”管理站点: ---------------------------------------------
Visual Studio 2005 IDE相关的11个提高开发效率的技巧 英文原创来源于: http://www.chinhdo.com/chinh/blog/20070920/top-11-vis
C#日期格式化 from: http://51xingfu.blog.51cto.com/219185/46222 日期转化一 为了达到不同的显示效果有时,我们需要对时间进行转化,默认格式为:2007
from: http://www.nikhilk.net/UpdateControls.aspx Two controls that go along with the UpdatePanel and
Open the report in the Designer. In the ToolBox, select/expand the &quot;Report Items&quot; section.
from: http://drupal.org/node/75844 Do this: find which TinyMCE theme you are using. For the sake of
asp.net中给用户控件添加自定义事件 用户控件中定义好代理和事件: public delegate void ItemSavedDelegate(object sender, EventArgs
在Windows版本的Safari中浏览以下的页面。 http://www.asp.net/AJAX/Control-Toolkit/Live/Calendar/Calendar.aspx Calen
http://aspnet.4guysfromrolla.com/articles/021506-1.aspx By Scott Mitchell Introduction When creating