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(); }
//----------------------------------------------------------// // CPacket::GetMessages //----------------------------------------------------------// CPacket::Error::Enum CPacket::GetMessages(TMessageList& tempList) { //-- DataBuffer should have exactly m_HeaderV1.m_nMessages inside it. tempList.clear(); u16 nProcessedMessages = 0; CPacketSerializer messageDeserializer(ISerializer::Mode::Deserializing, m_DataBuffer.Buffer(), m_DataBuffer.UsedSize()); CPacketSerializer::Error::Enum eSerError = CPacketSerializer::Error::Ok; while ( (CPacketSerializer::Error::Ok == eSerError) && (messageDeserializer.GetOffset() < messageDeserializer.GetSize()) ) { CMessage::Type nMessageType = CMessage::kTypeUnknown; if (IS_ZERO(messageDeserializer.SerializeU32(nMessageType, 'type'))) { return Error::Serializer; } CMessage* pMessage = CMessageFactory::CreateType(nMessageType); if (IS_NULL_PTR(pMessage)) { return Error::ProtocolMismatch; } pMessage->Serialize(messageDeserializer); eSerError = messageDeserializer.GetError(); if (CPacketSerializer::Error::Ok != eSerError) { return Error::Serializer; } SysSmartPtr<CMessage> pSmart(pMessage); tempList.push_back(pSmart); ++nProcessedMessages; } //-- Sanity tests //-- messageSerializer should have consumed all its bytes. assert(messageDeserializer.GetOffset() == messageDeserializer.GetSize()); if (messageDeserializer.GetOffset() != messageDeserializer.GetSize()) { return Error::SanityFail; } //-- We should have processed exactly the right number of messages. assert(nProcessedMessages == GetMessageCount()); if (nProcessedMessages != GetMessageCount()) { return Error::SanityFail; } return Error::Ok; }
//----------------------------------------------------------// // 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; }