Exemple #1
0
        public bool Serialize <T>(T packet, Stream destination) where T : Packet
        {
            PacketBase packetImpl = packet as PacketBase;

            if (packetImpl == null)
            {
                Log.Warning("Packet is invalid.");
                return(false);
            }

            if (packetImpl.PacketType != PacketType.ClientToServer)
            {
                Log.Warning("Send packet invalid.");
                return(false);
            }

            //memoryStream.SetLength(memoryStream.Capacity);
            //memoryStream.Position = PacketHeaderLength;
            memoryStream.Seek(PacketHeaderLength, SeekOrigin.Begin);
            memoryStream.SetLength(PacketHeaderLength);
            //Serializer.SerializeWithLengthPrefix(memoryStream, packet, PrefixStyle.Fixed32);
            ProtobufHelper.ToStream(packet, memoryStream);

            // 头部消息
            ET_CSPacketHeader packetHeader = ReferencePool.Acquire <ET_CSPacketHeader>();

            packetHeader.Flag         = 0;                                                        //客户端发送的消息,默认flag为0,服务器会解析flag字段值
            packetHeader.PacketLength = (int)memoryStream.Length - ETPackets.ET_PacketSizeLength; // 消息内容长度需要减去头部消息长度,只包含packetSize一个字段
            packetHeader.Id           = (ushort)packet.Id;

            memoryStream.Position = 0;
            this.byteses[0].WriteTo(0, (ushort)packetHeader.PacketLength);
            this.byteses[1][0] = packetHeader.Flag;
            this.byteses[2].WriteTo(0, packetHeader.Id);
            int index = 0;

            foreach (var bytes in this.byteses)
            {
                Array.Copy(bytes, 0, memoryStream.GetBuffer(), index, bytes.Length);
                index += bytes.Length;
            }

            //Serializer.SerializeWithLengthPrefix(memoryStream, packetHeader, PrefixStyle.Fixed32);

            ReferencePool.Release(packetHeader);

            memoryStream.WriteTo(destination);

            long len = destination.Length;
            long pos = destination.Position;

            byte[] temp = (destination as MemoryStream).GetBuffer();
            return(true);
            //}
        }
Exemple #2
0
        public Packet DeserializePacket(IPacketHeader packetHeader, Stream source, out object customErrorData)
        {
            customErrorData = null;
            ET_SCPacketHeader header = packetHeader as ET_SCPacketHeader;

            if (header == null)
            {
                Log.Warning("Packet header is invalid.");
                return(null);
            }

            Packet packet = null;

            if (header.IsValid)
            {
                Type packetType = GetServerToClientPacketType(header.Id);

                //Log.Debug("~~~~~~~~~~~"+packetType.Name);
                if (packetType != null && source is MemoryStream)
                {
                    //packet = (Packet)RuntimeTypeModel.Default.DeserializeWithLengthPrefix(source, ReferencePool.Acquire(packetType), packetType, PrefixStyle.Fixed32, 0);
                    object instance = Activator.CreateInstance(packetType);
                    //packet = (Packet)ProtobufHelper.FromStream(packetType, (MemoryStream)source);
                    packet = (Packet)ProtobufHelper.FromStream(instance, (MemoryStream)source);
                }
                else
                {
                    Log.Warning("Can not deserialize packet for packet id '{0}'.", header.Id.ToString());
                }
            }
            else
            {
                Log.Warning("Packet header is invalid.");
            }
            ReferencePool.Release(header);
            return(packet);
        }