{% for xml in dataFiles %}
namespace {{ xml.attrib['Name'] }}
{
    template <typename HANDLER>
    inline AzNetworking::PacketDispatchResult DispatchPacket(AzNetworking::IConnection* connection, const AzNetworking::IPacketHeader& packetHeader, AzNetworking::ISerializer& serializer, HANDLER& handler)
    {
        switch (aznumeric_cast<int32_t>(packetHeader.GetPacketType()))
        {
{% set packet_ns = namespace(handshake=false) %}
{%      for Packet in xml.iter('Packet') %}
{%          if ('HandshakePacket' in Packet.attrib) and (Packet.attrib['HandshakePacket']|booleanTrue == true) %}
{% set packet_ns.handshake = True %}
{%          endif %}
{%      endfor %}

{%      for Packet in xml.iter('Packet') %}
            case aznumeric_cast<int32_t>({{ Packet.attrib['Name'] }}::Type):
            {
                AZLOG(Debug_DispatchPackets, "Received packet %s", "{{ Packet.attrib['Name'] }}");
{%      if packet_ns.handshake %}
{%          if ('HandshakePacket' not in Packet.attrib) or (Packet.attrib['HandshakePacket'] == 'false') %}
                if (!handler.IsHandshakeComplete(connection))
                {
                    return AzNetworking::PacketDispatchResult::Skipped;
                }
{%          endif %}
{%      endif %}

                {{ Packet.attrib['Name'] }} packet;
                if (!serializer.Serialize(packet, "Packet"))
                {
                    return AzNetworking::PacketDispatchResult::Failure;
                }
                if(handler.HandleRequest(connection, packetHeader, packet))
                {
                    return AzNetworking::PacketDispatchResult::Success;
                }
                break;
            }
{%      endfor %}
        }
        return AzNetworking::PacketDispatchResult::Failure;
    }
}
{% endfor %}
