Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5096502
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类: Python/Ruby

2012-11-15 11:29:25

这个解析类是我现在项目中用到的一个as3erlang sokcet通信的一个类。 解析出来的数据是数组。
本想做成object的, 不过这得服务器那边配合. 服务器这边的工作量就会加大了, 因为每一个值都要加一个元素符这样才能构成object基本的组成元素。

我们的做法是:服务端那边返回的数据,在协作平台上把数据元素列出来, 前端做开发就参考这个数据列表来做开发


BertClass.as
文件定义一些字头类型

  1. package app.model.comsocket.comParse {

  2. /**

  3. * 字节头类型

  4. * @author zl-主程

  5. */

  6. public class BertClass{

  7.      public static const BERT_START:String = String.fromCharCode(131);

  8.      public static const SMALL_ATOM:String = String.fromCharCode(115);

  9.      public static const ATOM:String = String.fromCharCode(100);

  10.      public static const BINARY:String = String.fromCharCode(109);

  11.      public static const SMALL_INTEGER:String = String.fromCharCode(97);

  12.      public static const INTEGER:String = String.fromCharCode(98);

  13.      public static const SMALL_BIG:String = String.fromCharCode(110);

  14.      public static const LARGE_BIG:String = String.fromCharCode(111);

  15.      public static const FLOAT:String = String.fromCharCode(99);

  16.      public static const STRING:String = String.fromCharCode(107);

  17.      public static const LIST:String = String.fromCharCode(108);

  18.      public static const SMALL_TUPLE:String = String.fromCharCode(104);

  19.      public static const LARGE_TUPLE:String = String.fromCharCode(105);

  20.      public static const NIL:String = String.fromCharCode(106);

  21.      public static const ZERO:String = String.fromCharCode(0);

  22. }



  23. }

 

