/// <summary>
        /// 打包
        /// </summary>
        /// <param name="packet">需要发送的数据包</param>
        /// <param name="next">投递到下一个处理器</param>
        /// <returns>打包的数据</returns>
        public static ArraySegment <byte> Packed(SentPackage packet, Func <SentPackage, ArraySegment <byte> > next)
        {
            if (packet.MsgInfo == null || packet.MsgInfo.MsgType != MsgTypes.Protobuf)
            {
                return(next(packet));
            }

            var stream = new System.IO.MemoryStream(packet.SentBuffer.Array, packet.SentBuffer.Offset,
                                                    packet.SentBuffer.Count);

            ProtoBuf.Meta.RuntimeTypeModel.Default.Serialize(stream, packet.Packet);
            return(new ArraySegment <byte>(packet.SentBuffer.Array, packet.SentBuffer.Offset,
                                           (int)stream.Position));
        }
Beispiel #2
0
        /// <summary>
        /// 发送数据流
        /// </summary>
        //[ThreadStatic]
        //private static MMStream sentStream;

        /// <summary>
        /// 接收数据流
        /// </summary>
        //[ThreadStatic]
        //private static MMStream receiveStream;

        /// <summary>
        /// 发送数据流
        /// </summary>
        //private static MMStream SentStream
        //{
        //    get { return sentStream ?? (sentStream = new MMStream()); }
        //}

        /// <summary>
        /// 接收数据流
        /// </summary>
        //private static MMStream ReceiveStream
        //{
        //    get { return receiveStream ?? (receiveStream = new MMStream()); }
        //}

        /// <summary>
        /// 打包
        /// </summary>
        /// <param name="packet">需要发送的数据包</param>
        /// <param name="next">投递到下一个处理器</param>
        /// <returns>打包的数据</returns>
        public static ArraySegment <byte> Packed(SentPackage packet, Func <SentPackage, ArraySegment <byte> > next)
        {
            if (packet.MsgInfo == null || packet.MsgInfo.MsgType != MsgTypes.Binary)
            {
                return(next(packet));
            }

            byte[] buff = null;
            MsgHelper.PackParam(packet.Packet, out buff);

            Array.Copy(buff, 0, packet.SentBuffer.Array, packet.SentBuffer.Offset, buff.Length);
            //SentStream.Reset(packet.SentBuffer.Array, packet.SentBuffer.Offset, packet.SentBuffer.Count);
            //((IMsg)packet.Packet).Marshal(SentStream);
            return(new ArraySegment <byte>(packet.SentBuffer.Array, packet.SentBuffer.Offset, buff.Length));
        }