/// <summary>
        /// 解包
        /// </summary>
        /// <param name="packet">接受数据包</param>
        /// <param name="next">投递到下一个处理器</param>
        /// <returns>解包的对象</returns>
        public static object Unpacked(ReceivePackage packet, Func <ReceivePackage, object> next)
        {
            if (packet.MsgInfo == null || packet.MsgInfo.MsgType != MsgTypes.Protobuf)
            {
                return(next(packet));
            }

            // 如果这里引发了异常,那么这个协议会被抛弃
            // 所以我们不捕获这里的异常
            var stream = new System.IO.MemoryStream(packet.Body.Array, packet.Body.Offset, packet.Body.Count);

            stream.SetLength(packet.Body.Count);
            return(ProtoBuf.Meta.RuntimeTypeModel.Default.Deserialize(stream, null, packet.MsgInfo.ProtocolType));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 解包
        /// </summary>
        /// <param name="packet">接受数据包</param>
        /// <param name="next">投递到下一个处理器</param>
        /// <returns>解包的对象</returns>
        public static object Unpacked(ReceivePackage packet, Func <ReceivePackage, object> next)
        {
            if (packet.MsgInfo == null || packet.MsgInfo.MsgType != MsgTypes.Binary)
            {
                return(next(packet));
            }

            //ReceiveStream.Reset(packet.Body.Array, packet.Body.Offset, packet.Body.Count);

            var buff = new byte[packet.Body.Count];

            Array.Copy(packet.Body.Array, packet.Body.Offset, buff, 0, packet.Body.Count);

            var msg = MsgHelper.UnpackParam(packet.MsgInfo.ProtocolType, buff); // Generate(packet.MsgInfo) as IMsg;

            //if (msg == null)
            //{
            //    throw new NotSupportedException("Can not generate protocol [" + packet.MsgInfo.Name + "]");
            //}

            //msg.UnMarshal(ReceiveStream);
            return(msg);
        }