void cProtocolRecognizer::DataReceived(const char * a_Data, size_t a_Size)
{
	if (m_Protocol == nullptr)
	{
		if (!m_Buffer.Write(a_Data, a_Size))
		{
			m_Client->Kick("Unsupported protocol version");
			return;
		}

		if (!TryRecognizeProtocol())
		{
			return;
		}

		// The protocol has just been recognized, dump the whole m_Buffer contents into it for parsing:
		AString Dump;
		m_Buffer.ResetRead();
		m_Buffer.ReadAll(Dump);
		m_Protocol->DataReceived(Dump.data(), Dump.size());
	}
	else
	{
		m_Protocol->DataReceived(a_Data, a_Size);
	}
}
void cProtocolRecognizer::DataReceived(const char * a_Data, size_t a_Size)
{
	if (m_Protocol == nullptr)
	{
		if (!m_Buffer.Write(a_Data, a_Size))
		{
			m_Client->Kick("Unsupported protocol version");
			return;
		}

		if (m_InPingForUnrecognizedVersion)
		{
			// We already know the verison; handle it here.
			UInt32 PacketLen;
			UInt32 PacketID;
			if (!m_Buffer.ReadVarInt32(PacketLen))
			{
				return;
			}
			if (!m_Buffer.ReadVarInt32(PacketID))
			{
				return;
			}
			ASSERT(PacketID == 0x01);  // Ping packet
			ASSERT(PacketLen == 9);  // Payload of the packet ID and a UInt64

			Int64 Data;
			if (!m_Buffer.ReadBEInt64(Data))
			{
				return;
			}

			cPacketizer Pkt(*this, 0x01);  // Pong packet
			Pkt.WriteBEInt64(Data);
			return;
		}

		if (!TryRecognizeProtocol())
		{
			return;
		}

		// The protocol has just been recognized, dump the whole m_Buffer contents into it for parsing:
		AString Dump;
		m_Buffer.ResetRead();
		m_Buffer.ReadAll(Dump);
		m_Protocol->DataReceived(Dump.data(), Dump.size());
	}
	else
	{
		m_Protocol->DataReceived(a_Data, a_Size);
	}
}
void cProtocolRecognizer::DataReceived(const char * a_Data, size_t a_Size)
{
	if (m_Protocol != nullptr)
	{
		// Protocol was already recognized, send to the handler:
		m_Protocol->DataReceived(a_Data, a_Size);
		return;
	}

	if (!m_Buffer.Write(a_Data, a_Size))
	{
		m_Client->Kick("Unsupported protocol version");
		return;
	}

	if (!m_InPingForUnrecognizedVersion)
	{
		if (TryRecognizeProtocol())
		{
			// The protocol has just been recognized, dump the whole m_Buffer contents into it for parsing:
			AString Dump;
			m_Buffer.ResetRead();
			m_Buffer.ReadAll(Dump);
			m_Protocol->DataReceived(Dump.data(), Dump.size());
			return;
		}
		else
		{
			m_Buffer.ResetRead();
		}
	}

	if (!m_InPingForUnrecognizedVersion)
	{
		return;
	}

	// Handle server list ping packets
	for (;;)
	{
		UInt32 PacketLen;
		UInt32 PacketID;
		if (
			!m_Buffer.ReadVarInt32(PacketLen) ||
			!m_Buffer.CanReadBytes(PacketLen) ||
			!m_Buffer.ReadVarInt32(PacketID)
		)
		{
			// Not enough data
			m_Buffer.ResetRead();
			break;
		}

		if ((PacketID == 0x00) && (PacketLen == 1))  // Request packet
		{
			HandlePacketStatusRequest();
		}
		else if ((PacketID == 0x01) && (PacketLen == 9))  // Ping packet
		{
			HandlePacketStatusPing();
		}
		else
		{
			m_Client->Kick("Server list ping failed, unrecognized packet");
			return;
		}
	}
}