Beispiel #1
0
    /// <summary>
    /// 通用解包方法 需保证bytes数组完整性
    /// </summary>
    public object StartUnPack(ref byte[] bytes, ref ProtocolEventType type)
    {
        if (bytes.Length <= 12)
        {
            return(null);
        }
        int dataCount = ProtocolUtility.UnPackInt(ref bytes);                         //数据长度

        type = (ProtocolEventType)ProtocolUtility.UnPackInt(ref bytes);               //事件类型
        ProtocolNo protoNo        = (ProtocolNo)ProtocolUtility.UnPackInt(ref bytes); //协议号
        object     protocolObject = null;

        protocolObjectDict.TryGetValue(protoNo, out protocolObject);
        if (protocolObject != null)
        {
            List <object> list             = GetProtocolUnPackMemberListByProtocolNo(protoNo); //协议解包类结构如:{int,string,class}
            List <object> unPackObjectList = new List <object>();
            CommonUnPack(ref bytes, list, ref unPackObjectList);                               //开始递归解包
            ((ProtocolData)protocolObject).SetData(unPackObjectList);
            return(((ProtocolData)protocolObject).Clone());
        }
        else
        {
            Debug.LogError("protocolNo" + protoNo + ",not exist protocolObject in protocolObjectDict!");
            return(null);
        }
    }
        /// <summary>
        /// 通用解包方法 需保证bytes数组完整性
        /// </summary>
        public object StartUnPack(ref byte[] bytes, ref ProtocolEventType type)
        {
            if (bytes.Length <= 12)
            {
                return(null);
            }
            int dataCount = ProtocolUtility.UnPackInt(ref bytes);                         //数据长度

            type = (ProtocolEventType)ProtocolUtility.UnPackInt(ref bytes);               //事件类型
            ProtocolNo protoNo        = (ProtocolNo)ProtocolUtility.UnPackInt(ref bytes); //协议号
            object     protocolObject = null;

            protocolObjectDict.TryGetValue(protoNo, out protocolObject);
            if (protocolObject != null)
            {
                List <object> list             = GetProtocolUnPackMemberListByProtocolNo(protoNo);
                List <object> unPackObjectList = new List <object>();
                //递归通用解析(即遇到是类结构的成员会继续解析该类成员),所有的数据会被会解析出来并放入List<object>第三个参数
                CommonUnPack(ref bytes, list, ref unPackObjectList);
                //多线程时要加锁!这个缓存的protocolObject协议对象仅仅是为了拷贝 only for copy!when multiple thread, it must be locked!
                ((ProtocolData)protocolObject).SetData(unPackObjectList);
                //深拷贝方式 deepCopy method(way)
                return(((ProtocolData)protocolObject).Clone());
            }
            else
            {
                throw new Exception("protocolNo" + protoNo + ",not exist protocolObject in protocolObjectDict!");
            }
        }
Beispiel #3
0
 /// <summary>
 /// 通用递归解析方法
 /// </summary>
 /// <param name="bytes">要解析的数据字节数组</param>
 /// <param name="list">要解析的数据结构</param>
 /// <param name="unPackObjectList">解析出数据结构每个成员后保存的列表</param>
 private void CommonUnPack(ref byte[] bytes, List <object> list, ref List <object> unPackObjectList)
 {
     for (int i = 0; i < list.Count; i++)
     {
         if (list[i] is string)
         {
             //解析string是特殊的,需要先获取int(string数据长度)再解析出指定数据长度的string
             int stringLength = ProtocolUtility.UnPackInt(ref bytes);
             unPackObjectList.Add(ProtocolUtility.UnPackString(ref bytes, stringLength));
         }
         else if (list[i] is int)
         {
             unPackObjectList.Add(ProtocolUtility.UnPackInt(ref bytes));
         }
         else if (list[i] is float)
         {
             unPackObjectList.Add(ProtocolUtility.UnPackFloat(ref bytes));
         }
         else if (list[i] is double)
         {
             unPackObjectList.Add(ProtocolUtility.UnPackDouble(ref bytes));
         }
         else if (list[i] is bool)
         {
             unPackObjectList.Add(ProtocolUtility.UnPackBool(ref bytes));
         }
         else if (list[i] is char)
         {
             unPackObjectList.Add(ProtocolUtility.UnPackChar(ref bytes));
         }
         else if (list[i].GetType() == typeof(ClassBase))
         {
             //先解析出它的DataID
             int dataID = ProtocolUtility.UnPackInt(ref bytes);
             //根据dataID获取类结构List<object>根据它,解析该类的内部信息存入unPackObjectList
             CommonUnPack(ref bytes, GetUnPackClassStructByDataID((DataID)dataID), ref unPackObjectList);
         }
         else
         {
             Debug.LogError("接收了一个未经处理的类型!" + list[i].GetType());
             return;
         }
     }
 }