コード例 #1
0
        // 消息编码
        public override void Encode(System.Object packageObj)
        {
            NetPackage package = (NetPackage)packageObj;

            if (package == null)
            {
                Channel.HandleError(false, $"The package object is invalid : {packageObj.GetType()}");
                return;
            }

            // 检测逻辑是否合法
            if (package.IsHotfixPackage)
            {
                if (package.BodyBytes == null)
                {
                    Channel.HandleError(false, $"The package BodyBytes field is null : {packageObj.GetType()}");
                    return;
                }
            }
            else
            {
                if (package.MsgObj == null)
                {
                    Channel.HandleError(false, $"The package MsgObj field is null : {packageObj.GetType()}");
                    return;
                }
            }

            // 获取包体数据
            byte[] bodyData;
            if (package.IsHotfixPackage)
            {
                bodyData = package.BodyBytes;
            }
            else
            {
                bodyData = EncodeInternal(package.MsgObj);
            }

            // 检测包体长度
            if (bodyData.Length > NetworkDefine.PackageBodyMaxSize)
            {
                Channel.HandleError(false, $"The package {package.MsgID} body size is larger than NetworkDefine.PackageBodyMaxSize");
                return;
            }

            // 写入长度
            int packetSize = (int)MessageIDFieldType + bodyData.Length;

            if (PackageSizeFieldType == EPackageSizeFieldType.UShort)
            {
                // 检测是否越界
                if (packetSize > ushort.MaxValue)
                {
                    Channel.HandleError(true, $"The package {package.MsgID} size is larger than ushort.MaxValue.");
                    return;
                }
                _sendBuffer.WriteUShort((ushort)packetSize);
            }
            else
            {
                _sendBuffer.WriteInt(packetSize);
            }

            // 写入包头
            {
                // 写入消息ID
                if (MessageIDFieldType == EMessageIDFieldType.UShort)
                {
                    // 检测是否越界
                    if (package.MsgID > ushort.MaxValue)
                    {
                        Channel.HandleError(true, $"The package {package.MsgID} ID is larger than ushort.MaxValue");
                        return;
                    }
                    _sendBuffer.WriteUShort((ushort)package.MsgID);
                }
                else
                {
                    _sendBuffer.WriteInt(package.MsgID);
                }
            }

            // 写入包体
            _sendBuffer.WriteBytes(bodyData, 0, bodyData.Length);
        }
コード例 #2
0
        // 消息解码
        public override void Decode(List <System.Object> packageObjList)
        {
            // 循环解包
            while (true)
            {
                // 如果数据不够一个SIZE
                if (_receiveBuffer.ReadableBytes() < (int)PackageSizeFieldType)
                {
                    break;
                }

                _receiveBuffer.MarkReaderIndex();

                // 读取Package长度
                int packageSize;
                if (PackageSizeFieldType == EPackageSizeFieldType.UShort)
                {
                    packageSize = _receiveBuffer.ReadUShort();
                }
                else
                {
                    packageSize = _receiveBuffer.ReadInt();
                }

                // 如果剩余可读数据小于Package长度
                if (_receiveBuffer.ReadableBytes() < packageSize)
                {
                    _receiveBuffer.ResetReaderIndex();
                    break;                     //需要退出读够数据再解包
                }

                NetPackage package = new NetPackage();

                // 读取包头
                {
                    // 读取消息ID
                    if (MessageIDFieldType == EMessageIDFieldType.UShort)
                    {
                        package.MsgID = _receiveBuffer.ReadUShort();
                    }
                    else
                    {
                        package.MsgID = _receiveBuffer.ReadInt();
                    }
                }

                // 检测包体长度
                int bodySize = packageSize - (int)MessageIDFieldType;
                if (bodySize > NetworkDefine.PackageBodyMaxSize)
                {
                    Channel.HandleError(true, $"The package {package.MsgID} size is larger than NetworkDefine.PackageBodyMaxSize");
                    break;
                }

                // 正常解包
                try
                {
                    // 读取包体
                    byte[] bodyData = _receiveBuffer.ReadBytes(bodySize);

                    Type classType = NetMessageHandler.TryHandle(package.MsgID);
                    if (classType != null)
                    {
                        // 非热更协议
                        package.MsgObj = DecodeInternal(classType, bodyData);
                        if (package.MsgObj != null)
                        {
                            packageObjList.Add(package);
                        }
                    }
                    else
                    {
                        // 热更协议
                        package.IsHotfixPackage = true;
                        package.BodyBytes       = bodyData;
                        packageObjList.Add(package);
                    }
                }
                catch (Exception ex)
                {
                    // 解包异常后继续解包
                    Channel.HandleError(false, $"The package {package.MsgID} decode error : {ex.ToString()}");
                }
            }             //while end

            // 注意:将剩余数据移至起始
            _receiveBuffer.DiscardReadBytes();
        }