void CWiFiSensor::GetReceivedMessages(TMessageList& t_messages) {

    /*Resizing the vector, I'm sure to have enough space to store all the messages*/
    t_messages.resize(m_tMessages.size());
    /*Delivering the messages to the caller*/
    std::copy(m_tMessages.begin(),m_tMessages.end(),t_messages.begin());

    /*Remove the delivered up messages*/
    m_tMessages.clear();
  }
Exemple #2
0
//----------------------------------------------------------//
// CPacket::AddMessages
//----------------------------------------------------------//
CPacket::Error::Enum CPacket::AddMessages(TMessageList& sendList, TMessageList::iterator& it)
{
	bool bForcedEnd = false;
	bool bPacketIsCompressed = true;								//-- Prefer compressed.
	bool bPacketIsEncrypted = (SysString::INVALID_KEY != m_Key);	//-- Prefer encrypted if valid key is available.

	it = sendList.begin();
	
	if (it != sendList.end())
	{
		SysSmartPtr<CMessage> pFirst = *it;
		
		bPacketIsCompressed = pFirst->CanBeCompressed(bPacketIsCompressed);
		if (IS_TRUE(bPacketIsCompressed))
		{
			SET_FLAG(m_HeaderV1.m_nFlags, Flag::Compressed);
		}

		bPacketIsEncrypted = pFirst->CanBeEncrypted(bPacketIsEncrypted);
		if (IS_TRUE(bPacketIsEncrypted))
		{
			SET_FLAG(m_HeaderV1.m_nFlags, Flag::Encrypted);
		}
	}

	CPacketSerializer::Error::Enum eSerError = CPacketSerializer::Error::Ok;

	while ( IS_FALSE(bForcedEnd)
		&& (CPacketSerializer::Error::Ok == eSerError)
		&& (it != sendList.end()) )
	{
		SysSmartPtr<CMessage> pMessage = *it;

		if (bPacketIsCompressed != pMessage->CanBeCompressed(bPacketIsCompressed))
		{
			//-- Message cannot be included in same packet as previous message because
			//-- compressed flag has changed.
			bForcedEnd = true;
			break;
		}

		if (bPacketIsEncrypted != pMessage->CanBeEncrypted(bPacketIsEncrypted))
		{
			//-- Message cannot be included in same packet as previous message because
			//-- encrypted flag has changed.
			bForcedEnd = true;
			break;
		}

		CPacketSerializer messageSerializer(ISerializer::Mode::Serializing, m_DataBuffer.Buffer() + m_DataBuffer.UsedSize(), m_DataBuffer.UnusedSize());
		pMessage->Serialize(messageSerializer);

		eSerError = messageSerializer.GetError();
		switch (eSerError)
		{
			case CPacketSerializer::Error::Ok:
			{
				//-- Success
				if (IS_PTR(m_DataBuffer.InsTail(NULL, messageSerializer.GetOffset())))
				{
					++it;
					m_HeaderV1.m_nMessages++;

					if (IS_TRUE(pMessage->IsForcedEnd()))
					{
						bForcedEnd = true;	
					}
				}
				else
				{
					//-- Error!
					return Error::BadFail;
				}
			}
			break;
			case CPacketSerializer::Error::EndReached:
			{
				//-- This just means we can break out of the loop.
				return Error::Ok;
			}
			break;
			default:
			{
				//-- Something bad went wrong.
				return Error::Serializer;
			}
			break;
		}
	}

	return Error::Ok;
}