Exemple #1
0
        //序列化变化的部分,返回false说明没有任何改变
        public override bool SerializeChange(IoBuffer stream)
        {
            if (!ValueChange)
            {
                return(false);
            }
            if (!OrderChange())
            {
                ClearChange();
                return(false);
            }
            stream.Write(_changes.Count);//变化数量
#if SERIALIZE_DEBUG
            SerializeUtil.BeginParentLog(this, _changes.Count, true);
#endif
            ChangeCxt c;
            for (int i = 0; i < _changes.Count; ++i)
            {
                c = _changes[i];
                if (c.type != enSerializeChangeType.change)
                {
                    Debuger.LogError("逻辑错误,SObject的子节点只能被修改,不能增删。{0}", c.type);
                    continue;
                }

                stream.Write(c.idx);
#if SERIALIZE_DEBUG
                SerializeUtil.BeginChangeLog(i, enSerializeChangeType.change, _fieldsName[c.idx]);
#endif
                if (!c.obj.SerializeChange(stream))
                {
                    Debuger.LogError("逻辑错误,一定会有要序列化的东西");
                }
#if SERIALIZE_DEBUG
                SerializeUtil.EndChangeLog();
#endif
            }

            ClearChange();
#if SERIALIZE_DEBUG
            SerializeUtil.EndParentLog(this);
#endif
            return(true);
        }
Exemple #2
0
        public override bool getValue(IoBuffer ioBuffer, System.Type type, byte flag, out object value)
        {
            value = null;
            if (!type.IsArray)
            {
                Debuger.LogError("不是数组类型");
                return(false);
            }

            int len;

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

            Type  elemType = type.GetElementType();
            Array arr      = Array.CreateInstance(elemType, len);

            BeginParentLog("数组", len);
            bool needNewLine = elemType.IsClass && typeof(string) != elemType.GetElementType();

            for (int i = 0; i < len; i++)
            {
                if (needNewLine)
                {
                    AddLogNewLine("");
                }
                object e;
                if (!ProtocolCoder.instance.Decode(ioBuffer, elemType, out e))
                {
                    return(false);
                }
                arr.SetValue(e, i);
                AddLog(",");//加个分隔符
            }
            value = arr;
            EndParentLog();

            return(true);
        }
Exemple #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)
            {
                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
        }
        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);
        }
Exemple #5
0
        public override bool setValue(IoBuffer ioBuffer, object value)
        {
            byte flag = Types.MAP;

            // #### 0000
            ioBuffer.Write(flag);
            IDictionary d = (IDictionary)value;

            BeginParentLog("Dictionary", d.Count);
            //写入键数
            putVarInt32(ioBuffer, d.Count);
            foreach (DictionaryEntry e in d)
            {
                AddLogNewLine("[key]:"); //加个分隔符
                ProtocolCoder.instance.Encode(ioBuffer, e.Key);
                AddLog(" [value]:");     //加个分隔符
                ProtocolCoder.instance.Encode(ioBuffer, e.Value);
                AddLog(",");             //加个分隔符
            }
            EndParentLog();

            return(true);
        }
Exemple #6
0
        public override bool getValue(IoBuffer ioBuffer, System.Type type, byte flag, out object value)
        {
            value = null;

            int len;

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

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

            string str = ioBuffer.ReadOnlyStr(len);

            value = str;

            AddLog(value);
            return(true);
        }
Exemple #7
0
 public abstract bool getValue(IoBuffer ioBuffer, System.Type type, byte flag, out object value);
