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 | 0x0d | |
| Recordset | 0x0e | Remoting, server-to-client only | 
| XML | 0x0f | |
| TypedObject (Class instance) | 0×10 | |
| AMF3 data | 0×11 | Sent by Flash player 9+ | 
public enum DataType
{
   Number = 0,
   Boolean = 1,
   String = 2,
   UntypedObject = 3,
   MovieClip = 4,
   Null = 5,
   Undefined = 6,
   ReferencedObject = 7,
   MixedArray = 8,
   End = 9,
   Array = 10,//0x0A
   Date = 11,//0x0B
   LongString = 12,//0x0C
   TypeAsObject = 13,//0x0D
   Recordset = 14,//0x0E
   Xml = 15,//0x0F
   TypedObject = 16,//0x10
   AMF3data=17//0x11
}

double num=BitConverter.ToDouble(d,0);
string str=System.Text.Encoding.UTF8.GetString(buffer);
3.Object在.net中对应的就是Hashtable,内容由UTF8字符串作为Key,其他AMF类型作为Value,该对象由3个字节:00 00 09来表示结束。C#中读取该对象使用如下方法:
private Hashtable ReadUntypedObject()
      {
         Hashtable hash = new Hashtable();
         string key = ReadShortString();
         for (byte type = ReadByte(); type != 9; type = ReadByte())
         {
            hash.Add(key, ReadData(type));
            key = ReadShortString();
         }
         return hash;
      } 

private Hashtable ReadDictionary()
      {
         int size = ReadInt32();
         Hashtable hash = new Hashtable(size);
         string key = ReadShortString();
         for (byte type = ReadByte(); type != 9; type = ReadByte())
         {
            object value = ReadData(type);
            hash.Add(key, value);
            key = ReadShortString();
         }
         return hash;
      } 10.Array对应的就是.net中的ArrayList对象,该对象首先使用32位整数定义了ArralyList的长度,然后是密集的跟着ArrayList中的对象,读取该对象使用如下函数:
private ArrayList ReadArray()
      {
         int size = ReadInt32();
         ArrayList arr = new ArrayList(size);
         for (int i = 0; i < size; ++i)
         {
            arr.Add(ReadData(ReadByte()));
         }
         return arr;
      } 11.Date对应.net中的DateTime数据类型,Date在类型标识符0x0B后使用double来表示从1970/1/1到表示的时间所经过的毫秒数,然后再跟一个ushort的16位无符号整数表示时区。读取Date类型的C#代码为:
private DateTime ReadDate()
     {
        double ms = ReadDouble();
        DateTime BaseDate = new DateTime(1970, 1, 1);
        DateTime date = BaseDate.AddMilliseconds(ms);         
        ReadUInt16(); //get's the timezone         
        return date;
     } 12.LongString对应的也是string类型,不过和2对应的String不同的是这里使用32位整数来表示字符串的UTF8长度,而String使用的是16位。
15.XML是使用类型标识符0x0F后直接跟LongString类型的字符串表示。
这里大部分代码我都是摘自AMF.net 一个开源的.net AMF序列化和反序列化的库,大家若有兴趣可以到http://sourceforge.net/project/showfiles.php?group_id=159742 去下载。另外http://osflash.org/documentation/amf/astypes 这个英文网站也对AMF数据类型作了比较详细的介绍。
                    
                


         
                
            
        
浙公网安备 33010602011771号