/// <summary>
        /// Deserializes an array of bytes into a Packet of type P
        /// </summary>
        /// <param name="packetData">The array to deserialize to a packet. Must be exact length of bytes.</param>
        /// <returns>A new packet with data extracted from packetDate</returns>
        public IPacketWrapper Deserialize(byte[] packetData) {

            FrostbitePacket wrapper = new FrostbitePacket();

            int header = BitConverter.ToInt32(packetData, 0);
            //this.PacketSize = BitConverter.ToInt32(packet, 4);
            int wordsTotal = BitConverter.ToInt32(packetData, 8);

            wrapper.Packet.Origin = Convert.ToBoolean(header & 0x80000000) == true ? PacketOrigin.Server : PacketOrigin.Client;

            wrapper.Packet.Type = Convert.ToBoolean(header & 0x40000000) == false ? PacketType.Request : PacketType.Response;
            wrapper.Packet.RequestId = header & 0x3fffffff;

            int wordOffset = 0;

            for (UInt32 wordCount = 0; wordCount < wordsTotal; wordCount++) {
                UInt32 wordLength = BitConverter.ToUInt32(packetData, (int)this.PacketHeaderSize + wordOffset);

                wrapper.Packet.Words.Add(Encoding.GetEncoding(1252).GetString(packetData, (int)this.PacketHeaderSize + wordOffset + 4, (int)wordLength));

                wordOffset += Convert.ToInt32(wordLength) + 5; // WordLength + WordSize + NullByte
            }

            wrapper.Packet.Data = packetData;
            wrapper.Packet.Text = String.Join(" ", wrapper.Packet.Words);
            wrapper.Packet.DebugText = String.Join(" ", wrapper.Packet.Words.Select((word, index) => String.Format("[{0}-{1}]", index, word)));

            return wrapper;
        }
Example #2
0
 protected void SendResponse(FrostbitePacket request, params string[] words) {
     this.Send(new FrostbitePacket() {
         Packet = {
             Origin = request.Packet.Origin,
             Type = PacketType.Response,
             RequestId = request.Packet.RequestId,
             Words = new List<String>(words)
         }
     });
 }