Пример #1
0
        //反序列化
        public override void Deserialize(IoBuffer stream)
        {
            int changeCount = stream.ReadInt32();

#if SERIALIZE_DEBUG
            SerializeUtil.BeginParentLog(this, changeCount, false);
#endif
            for (int i = 0; i < changeCount; ++i)
            {
                enSerializeChangeType type = (enSerializeChangeType)stream.ReadByte();
                int idx = -1;
                if (type != enSerializeChangeType.clear)
                {
                    idx = stream.ReadInt32();
                }
#if SERIALIZE_DEBUG
                SerializeUtil.BeginChangeLog(i, type, idx.ToString());
#endif
                if (type == enSerializeChangeType.add)
                {
                    T item = (T)Activator.CreateInstance(typeof(T));
                    item.Parent = this;
                    item.Idx    = idx;
                    item.Deserialize(stream);
                    _dict[item.Idx] = item;
                }
                else if (type == enSerializeChangeType.remove)
                {
                    if (!_dict.Remove(idx))
                    {
                        Debuger.LogError("同步失败");
                    }
                }
                else if (type == enSerializeChangeType.change)
                {
                    T item = _dict.Get(idx);
                    if (item == null)
                    {
                        Debuger.LogError("同步失败2");
                    }
                    item.Deserialize(stream);
                }
                else if (type == enSerializeChangeType.clear)
                {
                    _dict.Clear();
                }
#if SERIALIZE_DEBUG
                SerializeUtil.EndChangeLog();
#endif
            }
#if SERIALIZE_DEBUG
            SerializeUtil.EndParentLog(this);
#endif
        }
Пример #2
0
        //反序列化
        public override void Deserialize(IoBuffer stream)
        {
            _value = stream.ReadInt32();
#if SERIALIZE_DEBUG
            SerializeUtil.AddLog(this);
#endif
        }
Пример #3
0
        //反序列化
        public override void Deserialize(IoBuffer stream)
        {
            int changeCount = stream.ReadInt32();

#if SERIALIZE_DEBUG
            SerializeUtil.BeginParentLog(this, changeCount, false);
#endif
            for (int i = 0; i < changeCount; ++i)
            {
                enSerializeChangeType type = (enSerializeChangeType)stream.ReadByte();
                int idx = -1;
                if (type != enSerializeChangeType.clear)
                {
                    idx = stream.ReadInt32();
                }
#if SERIALIZE_DEBUG
                SerializeUtil.BeginChangeLog(i, type, idx.ToString());
#endif
                if (type == enSerializeChangeType.add)
                {
                    T item = (T)Activator.CreateInstance(typeof(T));
                    item.Parent = this;
                    item.Idx    = idx;
                    item.Deserialize(stream);
                    _list.Insert(item.Idx, item);
                }
                else if (type == enSerializeChangeType.remove)
                {
                    _list.RemoveAt(idx);
                }
                else if (type == enSerializeChangeType.change)
                {
                    _list[idx].Deserialize(stream);
                }
                else if (type == enSerializeChangeType.clear)
                {
                    _list.Clear();
                }
#if SERIALIZE_DEBUG
                SerializeUtil.EndChangeLog();
#endif
            }
#if SERIALIZE_DEBUG
            SerializeUtil.EndParentLog(this);
#endif
        }
Пример #4
0
        public static Command valueOf(IoBuffer stream)
        {
            Command result = new Command();

            result.command = stream.ReadInt32();
            result.module  = stream.ReadByte();



            return(result);
        }
Пример #5
0
        //反序列化
        public override void Deserialize(IoBuffer stream)
        {
            int changeCount = stream.ReadInt32();

#if SERIALIZE_DEBUG
            SerializeUtil.BeginParentLog(this, changeCount, false);
#endif
            for (int i = 0; i < changeCount; ++i)
            {
                int idx = stream.ReadInt32();
#if SERIALIZE_DEBUG
                SerializeUtil.BeginChangeLog(i, enSerializeChangeType.change, _fieldsName[idx]);
#endif
                _fields[idx].Deserialize(stream);
#if SERIALIZE_DEBUG
                SerializeUtil.EndChangeLog();
#endif
            }
#if SERIALIZE_DEBUG
            SerializeUtil.EndParentLog(this);
#endif
        }
