/// <summary>
        /// Serializes a network packet in preparation of sending it across the wire.  When broadcasting packets to multiple recipients,
        /// it can be helpful to manually serialize the packet via this method and then send out the serialized bytes to all targets
        /// as opposed to serializing the data for each transmission.  Note, that if a packet is encrypted this strategy will fail,
        /// as encrypted packets, being encrypted with the target's unique key, can only be deserialized by the target recipient.
        /// When broadcasting packets in this manner, it may also be prudent to replace the Packet.Header.ToEndpointID string with something
        /// like "BROADCAST" to prevent sending sensitive information like the normally embedded account name to people in the broadcast list
        /// </summary>
        /// <param name="msg">the packet to serialize</param>
        /// <returns>the byte array version of the packet</returns>
        public virtual byte[] SerializePacket(Packet msg)
        {
            TimestampOutgoingPacket(msg);
            // assign a new ID
            msg.PacketID = Packet.NextPacketId;

            // Body
            Pointer bodyDataPointer = new Pointer();

            byte[] body = msg.Serialize(bodyDataPointer);

            // Encrypt and compress, if applicable
            body = OnBeforeSendPacket(body, msg.IsEncrypted, msg.IsCompressed, bodyDataPointer);

            // Envelope
            byte[] envelope = new byte[13]; // Flags/byte + MessageLength/Int32 + PacketType/Int32 + PacketSubType/Int32

            // Total message length // Int32
            int msgLen = bodyDataPointer.Position;

            BitConverter.GetBytes(msgLen).CopyTo(envelope, 0);

            // Packet type // Int32
            BitConverter.GetBytes(msg.PacketTypeID).CopyTo(envelope, 4);

            // Packet sub type // byte
            BitConverter.GetBytes(msg.PacketSubTypeID).CopyTo(envelope, 8);// envelope[8] = msg.PacketSubTypeID;

            // Flags // byte
            envelope[12] = (byte)msg.Flags;

            // Combine envelope and body into final data gram
            byte[] dataGram = new byte[bodyDataPointer.Position + envelope.Length];

            Util.Copy(envelope, 0, dataGram, 0, envelope.Length);
            Util.Copy(body, 0, dataGram, envelope.Length, bodyDataPointer.Position);

            //Log.LogMsg("==>Serializing " + msg.GetType().ToString() + " : " + dat.Length.ToString());
            return(dataGram);
        }
Esempio n. 2
0
        public PacketRelay MakeRelayPacket(string targetServer, Guid targetUser, ServerUser from, Packet message)
        {
            PacketRelay relay = new PacketRelay();
            relay.PacketID = (int)ServerPacketType.Relay;
            relay.PacketSubTypeID = message.PacketID;
            relay.Flags = message.Flags;
            relay.From = from.CurrentCharacter.CharacterInfo;
            relay.Message = message.Serialize(new Pointer());
            relay.OriginServer = this.ServerUserID;
            relay.TargetServer = targetServer;
            relay.To = targetUser;

            return relay;
        }
Esempio n. 3
0
        /// <summary>
        /// Serializes a network packet in preparation of sending it across the wire.  When broadcasting packets to multiple recipients,
        /// it can be helpful to manually serialize the packet via this method and then send out the serialized bytes to all targets
        /// as opposed to serializing the data for each transmission.  Note, that if a packet is encrypted this strategy will fail,
        /// as encrypted packets, being encrypted with the target's unique key, can only be deserialized by the target recipient.
        /// When broadcasting packets in this manner, it may also be prudent to replace the Packet.Header.ToEndpointID string with something
        /// like "BROADCAST" to prevent sending sensitive information like the normally embedded account name to people in the broadcast list
        /// </summary>
        /// <param name="msg">the packet to serialize</param>
        /// <returns>the byte array version of the packet</returns>
        public virtual byte[] SerializePacket(Packet msg)
        {
            TimestampOutgoingPacket(msg);
            // assign a new ID
            msg.PacketID = Packet.NextPacketId;

            // Body
            Pointer bodyDataPointer = new Pointer();

            byte[] body = msg.Serialize(bodyDataPointer);

            // Encrypt and compress, if applicable
            body = OnBeforeSendPacket(body, msg.IsEncrypted, msg.IsCompressed, bodyDataPointer);

            // Envelope
            byte[] envelope = new byte[13]; // Flags/byte + MessageLength/Int32 + PacketType/Int32 + PacketSubType/Int32

            // Total message length // Int32
            int msgLen = bodyDataPointer.Position;
            BitConverter.GetBytes(msgLen).CopyTo(envelope, 0);

            // Packet type // Int32
            BitConverter.GetBytes(msg.PacketTypeID).CopyTo(envelope, 4);

            // Packet sub type // byte
            BitConverter.GetBytes(msg.PacketSubTypeID).CopyTo(envelope, 8);// envelope[8] = msg.PacketSubTypeID;

            // Flags // byte
            envelope[12] = (byte)msg.Flags;

            // Combine envelope and body into final data gram
            byte[] dataGram = new byte[bodyDataPointer.Position + envelope.Length];

            Util.Copy(envelope, 0, dataGram, 0, envelope.Length);
            Util.Copy(body, 0, dataGram, envelope.Length, bodyDataPointer.Position);

            //Log.LogMsg("==>Serializing " + msg.GetType().ToString() + " : " + dat.Length.ToString());
            return dataGram;
        }