BonCodeAJP13Namespace.BonCodeAJP13Packet.GetPacket C# (CSharp) Method

GetPacket() public static method

Analyze the provided buffer and returns suitable packet instance
public static GetPacket ( byte Buffer ) : BonCodeAJP13Packet
Buffer byte
return BonCodeAJP13Packet
        public static BonCodeAJP13Packet GetPacket(byte[] Buffer)
        {
            // First of all we need to check that this is a valid packet
            // then we will return the suitable packet instance, otherwise null will be returnd.

            if (Buffer.Length < BonCodeAJP13Consts.MIN_BONCODEAJP13_PACKET_LENGTH)
            {
                return null;
            }
            if (!(Buffer[0] == BonCodeAJP13PacketFormat.BONCODEAJP13_PACKET_START && Buffer[Buffer.Length - 1] == BonCodeAJP13PacketFormat.BONCODEAJP13_PACKET_END))
            {
                return null;
            }

            byte PacketType = Buffer[BonCodeAJP13Packet.PACKET_TYPE_POS];

            if (PacketType == BonCodeAJP13ServerPacketType.SERVER_FORWARD_REQUEST)
            {

                if (Buffer.Length <= BonCodeAJP13Consts.MAX_BONCODEAJP13_PACKET_LENGTH && Buffer.Length >= BonCodeAJP13Consts.MIN_BONCODEAJP13_PACKET_LENGTH)
                            return null; // need to return new forward request new BonCodeAJP13GET(Buffer);
                        else
                            return null;

            }
            else if (PacketType == BonCodeAJP13PacketType.BONCODEAJP13_RESPONSE)
            {

                switch (PacketID)
                {
                    case BonCodeAJP13PacketID.SERVLETRESPONSE_ID:
                        if (Buffer.Length <= BonCodeAJP13ServletResponse.SERVLETRESPONSE_MAX_LENGTH && Buffer.Length >= BonCodeAJP13ServletResponse.SERVLETRESPONSE_MIN_LENGTH)
                            return new BonCodeAJP13ServletResponse(Buffer);
                        else
                            return null;

                }

            }
            else if (PacketType == BonCodeAJP13PacketType.BONCODEAJP13_EVENT)
            {

                switch (PacketID)
                {
                }

            }
            else if (PacketType == BonCodeAJP13PacketType.BONCODEAJP13_TWO_WAY_PACKET)
            {

                switch (PacketID)
                {
                    case BonCodeAJP13PacketID.DATALENGTHINITIALIZER_ID:
                        if (Buffer.Length == BonCodeAJP13DataLengthInitializer.DATALENGTHINITIALIZER_LENGTH)
                            return new BonCodeAJP13DataLengthInitializer(Buffer);
                        else
                            return null;
                }

            }
            else
            {
                return null;
            }

            return null;
        }

Usage Example

        /// <summary>
        /// Analyze the provided buffer and returns a collection of packets represented by the buffer
        /// </summary>
        public static BonCodeAJP13PacketCollection GetPackets(byte[] Buffer)
        {
            BonCodeAJP13PacketCollection AnalyzedResponses = new BonCodeAJP13PacketCollection();

            if (Buffer != null)
            {
                int start = 0;
                for (int i = 0; i < Buffer.Length; i++)
                {
                    if (Buffer[start] == BonCodeAJP13PacketFormat.BONCODEAJP13_PACKET_START)
                    {
                        UInt16 UserDataLength = 0;
                        GetUInt16(Buffer, ref UserDataLength, start + USERDATALENGTH_POS);
                        i = start + UserDataLength + 8;

                        // here we need to truncate the buffer and analyze the packet.
                        if (Buffer[i] == BonCodeAJP13PacketFormat.BONCODEAJP13_PACKET_END)
                        {
                            byte[] NewPacket = new byte[i + 1 - start];
                            Array.Copy(Buffer, start, NewPacket, 0, i + 1 - start);

                            BonCodeAJP13Packet packet = BonCodeAJP13Packet.GetPacket(NewPacket);
                            if (packet != null)
                            {
                                AnalyzedResponses.Add(packet);
                            }
                            else
                            {
                                return(null);
                            }

                            start = i + 1;
                        }
                        else
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        return(null);
                    }
                }
                return(AnalyzedResponses);
            }
            else
            {
                return(null);
            }
        }