Пример #6
0
        //反序列化
        public override void Deserialize(IoBuffer stream)
        {
            enType t = (enType)stream.ReadByte();

            SetType(t);
            if (_type == enType.Bool)
            {
                _value = stream.ReadBool();
            }
            else if (_type == enType.Byte)
            {
                _value = stream.ReadByte();
            }
            else if (_type == enType.Short)
            {
                _value = stream.ReadInt16();
            }
            else if (_type == enType.Int)
            {
                _value = stream.ReadInt32();
            }
            else if (_type == enType.Long)
            {
                _value = stream.ReadInt64();
            }
            else if (_type == enType.Float)
            {
                _value = stream.ReadFloat();
            }
            else if (_type == enType.Double)
            {
                _value = stream.ReadDouble();
            }
            else if (_type == enType.String)
            {
                _value = stream.ReadStr();
            }
            else
            {
                Debuger.LogError("未知的类型");
            }
#if SERIALIZE_DEBUG
            SerializeUtil.AddLog(this);
#endif
        }
Пример #7
0
        public override bool getValue(IoBuffer ioBuffer, System.Type type, byte flag, out object value)
        {
            value = null;

            if (ioBuffer.ReadSize < 4)
            {
                Debuger.LogError("可读字节长度不足" + 4);
                return(false);
            }

            int code = ioBuffer.ReadInt32();

            //类型的获取
            EnumDef def = ProtocolCoder.instance.getEnumDef(code);

            if (def == null)
            {
                Debuger.LogError("枚举定义{0}不存在", type.Name);
                return(false);
            }

            if (def.type != type)
            {
                Debuger.LogError("枚举类型不匹配。EnumDef:{0} Type:", def.type.Name, type.Name);
                return(false);
            }

            // 枚举值
            int valNum;

            if (!readVarInt32(ioBuffer, out valNum))
            {
                return(false);
            }

            value = System.Enum.ToObject(type, valNum);
            AddLog(value);
            return(true);
        }
Пример #8
0
        public override bool getValue(IoBuffer ioBuffer, System.Type type, byte flag, out object value)
        {
            value = null;

            if (ioBuffer.ReadSize < 4)
            {
                Debuger.LogError("可读字节长度不足" + 4);
                return(false);
            }

            int code = ioBuffer.ReadInt32();

            //取键数
            int fieldCount;

            if (!readVarInt32(ioBuffer, out fieldCount))
            {
                return(false);
            }

            if (ioBuffer.ReadSize < 4)
            {
                Debuger.LogError("可读字节长度不足" + 4);
                return(false);
            }

            var objLen = ioBuffer.ReadInt32();

            //减去objLen自己占用4字节
            objLen -= 4;

            if (ioBuffer.ReadSize < objLen)
            {
                Debuger.LogError("可读字节长度不足" + objLen);
                return(false);
            }

            // 类型的获取
            TypeDef def = ProtocolCoder.instance.getTypeDef(code);

            if (def == null)
            {
                Debuger.LogError("类型定义{0}不存在", type.Name);
                return(false);
            }
            if (def.type != type)
            {
                Debuger.LogError("类型不匹配。TypeDef:{0} Type:{1}", def.type.Name, type.Name);
                return(false);
            }

            value = System.Activator.CreateInstance(type);

            //先保存读取位置,如果后面有多余的字段,就跳过去
            var readPos1 = ioBuffer.ReadPos;

            BeginParentLog(type.Name, fieldCount);
            var fieldCountInCfg = def.fields.Length;

            for (int i = 0; i < fieldCount; i++)
            {
                if (i < fieldCountInCfg)
                {
                    FieldInfo fieldInfo = def.fields[i];

                    AddLogNewLine(fieldInfo.Name + ": ");//加个分隔符
                    object obj;
                    if (!ProtocolCoder.instance.Decode(ioBuffer, fieldInfo.FieldType, out obj))
                    {
                        return(false);
                    }
                    fieldInfo.SetValue(value, obj);
                    AddLog(",");//加个分隔符
                }
                else
                {
                    Debuger.Log("实际数据成员数比配置的多,主类型:{0}", type.Name);

                    //居然实现字段比描述的字段还多,看来对方加字段了,这里跳过这个对象剩余的数据吧
                    var readPos2 = ioBuffer.ReadPos;
                    //对象总长度减去已读的长度
                    ioBuffer.Skip(objLen - (readPos2 - readPos1));
                    break;
                }
            }
            EndParentLog();

            return(true);
        }
