Exemplo n.º 1
0
std::vector<uint8> CPacketUtils::ReadPacket(Framework::CMemStream& stream)
{
	std::vector<uint8> result;

	if(!CPacketUtils::HasPacket(stream))
	{
		assert(0);
		return result;
	}

	stream.Seek(0, Framework::STREAM_SEEK_SET);
	PACKETHEADER header;
	stream.Read(&header, sizeof(PACKETHEADER));

	if(header.packetSize < sizeof(PACKETHEADER))
	{
		//Invalid packet
		CLog::GetInstance().LogError(LOG_NAME, "Packet size in header is invalid.\r\n");
		return result;
	}

	stream.Seek(0, Framework::STREAM_SEEK_SET);
	result.resize(header.packetSize);
	stream.Read(&result[0], header.packetSize);
	stream.Truncate();

	stream.Seek(0, Framework::STREAM_SEEK_END);

	return result;
}
Exemplo n.º 2
0
	bool CPacketUtils::HasPacket(Framework::CMemStream& stream)
	{
		if (stream.GetSize() < sizeof(PACKETHEADER))
		{
			return false;
		}

		stream.Seek(0, Framework::STREAM_SEEK_SET);

		PACKETHEADER header;
		stream.Read(&header, sizeof(PACKETHEADER));
		stream.Seek(0, Framework::STREAM_SEEK_END);

		if (stream.GetSize() < header.packetSize)
		{
			return false;
		}
		return true;
	}