Exemple #8
0
        public static void putVarInt64(IoBuffer ioBuffer, long value)
        {
            if (value < 0)
            {
                // 不能 < 0
                Debuger.LogError("不能小于0");
            }

            if (value <= int.MaxValue)
            {
                putVarInt32(ioBuffer, (int)value);
            }
            else if ((value >> 56) > 0)
            {
                byte b = (byte)(FLAG_0X80 | 8);
                ioBuffer.Write(b);
                //
                byte b1 = (byte)(value >> 56 & 0xFF);
                byte b2 = (byte)(value >> 48 & 0xFF);
                byte b3 = (byte)(value >> 40 & 0xFF);
                byte b4 = (byte)(value >> 32 & 0xFF);
                byte b5 = (byte)(value >> 24 & 0xFF);
                byte b6 = (byte)(value >> 16 & 0xFF);
                byte b7 = (byte)(value >> 8 & 0xFF);
                byte b8 = (byte)(value & 0xFF);
                ioBuffer.Write(b1);
                ioBuffer.Write(b2);
                ioBuffer.Write(b3);
                ioBuffer.Write(b4);
                ioBuffer.Write(b5);
                ioBuffer.Write(b6);
                ioBuffer.Write(b7);
                ioBuffer.Write(b8);
            }
            else if ((value >> 48) > 0)
            {
                byte b = (byte)(FLAG_0X80 | 7);
                ioBuffer.Write(b);
                //
                byte b1 = (byte)(value >> 48 & 0xFF);
                byte b2 = (byte)(value >> 40 & 0xFF);
                byte b3 = (byte)(value >> 32 & 0xFF);
                byte b4 = (byte)(value >> 24 & 0xFF);
                byte b5 = (byte)(value >> 16 & 0xFF);
                byte b6 = (byte)(value >> 8 & 0xFF);
                byte b7 = (byte)(value & 0xFF);
                ioBuffer.Write(b1);
                ioBuffer.Write(b2);
                ioBuffer.Write(b3);
                ioBuffer.Write(b4);
                ioBuffer.Write(b5);
                ioBuffer.Write(b6);
                ioBuffer.Write(b7);
            }
            else if ((value >> 40) > 0)
            {
                byte b = (byte)(FLAG_0X80 | 6);
                ioBuffer.Write(b);
                //
                byte b2 = (byte)(value >> 40 & 0xFF);
                byte b3 = (byte)(value >> 32 & 0xFF);
                byte b4 = (byte)(value >> 24 & 0xFF);
                byte b5 = (byte)(value >> 16 & 0xFF);
                byte b6 = (byte)(value >> 8 & 0xFF);
                byte b7 = (byte)(value & 0xFF);
                ioBuffer.Write(b2);
                ioBuffer.Write(b3);
                ioBuffer.Write(b4);
                ioBuffer.Write(b5);
                ioBuffer.Write(b6);
                ioBuffer.Write(b7);
            }
            else if ((value >> 32) > 0)
            {
                byte b = (byte)(FLAG_0X80 | 5);
                ioBuffer.Write(b);
                //
                byte b3 = (byte)(value >> 32 & 0xFF);
                byte b4 = (byte)(value >> 24 & 0xFF);
                byte b5 = (byte)(value >> 16 & 0xFF);
                byte b6 = (byte)(value >> 8 & 0xFF);
                byte b7 = (byte)(value & 0xFF);
                ioBuffer.Write(b3);
                ioBuffer.Write(b4);
                ioBuffer.Write(b5);
                ioBuffer.Write(b6);
                ioBuffer.Write(b7);
            }
            else
            {
                byte b = (byte)(FLAG_0X80 | 4);
                ioBuffer.Write(b);
                //
                byte b4 = (byte)(value >> 24 & 0xFF);
                byte b5 = (byte)(value >> 16 & 0xFF);
                byte b6 = (byte)(value >> 8 & 0xFF);
                byte b7 = (byte)(value & 0xFF);
                ioBuffer.Write(b4);
                ioBuffer.Write(b5);
                ioBuffer.Write(b6);
                ioBuffer.Write(b7);
            }
        }
Exemple #9
0
 /**
  * 将当前对象转换为通信表示格式
  * @return
  */
 public void  write(IoBuffer stream)
 {
     stream.Write(command);
     stream.Write(module);
 }
