Пример #1
0
        // Processes our packet reading buffer to deserialize it into a usable packet
        internal void ProcessBuffer(byte[] Data)
        {
            // Add this data to our working byte buffer
            Buffer = Buffer.AppendBytes(Data);

            // Header deserialization
            if (ReadState == PacketReadState.HEADER)
            {
                // Check if we have enough data for a packet header
                if (Buffer.Length < 33)
                {
                    return;
                }

                // Attempt to deserialize our packet header
                try
                {
                    // Store header data and set our read state to body
                    Header    = new PacketHeader(Buffer);
                    ReadState = PacketReadState.BODY;
                }
                catch { }

                // Remove the bytes we read from our buffer
                Buffer = Buffer.SubBytes(33, Buffer.Length - 33);
            }

            // Body deserialization
            if (ReadState == PacketReadState.BODY)
            {
                // Check whether our buffer is the correct size
                if ((ulong)Buffer.LongLength < Header.PayloadSize)
                {
                    return;
                }

                // Attempt to deserialize our packet body
                PortableStorage Body = null;
                try
                {
                    // Get body bytes
                    var BodyBytes = Buffer.SubBytes(0, (int)Header.PayloadSize);

                    // Store deserialized body object
                    Body = new PortableStorage(BodyBytes, out _);
                }
                catch { }

                // Invoke our packet received handler
                if (Body != null)
                {
                    Node.OnPacketReceived(this, new Packet(Header, Body));
                }

                // Remove the bytes we read from our buffer
                Buffer = Buffer.SubBytes((int)Header.PayloadSize, Buffer.Length - (int)Header.PayloadSize);

                // Reset our read state back to header
                ReadState = PacketReadState.HEADER;
            }
        }