ParseSocketData.as 解析erlang神发送的二进制和发送数据给erlang


 

  1. package app.model.comsocket.comParse {

  2. import app.utility.debug.Debug;

  3. import flash.display.Sprite;

  4. import flash.utils.ByteArray;

  5. /**

  6. * @author silva-主程

  7. */

  8. public class ParseSocketData extends Sprite {

  9.      public static function bytes_to_string(arr:ByteArray):String {

  10.          var str:String = ";";;

  11.          var len:int = arr.length;

  12.        //Debug.log(";len:"; + arr.length);


  13.        for (var i:int = 0; i <; len; i++) {

  14.          //Debug.log(arr[i])


  15.            str += String.fromCharCode(arr[i]);

  16.          }

  17.        return str;

  18.        }

  19.      /**

  20.        *

  21.        * @param str Unicode 字符代码

  22.        * @return

  23.        */

  24.      public static function decode(str:String):Array {

  25.        if (String.fromCharCode(str.charCodeAt(0)) != BertClass.BERT_START) {

  26.            Debug.error(";不是有效的二进制";);

  27.          return [];

  28.          }

  29.          var Obj:Object = decode_inner(str.substring(1));

  30.        try {

  31.          if (Obj.rest !== ";";) {

  32.              Debug.error(";空";);

  33.            }

  34.          }catch (e:Error) {

  35.            Debug.error(";解析出错!!"; + e.toString());

  36.          }

  37.      

  38.        return Obj.value;

  39.        };

  40.      /**

  41.        * 从不同类型中拿数据

  42.        * @param str

  43.        */

  44.      private static function decode_inner(str:String):Object {

  45.          var Type:String = String.fromCharCode(str.charCodeAt(0));

  46.        //Debug.log(";Type:";+str.charCodeAt(0));


  47.        //跳过头


  48.          var str:String = str.substring(1);

  49.        switch (Type) {

  50.          case BertClass.SMALL_INTEGER:

  51.            //97


  52.            return decode_integer(str, 1);

  53.          break;

  54.          case BertClass.INTEGER:

  55.            //98


  56.            return decode_integer(str, 4);

  57.          break;

  58.          case BertClass.SMALL_ATOM:

  59.            //115


  60.            return decode_atom(str, 1);

  61.          break;

  62.          case BertClass.FLOAT:

  63.            //99


  64.            return decode_float(str);

  65.          break;

  66.          case BertClass.ATOM:

  67.            //常量 100


  68.            return decode_atom(str, 2);

  69.          break;

  70.          case BertClass.STRING:

  71.            //字符型 107


  72.            return decode_string(str);

  73.          case BertClass.LIST:

  74.            //LIST 108


  75.            return decode_list(str);

  76.          break;

  77.          case BertClass.SMALL_TUPLE:

  78.            //104 {}


  79.            return decode_tuple(str, 1);

  80.          break;

  81.          case BertClass.NIL:

  82.            //


  83.            return decode_nil(str);

  84.          break;

  85.          case BertClass.SMALL_BIG:

  86.            //110


  87.            return decode_big(str, 1);

  88.          break;

  89.          case BertClass.LARGE_BIG:

  90.            //111


  91.            return decode_big(str, 4);

  92.          break;

  93.          default:

  94.              Debug.error(";找不到字节头类型"; + str.charCodeAt(0));

  95.              Debug.error(";找不到字节头类型"; + Type==BertClass.SMALL_INTEGER);

  96.              Debug.error(";找不到字节头类型"; + Type);

  97.              Debug.error(";找不到字节头类型"; + BertClass.SMALL_INTEGER);

  98.            return {

  99.                value:null

  100.              };

  101.            }

  102.        };

  103.      /**

  104.        * 97 98 获取

  105.        * @param str

  106.        * @param count

  107.        */

  108.      private static function decode_integer(str:String, count:int):Object {

  109.          var Value:int = bytes_to_int(str, count);

  110.          var temStr:String = str.substring(count);

  111.        return {

  112.            value: Value,

  113.            rest: temStr

  114.          };

  115.        };

  116.      /**

  117.        * 99

  118.        * @param str

  119.        * @return

  120.        */

  121.      private static function decode_float(str:String):Object {

  122.          var Size:int = 31;

  123.        return {

  124.            value: parseFloat(str.substring(0, Size)),

  125.            rest: str.substring(Size)

  126.          };

  127.        };

  128.      /**

  129.        * 100 常量字符获取

  130.        * @param str

  131.        * @param count

  132.        * @return

  133.        */

  134.      private static function decode_atom(str:String, count:int):Object {

  135.          var Size:int;

  136.          Size = bytes_to_int(str, count);

  137.          var temStr:String = str.substring(count);

  138.          var value:String;

  139.          var temBy:ByteArray = new ByteArray();

  140.        for (var i:int = 0; i <; Size; i++) {

  141.            temBy.writeByte(temStr.charCodeAt(i));

  142.          }

  143.          temBy.position = 0;

  144.          value = temBy.readUTFBytes(Size);

  145.        return {

  146.            value:value,

  147.            rest:temStr.substring(Size)

  148.          }

  149.        };

  150.    

  151.      private static function decode_nil(str:String):Object {

  152.        // nil is an empty list


  153.        return {

  154.            value: [],

  155.            rest: str

  156.          };

  157.        };

  158.      /**

  159.        * 107

  160.        * 获取字符串

  161.        * @param str

  162.        */

  163.      private static function decode_string(str:String):Object {

  164.          var Size:int = bytes_to_int(str, 2);

  165.          var temStr:String = str.substring(2);

  166.          var temBy:ByteArray = new ByteArray();

  167.          var value:String;

  168.        for (var i:int = 0; i <; Size; i++) {

  169.            temBy.writeByte(temStr.charCodeAt(i));

  170.          }

  171.          temBy.position = 0;

  172.          value = temBy.readUTFBytes(Size);

  173.        return {

  174.            value: value,

  175.            rest: temStr.substring(Size)

  176.          };

  177.        };

  178.      /**

  179.        * 104

  180.        * @param str

  181.        * @param count

  182.        * @return

  183.        */

  184.      private static function decode_tuple(str:String, count:int):Object {

  185.          var Arr:Array = new Array();

  186.          var El:Object;

  187.          var Size:int = bytes_to_int(str, count);

  188.          var temStr:String = str.substring(count);

  189.        for (var i:int = 0; i <; Size; i++) {

  190.            El = decode_inner(temStr);

  191.            Arr.push(El.value);

  192.            temStr = El.rest;

  193.          }

  194.        return {

  195.            value: Arr,

  196.            rest: temStr

  197.          };

  198.        };

  199.      /**

  200.        * 108

  201.        */

  202.      private static function decode_list(str:String):Object {

  203.          var Size:int;

  204.          var El:Object;

  205.          var LastChar:String;

  206.          var Arr:Array = new Array();

  207.          Size = bytes_to_int(str, 4);

  208.          var temStr:String = str.substring(4);

  209.        for (var i:int = 0; i <; Size; i++) {

  210.            El = decode_inner(temStr);

  211.            Arr.push(El.value);

  212.            temStr = El.rest;

  213.          }

  214.          LastChar = String.fromCharCode(temStr.charCodeAt(0));

  215.        if (LastChar != BertClass.NIL) {

  216.            Debug.error(";数组未结束";);

  217.          }

  218.          var temStr2:String = temStr.substring(1);

  219.        return {

  220.            value: Arr,

  221.            rest: temStr2

  222.          };

  223.        };

  224.      /**

  225.        * 110 111

  226.        */

  227.      private static function decode_big(str:String, count:int):Object {

  228.          var Size:int, Value:Number;

  229.          Size = bytes_to_int(str, count);

  230.          var temStr:String = str.substring(count);

  231.          Value = bytes_to_bignum(temStr, Size);

  232.        return {

  233.            value : Value,

  234.            rest: temStr.substring(Size + 1)

  235.          };

  236.        };

  237.      /**

  238.        * 大数字算法

  239.        * @param str

  240.        * @param count

  241.        */

  242.      private static function bytes_to_bignum(str:String, count:int):Number {

  243.          var isNegative:Boolean;

  244.          var n:Number;

  245.          var Num:Number;

  246.          isNegative = (str.charCodeAt(0) === 1);

  247.          str = str.substring(1);

  248.        for (var i:int = count - 1; i >;= 0; i--) {

  249.            n = str.charCodeAt(i);

  250.          if (Num === 0) {

  251.              Num = n;

  252.            }

  253.          else {

  254.              Num = Num * 256 + n;

  255.            }

  256.          }

  257.        if (isNegative) {

  258.          return Num * -1;

  259.          }

  260.        return Num;

  261.        };

  262.      /**

  263.        * 返回获取数据的长度

  264.        * @param str

  265.        * @param Length

  266.        * @return

  267.        */

  268.      private static function bytes_to_int(str:String, Length:int):int {

  269.          var isNegative:Boolean;

  270.          var n:Number ;

  271.          var Num:int;

  272.          isNegative = (str.charCodeAt(0) >; 128);

  273.        for (var i:int = 0; i <; Length; i++) {

  274.            n = str.charCodeAt(i);

  275.          if (isNegative) {

  276.              n = 255 - n;

  277.            }

  278.          if (Num === 0) {

  279.              Num = n;

  280.            }else {

  281.              Num = Num * 256 + n;

  282.            }

  283.          }

  284.        if (isNegative) {

  285.            Num = Num * (0 - 1);

  286.          }

  287.        return Num;

  288.        };

  289.      ///----发送---


  290.      /**

  291.        * 发送数据解析

  292.        * @param _arr:Array 发送数据数组

  293.        * @return 返回ByteArray 对象

  294.        */

  295.      public static function parseSendData(_arr:Array):ByteArray {

  296.          var TemByteArr:ByteArray = new ByteArray();

  297.          var ReturnTemArr:ByteArray = new ByteArray();

  298.          var temBy:Array = new Array();

  299.        //TemByteArr.writeShort(_arr.length);


  300.          TemByteArr.writeByte(131);

  301.          TemByteArr.writeByte(104);

  302.          TemByteArr.writeByte(_arr.length);

  303.          temBy = sendData(_arr);

  304.        for (var i :int = 0; i <; temBy.length; i++) {

  305.          //Debug.log(temBy[i])


  306.            TemByteArr.writeByte(temBy[i]);

  307.          }

  308.          ReturnTemArr.writeUnsignedInt(TemByteArr.length);

  309.        //加密发送


  310.          ReturnTemArr.writeBytes(Encrypt.enArc4(TemByteArr));

  311.        return ReturnTemArr as ByteArray;

  312.        }

  313.      /**

  314.        * 声明 暂时没找到AS3区分字符串与常量的办法 故已特殊字符做了一下处理

  315.        * @param _arr:Array 查询数组

  316.        * @return 二进制数组

  317.        */

  318.      private static function sendData(_arr:Array):Array {

  319.          var len:int = _arr.length;

  320.          var SendByteArray:Array = new Array();

  321.        for (var s:int = 0; s <; len; s++) {

  322.          if (_arr[s] is String) {

  323.            if (_arr[s].substr(0, 6) == ";const@";) {

  324.                SendByteArray=SendByteArray.concat(DataUtil.parseCode(_arr[s].split(/@/)[1], ";const";));

  325.              }else {

  326.                SendByteArray=SendByteArray.concat(DataUtil.parseCode(_arr[s], ";string";));

  327.              }

  328.            }else if (_arr[s] is Number) {

  329.              SendByteArray = SendByteArray.concat(DataUtil.parseCode(_arr[s], ";number";));

  330.            }else if (_arr[s] is Array) {

  331.              SendByteArray = SendByteArray.concat([104,_arr[s].length]);

  332.              sendData(_arr[s]);

  333.            continue;

  334.            }

  335.          }

  336.      

  337.        return SendByteArray as Array;

  338.        }

  339. }

  340. }


 

 


文章来自:

 

阅读(2283) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~