Exemple #10
0
        public bool Encode(IoBuffer buffer, object obj)
        {
            //类型
            byte type;

            //System.Type t = obj != null ? obj.GetType():null;
            if (obj == null)
            {
                type = Types.NULL;
            }
            else if (obj is byte || obj is short || obj is int || obj is long || obj is float || obj is double)
            {
                type = Types.NUMBER;
            }
            else if (obj is string)
            {
                type = Types.STRING;
            }
            else if (obj is bool)
            {
                type = Types.BOOLEAN;
            }
            else if (obj is System.Enum)//t.IsEnum
            {
                type = Types.ENUM;
            }
            else if (obj is System.DateTime)
            {
                type = Types.DATE_TIME;
            }
            else if (obj is IDictionary)
            {
                type = Types.MAP;
            }
            else if (obj is System.Array)//t.IsArray
            {
                type = Types.ARRAY;
            }
            else if (obj is IList)
            {
                type = Types.COLLECTION;
            }
            else
            {
                System.Type t = obj.GetType();
                if (IsObjectType(t))//这里不能序列化模板类型、c#原生类型、继承自其他类的类型,不然可能会序列化一些不想要的东西
                {
                    type = Types.OBJECT;
                }
                else
                {
                    Debuger.LogError("不能序列化的类型,不能序列化模板类型、c#原生类型、继承自其他类的类型:" + obj.GetType().Name);
                    return(false);
                }
            }


            Proxy proxy = m_proxys.Get(type);

            if (proxy == null)
            {
                Debuger.LogError("找不到序列化类。{0}", obj.GetType().ToString());
                return(false);
            }

            if (!proxy.setValue(buffer, obj))
            {
                return(false);
            }
            return(true);
        }
Exemple #11
0
        void SendThread()
        {
            var tempBuffer = new IoBuffer(ONCE_SIZE);

            while (m_isIOThreadActive)
            {
                if (m_socket == null || !m_socket.Connected)
                {
                    Thread.Sleep(20);
                    continue;
                }

                if (m_sendMessageList.Count == 0)
                {
                    Thread.Sleep(20);
                    continue;
                }

                lock (m_sendMessageList)
                {
                    m_sendTempMsgList.AddRange(m_sendMessageList);
                    m_sendMessageList.Clear();
                }

                while (m_sendTempMsgList.Count > 0)
                {
                    try
                    {
                        tempBuffer.Reset();
                        encode(m_sendTempMsgList[0], tempBuffer);
                    }
                    catch (System.Exception e)
                    {
                        Util.SafeLogError(e.Message + "\n" + e.StackTrace);
                        continue;
                    }

                    try
                    {
                        while (tempBuffer.ReadSize > 0)
                        {
                            int sendSize = Math.Min(tempBuffer.ReadSize, ONCE_SIZE);

                            SocketError error = SocketError.Success;
                            sendSize = m_socket.Send(tempBuffer.Buffer, tempBuffer.ReadPos, sendSize, SocketFlags.None, out error);
                            if (error != SocketError.Success)
                            {
                                AddError(error, "m_socket.Send error");
                                return;
                            }

                            tempBuffer.ReadPos += sendSize;
                        }
                        Message.put(m_sendTempMsgList[0]);
                        m_sendTempMsgList.RemoveAt(0);
                    }
                    catch (System.Net.Sockets.SocketException e)
                    {
                        AddError(e.SocketErrorCode, e.Message + "\n" + e.StackTrace);
                        return;
                    }
                    catch (Exception e)
                    {
                        AddError(SocketError.SocketError, e.Message + "\n" + e.StackTrace);
                        return;
                    }
                }

                //发送速度不要太快
                Thread.Sleep(5);
            }
        }
Exemple #12
0
 //反序列化,从流初始化或者修改自己
 public abstract void Deserialize(IoBuffer stream);
Exemple #13
0
 public void Read(IoBuffer buffer, int len)
 {
     ReadBytes(buffer.Buffer, buffer.WritePos, len);
     buffer.WritePos += len;
 }