Пример #9
0
        public static Message FromIOBuffer(IoBuffer inBuf, int dataLen)
        {
            var msg = get();

            do
            {
                if (dataLen < 12)
                {
                    break;
                }
                else
                {
                    dataLen -= 12;
                }
                msg.m_module  = inBuf.ReadInt32();
                msg.m_command = inBuf.ReadInt32();
                msg.m_flag    = inBuf.ReadInt32();
                //如果是回应消息,就要提取错误码
                if ((msg.m_flag & MSG_FLAG_RESPONSE) != 0)
                {
                    if (dataLen < 4)
                    {
                        break;
                    }
                    else
                    {
                        dataLen -= 4;
                    }
                    msg.m_code = inBuf.ReadInt32();
                }
                else
                {
                    msg.m_code = 0;
                }
                //如果有错误消息,就要提取错误消息
                if ((msg.m_flag & MSG_FLAG_ERR_MSG) != 0)
                {
                    if (dataLen < 4)
                    {
                        break;
                    }
                    else
                    {
                        dataLen -= 4;
                    }
                    var len = inBuf.ReadInt32();
                    if (len < 0 || dataLen < len)
                    {
                        break;
                    }
                    else
                    {
                        dataLen -= len;
                    }
                    msg.m_msg = inBuf.ReadOnlyStr(len);
                }
                else
                {
                    msg.m_msg = null;
                }
                //有消息体?
                if (dataLen > 0)
                {
                    var bodyBuf = new IoBuffer(dataLen);
                    inBuf.Read(bodyBuf, dataLen);
                    msg.m_body = bodyBuf;
                }
                else
                {
                    msg.m_body = null;
                }
                return(msg);
            }while (false);

            //对象放回池里
            put(msg);
            //能到这里来?那说明前面遇到break,说明数据不够长
            Util.SafeLogError("Message~fromIOBuffer数据不够长");
            //跳过还未读的
            inBuf.Skip(dataLen);
            //返回null
            return(null);
        }
Пример #10
0
        void decode(IoBuffer inBuf, List <Message> outObjList)
        {
            while (inBuf.ReadSize > 0)
            {
                //包头
                if (!m_inReceiving)
                {
                    // 检查包头数据是否足够,这里应该一种防止包出错的机制,如果上个包出错,这里可以找到当前包的包头
                    while (true)
                    {
                        if (inBuf.ReadSize < Message.PACKAGE_PRE_LENGTH)
                        {
                            return;
                        }
                        if (inBuf.PeekInt32() == Message.PACKAGE_INDETIFIER)
                        {
                            // 已检测到数据头
                            inBuf.Skip(4);
                            break;
                        }
                        else
                        {
                            // 跳过这个字节
                            inBuf.Skip(1);
                        }
                    }

                    //找到前导字节了,取本包长度
                    var packLen = inBuf.ReadInt32();
                    //如果没有数据,那至少应该8字节,数据长度字段4字节 + 检验和4字节
                    if (packLen < 8)
                    {
                        Util.SafeLogError("收到的数据包总长小于8, 包总长:{0}", packLen);
                        continue;
                    }
                    m_waitingLength = packLen - 4;   //减去数据长度字段4字节
                    m_inReceiving   = true;
                }

                //包体
                if (m_inReceiving)
                {
                    if (inBuf.ReadSize < m_waitingLength)
                    {
                        return;
                    }

                    var msgLen = m_waitingLength - 4;  //减去检验和4字节
                    m_inReceiving   = false;
                    m_waitingLength = 0;

                    //获得检验和
                    var checkSum1 = inBuf.ReadInt32();
                    //计算检验和
                    var checkSum2 = Message.BPHash(inBuf.Buffer, inBuf.ReadPos, msgLen);
                    //检验和不对?
                    if (checkSum1 != checkSum2)
                    {
                        //跳过这个包
                        inBuf.Skip(msgLen);
                        Util.SafeLogError("数据包检验和不正确, checkSum1:{0},checkSum2:{0}", checkSum1, checkSum2);
                        continue;
                    }

                    //解密数据
                    Encrypt.Tea16.DecryptInplace(inBuf.Buffer, inBuf.ReadPos, msgLen);
                    var msgObj = Message.FromIOBuffer(inBuf, msgLen);
                    //如果解析正确,加入消息列表
                    if (msgObj != null)
                    {
                        lock (outObjList)
                        {
                            outObjList.Add(msgObj);
                        }
                    }
                }
            }
        }