Exemple #14
0
        public bool SetRequestBodyObject(object obj)
        {
            try
            {
                //如果是null,那就直接body为null
                if (obj == null)
                {
                    m_body = null;
                    m_msg  = null;
                    m_code = 0;
                    m_flag = 0;
                    return(true);
                }

                if (USE_JSON_FORMAT)
                {
                    var jsonStr   = LitJson.JsonMapper.ToJson(obj);
                    var jsonBytes = System.Text.Encoding.UTF8.GetBytes(jsonStr);
                    //如果要压缩,且达到要压缩的字符数,那就压缩,compress可以直接得到Buffer,再直接转为IOBuffer
                    if (jsonStr.Length > JSON_COMPRESS_LEN && ENABLE_COMPRESS)
                    {
                        //压缩并直接转为IOBuffer
                        m_body = new IoBuffer(Snappy.Sharp.Snappy.Compress(jsonBytes));
                        //错误消息为空
                        m_msg = null;
                        //错误码为空
                        m_code = 0;
                        //加上标记
                        m_flag = MSG_FLAG_JSON | MSG_FLAG_COMPRESS;
                    }
                    //否则那就把字符串转为IOBuffer吧
                    else
                    {
                        //把字符串转成IOBuffer
                        m_body = new IoBuffer(jsonBytes);
                        //错误消息为空
                        m_msg = null;
                        //错误码为空
                        m_code = 0;
                        //加上标记
                        m_flag = MSG_FLAG_JSON;
                    }
                }
                else
                {
                    if (m_body == null)
                    {
                        m_body = new IoBuffer(256);
                    }
                    else
                    {
                        m_body.Reset();
                    }

                    //错误消息为空
                    m_msg = null;
                    //错误码为空
                    m_code = 0;
                    //设置标记
                    m_flag = 0;
                    ProtocolCoder.instance.Encode(m_body, obj);
                }

                return(true);
            }
            catch (Exception err)
            {
                Util.SafeLogError("Message~setRequestBodyObject发生错误{0}", err.StackTrace);
                m_body = null;
                m_msg  = null;
                m_code = 0;
                m_flag = 0;
                return(false);
            }
        }
Exemple #15
0
        public bool SetResponseWithMsg(int code, string errMsg, object obj)
        {
            try
            {
                var errMsgFlag = errMsg == null ? 0 : MSG_FLAG_ERR_MSG;
                //如果是null,那就直接body为null
                if (obj == null)
                {
                    m_body = null;
                    m_msg  = errMsg;
                    m_code = code;
                    m_flag = MSG_FLAG_RESPONSE | errMsgFlag;
                    return(true);
                }

                if (USE_JSON_FORMAT)
                {
                    var jsonStr   = LitJson.JsonMapper.ToJson(obj);
                    var jsonBytes = System.Text.Encoding.UTF8.GetBytes(jsonStr);
                    //如果要压缩,且达到要压缩的字符数,那就压缩,compress可以直接得到Buffer,再直接转为IOBuffer
                    if (jsonStr.Length > JSON_COMPRESS_LEN && ENABLE_COMPRESS)
                    {
                        //压缩并直接转为IOBuffer
                        m_body = new IoBuffer(Snappy.Sharp.Snappy.Compress(jsonBytes));
                        //添加错误消息
                        m_msg = errMsg;
                        //添加错误码
                        m_code = code;
                        //加上标记
                        m_flag = MSG_FLAG_RESPONSE | MSG_FLAG_JSON | MSG_FLAG_COMPRESS | errMsgFlag;
                    }
                    //否则那就把字符串转为IOBuffer吧
                    else
                    {
                        //把字符串转成IOBuffer
                        m_body = new IoBuffer(jsonBytes);
                        //添加错误消息
                        m_msg = errMsg;
                        //添加错误码
                        m_code = code;
                        //加上标记
                        m_flag = MSG_FLAG_RESPONSE | MSG_FLAG_JSON | errMsgFlag;
                    }
                }
                else
                {
                    if (m_body == null)
                    {
                        m_body = new IoBuffer(256);
                    }
                    else
                    {
                        m_body.Reset();
                    }

                    //添加错误消息
                    m_msg = errMsg;
                    //添加错误码
                    m_code = code;
                    //设置标记
                    m_flag = MSG_FLAG_RESPONSE | errMsgFlag;
                    ProtocolCoder.instance.Encode(m_body, obj);
                }

                return(true);
            }
            catch (Exception err)
            {
                Util.SafeLogError("Message~setResponseWithMsg发生错误:{0}", err.StackTrace);
                m_body = null;
                m_msg  = null;
                m_code = 0;
                m_flag = 0;
                return(false);
            }
        }
Exemple #16
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);
        }
Exemple #17
0
 public void Write(IoBuffer buffer, int len)
 {
     Write(buffer.Buffer, buffer.ReadPos, len);
     //多数情况下,没必要修改源Buffer的ReadPos
     //buffer.ReadPos += len;
 }
        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);
                        }
                    }
                }
            }
        }
Exemple #19
0
 public abstract bool setValue(IoBuffer ioBuffer, object value);
Exemple #20
0
 public override bool getValue(IoBuffer ioBuffer, System.Type type, byte flag, out object value)
 {
     value = null;
     AddLog(null);
     return(true);
 }
Exemple #21
0
 //序列化自己或者子节点变化的部分,如果没有变化,返回false
 public abstract bool SerializeChange(IoBuffer stream);
Exemple #22
0
        public override bool getValue(IoBuffer ioBuffer, System.Type type, byte flag, out object value)
        {
            value = null;

            byte flagType = getFlagTypes(flag);

            if (flagType != Types.NUMBER)
            {
                Debuger.LogError("Types.NUMBER 类型解析出错 {0}", flag);

                return(false);
            }

            // 0000 #000
            bool nevigate = ((flag & FLAG_0X08) != 0);

            // 0000 0###
            byte signal = getFlagSignal(flag);

            if (signal == INT32)
            {
                int i;
                if (!readVarInt32(ioBuffer, out i))
                {
                    return(false);
                }

                if (type == typeof(short))
                {
                    value = (short)(nevigate ? -i : i);
                }
                else if (type == typeof(byte))
                {
                    value = (byte)(nevigate ? -i : i);
                }
                else
                {
                    value = nevigate ? -i : i;
                }
            }
            else if (signal == INT64)
            {
                long l;
                if (!readVarInt64(ioBuffer, out l))
                {
                    return(false);
                }
                value = nevigate ? -l : l;
            }
            else if (signal == FLOAT)
            {
                if (ioBuffer.ReadSize < 4)
                {
                    Debuger.LogError("可读字节长度不足" + 4);
                    return(false);
                }
                value = ioBuffer.ReadFloat();
            }
            else if (signal == DOUBLE)
            {
                if (ioBuffer.ReadSize < 8)
                {
                    Debuger.LogError("可读字节长度不足" + 8);
                    return(false);
                }
                value = ioBuffer.ReadDouble();
            }
            AddLog(value);
            return(true);
        }
        //序列化,返回false说明没有任何改变
        public override bool SerializeChange(IoBuffer stream)
        {
            if (!ValueChange)
            {
                return(false);
            }

            if (!OrderChange())
            {
                ClearChange();
                return(false);
            }


            stream.Write(_changes.Count);//变化数量
#if SERIALIZE_DEBUG
            SerializeUtil.BeginParentLog(this, _changes.Count, true);
#endif
            ChangeCxt c;
            for (int i = 0; i < _changes.Count; ++i)
            {
                c = _changes[i];
                stream.Write((byte)c.type);
                if (c.type != enSerializeChangeType.clear)
                {
                    stream.Write(c.idx);
                }
#if SERIALIZE_DEBUG
                SerializeUtil.BeginChangeLog(i, c.type, c.idx.ToString());
#endif
                if (c.type == enSerializeChangeType.add)
                {
                    if (!c.obj.SerializeChange(stream))
                    {
                        Debuger.LogError("逻辑错误,一定会有要序列化的东西");
                    }
                }
                else if (c.type == enSerializeChangeType.remove)
                {
                }
                else if (c.type == enSerializeChangeType.change)
                {
                    if (!c.obj.SerializeChange(stream))
                    {
                        Debuger.LogError("逻辑错误,一定会有要序列化的东西");
                    }
                }
                else if (c.type == enSerializeChangeType.clear)
                {
                }

#if SERIALIZE_DEBUG
                SerializeUtil.EndChangeLog();
#endif
            }
            ClearChange();
#if SERIALIZE_DEBUG
            SerializeUtil.EndParentLog(this);
#endif
            return(true);
        }
Exemple #24
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);
        }