Files

1139 lines
35 KiB
C++

/**
* @file include/ActivMiddleware/ActivFramework/HeapMessage.h
*
* @brief Header file for HeapMessage class.
*/
#if (!defined ACTIV_FRAMEWORK_HEAP_MESSAGE_H)
#define ACTIV_FRAMEWORK_HEAP_MESSAGE_H
#include "ActivMiddleware/ActivFramework/External.h"
#include "ActivMiddleware/ActivBase/Ip/IpAddress.h"
#include "ActivMiddleware/ActivBase/MessageBuilder.h"
#include "ActivMiddleware/ActivBase/ScatterBufferHelper.h"
#include "ActivMiddleware/ActivBase/StatusCodeException.h"
#include "ActivMiddleware/ActivFramework/Address.h"
#include "ActivMiddleware/ActivFramework/IHeap.h"
#include "ActivMiddleware/ActivFramework/MessageImpl.h"
#include "ActivMiddleware/ActivFramework/RequestId.h"
#include "ActivMiddleware/ActivFramework/Subject.h"
#include ACTIV_BOOST_INCLUDE(move/move.hpp)
#include ACTIV_BOOST_INCLUDE(utility/explicit_operator_bool.hpp)
//! Activ namespace.
namespace Activ
{
class Heap;
class HeapPool;
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Heap message class.
*/
class HeapMessage
{
public:
//! Heap message iterator class.
class Iterator
{
public:
//! Default constructor.
Iterator();
/**
* @brief Dereference operator.
*
* @return Reference to the current message.
*/
MessageImpl &operator*();
/**
* @brief Member access operator.
*
* @return Pointer to the current message.
*/
MessageImpl *operator->();
/**
* @brief Prefix increment operator.
*
* @return Reference to the updated object.
*/
Iterator &operator++();
/**
* @brief Postfix increment operator.
*
* @return Copy of the original object.
*/
const Iterator operator++(int);
/**
* @brief Equality operator.
*
* @param rhs the object to test against.
*
* @return whether the two objects are equal.
*/
bool operator==(const Iterator &rhs) const;
/**
* @brief Inequality operator.
*
* @param rhs the object to test against.
*
* @return whether the two objects are not equal.
*/
bool operator!=(const Iterator &rhs) const;
//! Get pointer to the underlying buffer.
const void *GetData() const;
//! Get the size of the underlying buffer. Note, this is not necessarily how much data is populated!
size_t GetSize() const;
private:
/**
* @brief Constructor.
*
* @param heapMessage the underlying heap message object.
* @param pMessage the current message object.
*/
ACTIV_FRAMEWORK_API
Iterator(const HeapMessage &heapMessage, MessageImpl *pMessage);
//! Slow-path of increment operator.
ACTIV_FRAMEWORK_API
void InternalIncrementOperator();
const HeapMessage *m_pHeapMessage; ///< Underlying HeapMessage object.
MessageImpl *m_pMessage; ///< Current message object.
friend class HeapMessage;
};
//! HeapMessage stateful copy helper class.
class StatefulCopyHelper : private Boost::noncopyable
{
public:
//! Default constructor.
StatefulCopyHelper();
/**
* @brief Constructor.
*
* @param heapMessage
*/
StatefulCopyHelper(const HeapMessage &heapMessage);
/**
* @brief Initialize with a new HeapMessage object.
*
* @param heapMessage
*/
void Initialize(const HeapMessage &heapMessage);
/**
* @brief Copy data from the HeapMessage.
*
* @param pData the address to copy to.
* @param dataLength the length to copy.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_INVALID_PARAMETER dataLength + the current offset is greater than the HeapMessage's GetLength().
*/
ACTIV_FRAMEWORK_API
StatusCode operator()(void *pData, const size_t dataLength);
private:
const HeapMessage * m_pHeapMessage; ///< Underlying HeapMessage object.
Iterator m_iterator; ///< Iterator (used for caching current position).
size_t m_heapMessageOffset; ///< Offset within the HeapMessage object.
size_t m_messageImplOffset; ///< Offset within the current iterated to message object.
};
//! Info for a single HeapMessage, ignoring chaining.
class Stats
{
public:
HeapId m_heapId;
MessageId m_messageId;
BucketId m_bucketId;
MessageType m_type;
StatusCode m_statusCode;
Address m_sourceAddress;
Address m_destinationAddress;
Address m_returnAddress;
IpAddress m_ipAddress;
RequestId m_requestId;
Subject m_subject;
size_t m_totalSpace;
size_t m_freeSpace;
String m_hexDump; ///< Hex dump of message body.
};
//! Default Constructor.
HeapMessage();
/**
* @brief Get a message or chain of messages from a heap.
*
* @param heap the heap to allocate from.
* @param size the number of bytes the message needs to hold.
*
* This does not throw on heap allocation failures. Use "operator!" or "operator bool" to check for allocation
* after construction.
*/
HeapMessage(IHeap &heap, const size_t size);
/**
* @brief Get a message or chain of messages from a heap and copy data in.
*
* @param heap the heap to allocate from.
* @param pData pointer to data to copy into the message.
* @param dataLength the number of bytes the messages needs to hold and the number of bytes from @a pData copied in.
*
* This does not throw on heap allocation failures. Use "operator!" or "operator bool" to check for allocation
* after construction.
*/
HeapMessage(IHeap &heap, const void *pData, size_t dataLength);
/**
* @brief Get a message or chain of messages from a heap, copy data in and block if the heap is empty.
*
* @param heap the heap to allocate from.
* @param pData pointer to data to copy into the message.
* @param dataLength the number of bytes the messages needs to hold and the number of bytes from @a pData copied in.
* @param timeoutPeriod maximum time to wait for a message allocation to succeed if the heap is empty.
*
* This does not throw on heap allocation failures. Use "operator!" or "operator bool" to check for allocation
* after construction.
*/
HeapMessage(IHeap &heap, const void *pData, size_t dataLength, const TimeoutPeriod timeoutPeriod);
/**
* @brief Get a message or chain of messages from a heap and copy data in.
*
* @param heap the heap to allocate from.
* @param messageBuilder containing the data to copy in to the message.
*
* This does not throw on heap allocation failures. Use "operator!" or "operator bool" to check for allocation
* after construction.
*/
HeapMessage(IHeap &heap, const MessageBuilder &messageBuilder);
/**
* @brief Move constructor.
*
* @param rhs transfer ownership of the underlying message managed by @a rhs to this. Note any pointers from the Get()
* family of functions on @a rhs should be considered invalid after moving to @a this.
*/
HeapMessage(ACTIV_BOOST_RV_REF(HeapMessage) rhs) :
m_pMessage(rhs.m_pMessage),
m_pHeap(rhs.m_pHeap)
{
// Inline here for G++.
rhs.Release();
}
//! Destructor.
~HeapMessage();
//! Return a message to its heap (if one is attached).
void Return();
/**
* @brief Move assignment operator.
*
* @param rhs transfer ownership of the underlying message managed by @a rhs to this. Note any pointers from the Get()
* family of functions on @a rhs should be considered invalid after moving to @a this.
*
* @return a reference to this object.
*/
HeapMessage &operator=(ACTIV_BOOST_RV_REF(HeapMessage) rhs)
{
// Inline here for G++.
if (ACTIV_LIKELY(this != &rhs))
{
// Assume generally not assigning to a HeapMessage with a MessageImpl already assigned.
if (ACTIV_UNLIKELY(0 != m_pMessage))
InternalReturn();
m_pMessage = rhs.m_pMessage;
m_pHeap = rhs.m_pHeap;
rhs.Release();
}
return *this;
}
//! Is the underlying buffer contiguous (i.e. is this not a chain of messages)?
bool IsContiguous() const;
//! Get the heap id.
HeapId GetHeapId() const;
//! Get the message type.
MessageType GetType() const;
//! Set the message type.
void SetType(const MessageType type);
//! Get the message status code.
StatusCode GetStatusCode() const;
//! Set the message status code.
void SetStatusCode(const StatusCode statusCode);
//! Set the message status code.
void SetStatusCode(const StatusCodeErrc statusCode);
//! Get the source address.
Address &GetSourceAddress() const;
//! Set the source address.
void SetSourceAddress(const Address &sourceAddress);
//! Get the destination address.
Address &GetDestinationAddress() const;
//! Set the destination address.
void SetDestinationAddress(const Address &destinationAddress);
//! Get the return address.
Address &GetReturnAddress() const;
//! Set the return address.
void SetReturnAddress(const Address &returnAddress);
//! Get the ip address.
IpAddress &GetIpAddress() const;
//! Set the ip address.
void SetIpAddress(const IpAddress &ipAddress);
//! Get the request id.
RequestId &GetRequestId() const;
//! Set the request id.
void SetRequestId(const RequestId &requestId);
//! Get a reference to the message subject.
Subject &GetSubject() const;
//! Set the message subject.
void SetSubject(const Subject &subject);
//! Get the current maximum message length (you can always Extend() if it insufficient).
size_t GetCapacity() const;
/**
* @brief Extend the capacity of the message.
*
* @param size the size to extend the message's capacity by.
*
* @return STATUS_CODE_SUCCESS or an error status.
*/
ACTIV_FRAMEWORK_API
StatusCode Extend(const size_t size);
//! Get the message length.
size_t GetLength() const;
//! Get the current available space in the message.
size_t GetAvailableSpace() const;
/**
* @brief Increment the message length.
*
* @param length the amount to increment the message length by.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_INVALID_PARAMETER cannot increment the length by greater than the current free capacity.
* Use Extend() first.
*/
StatusCode IncrementLength(size_t length);
/**
* @brief Set the message length.
*
* @param length the length of the message.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_INVALID_PARAMETER cannot set a length greater than the current Capacity(). Use Extend() first.
*/
StatusCode SetLength(size_t length);
/**
* @brief Append data.
*
* @param pData the address of the data to append.
* @param dataLength the length of the data to append. This message will be Extend()ed as appropriate.
*
* @return STATUS_CODE_SUCCESS or an error status code.
*/
StatusCode Append(const void *pData, const size_t dataLength);
/**
* @brief Append data.
*
* @param messageBuilder the data to append. This message will be Extend()ed as appropriate.
*
* @return STATUS_CODE_SUCCESS or an error status.
*/
StatusCode Append(const MessageBuilder &messageBuilder);
/**
* @brief Append data.
*
* @param scatterBufferHelper the data to append. This message will be Extend()ed as appropriate.
*
* @return STATUS_CODE_SUCCESS or an error status.
*/
ACTIV_FRAMEWORK_API
StatusCode Append(const ScatterBufferHelper &scatterBufferHelper);
/**
* @brief Copy some data from the beginning of the message to a buffer.
*
* @param pData the address to copy data to.
* @param dataLength the length to copy. The buffer pointed to by @a pData must be at least this big.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_INVALID_PARAMETER dataLength is greater than GetLength().
*/
StatusCode Copy(void *pData, const size_t dataLength) const;
/**
* @brief Copy all data from the message to a MessageBuilder.
*
* @param messageBuilder the MessageBuilder to append to.
*
* @return STATUS_CODE_SUCCESS or an error status.
*/
ACTIV_FRAMEWORK_API
StatusCode Copy(MessageBuilder &messageBuilder) const;
/**
* @brief Get a MessageValidator initialized with all data.
*
* If the requested data is in a contiguous block, then no copying is required and the MessageValidator
* will be initialized with the HeapMessage's managed buffer. If not, the requested data will be copied into
* a separate internally managed contiguous buffer and MessageValidator be initialized with that.
*
* @param messageValidator reference to the MessageValidator to initialize with the heap message contents.
* @param messageVersion the message version to initialize the MessageValidator with.
*
* @return STATUS_CODE_SUCCESS or an error status.
*/
StatusCode Get(MessageValidator &messageValidator,
const MessageHandler::MessageVersion messageVersion = MessageHandler::MESSAGE_VERSION_UNDEFINED) const;
/**
* @brief Get a pointer to all data.
*
* If the requested data is in a contiguous block, then no copying is required and pData
* will point to within the HeapMessage's managed buffer. If not, the requested data will be copied into
* a separate internally managed contiguous buffer and pData will point to that.
*
* @param pData reference to the address of the data.
* @param dataLength the length of the message.
*
* @return STATUS_CODE_SUCCESS or an error status.
*/
StatusCode Get(const void *&pData, size_t &dataLength) const;
/**
* @brief Get a pointer to all data.
*
* If the requested data is in a contiguous block, then no copying is required and pData
* will point to within the HeapMessage's managed buffer. If not, the requested data will be copied into
* either pGetBuffer (if it's non null and @a getBufferSize is big enough) or a separate internally managed buffer
* and pData will point to that.
*
* @param pData reference to the address of the data.
* @param dataLength the length of the message.
* @param pGetBuffer buffer to be used if data not contiguous.
* @param getBufferSize the size of the get buffer.
*
* @return STATUS_CODE_SUCCESS or an error status.
*/
StatusCode Get(const void *&pData, size_t &dataLength, void *pGetBuffer, const size_t getBufferSize) const;
/**
* @brief Get pointers to all data.
*
* For each buffer this HeapMessage is managing, @a scatterBufferHelper will have the buffer's pointer and length appended.
* Generally this will be one buffer, as this HeapMessage will be contiguous.
*
* @param scatterBufferHelper the ScatterBufferHelper. It is not reset initially.
*
* @return STATUS_CODE_SUCCESS or an error status.
*/
StatusCode Get(ScatterBufferHelper &scatterBufferHelper) const;
/**
* @brief Attach another message to this object.
*
* @param heapMessage reference to the message to be attached.
*
* @return STATUS_CODE_SUCCESS or an error status.
*/
ACTIV_FRAMEWORK_API
StatusCode Attach(HeapMessage &heapMessage);
/**
* @brief Clone another message into this HeapMessage.
*
* @param sourceHeapMessage reference to the message to be cloned.
* @param pHeap heap to get message copy from. Default is same heap as @a sourceHeapMessage.
*
* @return STATUS_CODE_SUCCESS or an error status.
*/
ACTIV_FRAMEWORK_API
StatusCode Clone(const HeapMessage &sourceHeapMessage, IHeap * const pHeap = 0);
/**
* @brief Clone another message into this HeapMessage.
*
* @param sourceHeapMessage reference to the message to be cloned.
* @param heapPool to get message copy from.
*
* @return STATUS_CODE_SUCCESS or an error status.
*/
ACTIV_FRAMEWORK_API
StatusCode Clone(const HeapMessage &sourceHeapMessage, HeapPool &heapPool);
/**
* @brief Build a hex dump of a message contents.
*
* @param bytesPerLine number of bytes to display per line.
*
* @return Formatted hex dump of the message.
*/
ACTIV_FRAMEWORK_API
String ToHexDumpString(const size_t bytesPerLine = 16) const;
//! Get an iterator to the first buffer in the message chain.
Iterator Begin() const;
//! Get an iterator that points to beyond the last buffer in the message chain.
Iterator End() const;
//! Is a message attached to this object?
ACTIV_BOOST_EXPLICIT_OPERATOR_BOOL()
//! Is a message not attached to this object?
bool operator!() const;
private:
ACTIV_BOOST_MOVABLE_BUT_NOT_COPYABLE(HeapMessage)
// Undefined.
bool operator==(const HeapMessage &rhs) const;
bool operator!=(const HeapMessage &rhs) const;
//! Release the message pointer.
void Release();
//! Return the attached message to its heap.
ACTIV_FRAMEWORK_API
void InternalReturn();
/**
* @brief Append data.
*
* @param pData the address of the data to append.
* @param dataLength the length of the data to append. This message will be Extend()ed as appropriate.
*
* @return STATUS_CODE_SUCCESS or an error status code.
*/
ACTIV_FRAMEWORK_API
StatusCode InternalAppend(const void *pData, const size_t dataLength);
/**
* @brief Copy some data from the beginning of the message to a buffer.
*
* @param pData the address to copy data to.
* @param dataLength the length to copy. The buffer pointed to by @a pData must be at least this big.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_INVALID_PARAMETER dataLength is greater than GetLength().
*/
ACTIV_FRAMEWORK_API
StatusCode InternalCopy(void *pData, const size_t dataLength) const;
/**
* @brief Get pointers to all data.
*
* For each buffer this HeapMessage is managing, @a scatterBufferHelper will have the buffer's pointer and length appended.
* Generally this will be one buffer, as this HeapMessage will be contiguous.
*
* @param scatterBufferHelper the ScatterBufferHelper. It is not reset initially.
*
* @return STATUS_CODE_SUCCESS or an error status.
*/
ACTIV_FRAMEWORK_API
StatusCode InternalGet(ScatterBufferHelper &scatterBufferHelper) const;
/**
* @brief Internal clone. Clone another message into this HeapMessage.
*
* @param sourceHeapMessage reference to the message to be cloned.
* @param clonedHeapMessage intermediate message to use while cloning.
*
* @return STATUS_CODE_SUCCESS or an error status.
*/
ACTIV_FRAMEWORK_API
StatusCode InternalClone(const HeapMessage &sourceHeapMessage, HeapMessage &clonedHeapMessage);
MessageImpl * m_pMessage; ///< Underlying message.
Heap * m_pHeap;
mutable Boost::scoped_ptr<MessageBuilder> m_pReadBuffer; ///< For Get() type calls when HeapMessage is not contiguous.
friend class HeapMessageAccessor;
};
// ---------------------------------------------------------------------------------------------------------------------------------
inline HeapMessage::HeapMessage() :
m_pMessage(0),
m_pHeap(0)
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline HeapMessage::HeapMessage(IHeap &heap, const size_t size) :
m_pMessage(0),
m_pHeap(0)
{
heap.GetMessage(size, *this);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline HeapMessage::HeapMessage(IHeap &heap, const void *pData, size_t dataLength) :
m_pMessage(0),
m_pHeap(0)
{
heap.GetMessage(pData, dataLength, *this);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline HeapMessage::HeapMessage(IHeap &heap, const void *pData, size_t dataLength, const TimeoutPeriod timeoutPeriod) :
m_pMessage(0),
m_pHeap(0)
{
heap.GetMessage(pData, dataLength, timeoutPeriod, *this);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline HeapMessage::HeapMessage(IHeap &heap, const MessageBuilder &messageBuilder) :
m_pMessage(0),
m_pHeap(0)
{
heap.GetMessage(messageBuilder, *this);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline HeapMessage::~HeapMessage()
{
Return();
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void HeapMessage::Return()
{
if (0 != m_pMessage)
InternalReturn();
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool HeapMessage::IsContiguous() const
{
return (MessageKey::INVALID_MESSAGE_ID == m_pMessage->m_continuationMessageId);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline HeapId HeapMessage::GetHeapId() const
{
return m_pMessage->m_thisMessage.GetHeapId();
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline MessageType HeapMessage::GetType() const
{
return m_pMessage->m_type;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void HeapMessage::SetType(const MessageType type)
{
m_pMessage->m_type = type;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode HeapMessage::GetStatusCode() const
{
return m_pMessage->m_statusCode;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void HeapMessage::SetStatusCode(const StatusCode statusCode)
{
m_pMessage->m_statusCode = statusCode;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void HeapMessage::SetStatusCode(const StatusCodeErrc statusCode)
{
m_pMessage->m_statusCode = Boost::underlying_cast<StatusCode>(statusCode);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline Address &HeapMessage::GetSourceAddress() const
{
return m_pMessage->m_sourceAddress;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void HeapMessage::SetSourceAddress(const Address &sourceAddress)
{
m_pMessage->m_sourceAddress = sourceAddress;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline Address &HeapMessage::GetDestinationAddress() const
{
return m_pMessage->m_destinationAddress;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void HeapMessage::SetDestinationAddress(const Address &destinationAddress)
{
m_pMessage->m_destinationAddress = destinationAddress;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline Address &HeapMessage::GetReturnAddress() const
{
return m_pMessage->m_returnAddress;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void HeapMessage::SetReturnAddress(const Address &returnAddress)
{
m_pMessage->m_returnAddress = returnAddress;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline IpAddress &HeapMessage::GetIpAddress() const
{
return m_pMessage->m_ipAddress;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void HeapMessage::SetIpAddress(const IpAddress &ipAddress)
{
m_pMessage->m_ipAddress = ipAddress;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline RequestId &HeapMessage::GetRequestId() const
{
return m_pMessage->m_requestId;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void HeapMessage::SetRequestId(const RequestId &requestId)
{
m_pMessage->m_requestId = requestId;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline Subject &HeapMessage::GetSubject() const
{
return m_pMessage->m_subject;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void HeapMessage::SetSubject(const Subject &subject)
{
m_pMessage->m_subject = subject;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline size_t HeapMessage::GetCapacity() const
{
return m_pMessage->m_totalSpace;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline size_t HeapMessage::GetLength() const
{
return (m_pMessage->m_totalSpace - m_pMessage->m_freeSpace);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline size_t HeapMessage::GetAvailableSpace() const
{
return m_pMessage->m_freeSpace;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode HeapMessage::IncrementLength(size_t length)
{
if (ACTIV_LIKELY(length <= m_pMessage->m_freeSpace))
{
m_pMessage->m_freeSpace -= static_cast<uint32_t>(length);
return STATUS_CODE_SUCCESS;
}
else
{
return STATUS_CODE_INVALID_PARAMETER;
}
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode HeapMessage::SetLength(size_t length)
{
if (ACTIV_LIKELY(length <= m_pMessage->m_totalSpace))
{
m_pMessage->m_freeSpace = m_pMessage->m_totalSpace - static_cast<uint32_t>(length);
return STATUS_CODE_SUCCESS;
}
else
{
return STATUS_CODE_INVALID_PARAMETER;
}
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode HeapMessage::Append(const void *pData, const size_t dataLength)
{
size_t messageImplOffset = GetLength();
const size_t newLength = messageImplOffset + dataLength;
if (ACTIV_LIKELY(IsContiguous()) && ACTIV_LIKELY(newLength <= m_pMessage->m_size))
{
byte_t * const pDestination =
const_cast<byte_t *>(reinterpret_cast<const byte_t *>(m_pMessage->GetDataPointer()) + messageImplOffset);
MemoryCopy(pDestination, pData, dataLength);
return SetLength(newLength);
}
else
{
return InternalAppend(pData, dataLength);
}
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode HeapMessage::Append(const MessageBuilder &messageBuilder)
{
return Append(messageBuilder.GetMessage(), messageBuilder.GetLength());
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode HeapMessage::Copy(void *pData, const size_t dataLength) const
{
const size_t messageLength = GetLength();
if (ACTIV_UNLIKELY(dataLength > messageLength))
return STATUS_CODE_INVALID_PARAMETER;
if (ACTIV_LIKELY(IsContiguous()))
{
MemoryCopy(pData, m_pMessage->GetDataPointer(), GetMinimum<size_t>(dataLength, messageLength));
return STATUS_CODE_SUCCESS;
}
else
{
return InternalCopy(pData, dataLength);
}
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode HeapMessage::Get(MessageValidator &messageValidator, const MessageHandler::MessageVersion messageVersion) const
{
const void *pData;
size_t dataLength;
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, Get(pData, dataLength));
messageValidator.Initialize(pData, dataLength, messageVersion);
return STATUS_CODE_SUCCESS;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode HeapMessage::Get(const void *&pData, size_t &dataLength) const
{
const size_t length = GetLength();
if (ACTIV_LIKELY(IsContiguous()))
{
pData = m_pMessage->GetDataPointer();
}
else
{
if (m_pReadBuffer)
m_pReadBuffer->Reset();
else
m_pReadBuffer.reset(new MessageBuilder(length));
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, Copy(*m_pReadBuffer));
pData = m_pReadBuffer->GetMessage();
}
dataLength = length;
return STATUS_CODE_SUCCESS;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode HeapMessage::Get(const void *&pData, size_t &dataLength, void *pGetBuffer, const size_t getBufferSize) const
{
const size_t length = GetLength();
if (ACTIV_LIKELY(IsContiguous()))
{
pData = m_pMessage->GetDataPointer();
}
else
{
if ((0 != pGetBuffer) && (getBufferSize >= length))
{
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, Copy(pGetBuffer, length));
pData = pGetBuffer;
}
else
{
if (m_pReadBuffer)
m_pReadBuffer->Reset();
else
m_pReadBuffer.reset(new MessageBuilder(length));
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, Copy(*m_pReadBuffer));
pData = m_pReadBuffer->GetMessage();
}
}
dataLength = length;
return STATUS_CODE_SUCCESS;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode HeapMessage::Get(ScatterBufferHelper &scatterBufferHelper) const
{
if (ACTIV_LIKELY(IsContiguous()))
return scatterBufferHelper.Attach(m_pMessage->GetDataPointer(), GetLength());
else
return InternalGet(scatterBufferHelper);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline HeapMessage::Iterator HeapMessage::Begin() const
{
return Iterator(*this, m_pMessage);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline HeapMessage::Iterator HeapMessage::End() const
{
return Iterator(*this, 0);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool HeapMessage::operator!() const
{
return (0 == m_pMessage);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void HeapMessage::Release()
{
m_pMessage = 0;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline HeapMessage::Iterator::Iterator() :
m_pHeapMessage(0),
m_pMessage(0)
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline HeapMessage::Iterator::Iterator(const HeapMessage &heapMessage, MessageImpl *pMessage) :
m_pHeapMessage(&heapMessage),
m_pMessage(pMessage)
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline MessageImpl &HeapMessage::Iterator::operator*()
{
return *m_pMessage;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline MessageImpl *HeapMessage::Iterator::operator->()
{
return m_pMessage;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline HeapMessage::Iterator &HeapMessage::Iterator::operator++()
{
if (ACTIV_LIKELY(MessageKey::INVALID_MESSAGE_ID == m_pMessage->m_continuationMessageId))
m_pMessage = 0;
else
InternalIncrementOperator();
return *this;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline const HeapMessage::Iterator HeapMessage::Iterator::operator++(int)
{
Iterator oldValue = *this;
++(*this);
return oldValue;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool HeapMessage::Iterator::operator==(const HeapMessage::Iterator &rhs) const
{
return ((m_pHeapMessage == rhs.m_pHeapMessage) && (m_pMessage == rhs.m_pMessage));
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool HeapMessage::Iterator::operator!=(const HeapMessage::Iterator &rhs) const
{
return !((*this) == rhs);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline const void *HeapMessage::Iterator::GetData() const
{
return m_pMessage->GetDataPointer();
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline size_t HeapMessage::Iterator::GetSize() const
{
return m_pMessage->m_size;
}
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
inline HeapMessage::StatefulCopyHelper::StatefulCopyHelper() :
m_pHeapMessage(0),
m_heapMessageOffset(0),
m_messageImplOffset(0)
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline HeapMessage::StatefulCopyHelper::StatefulCopyHelper(const HeapMessage &heapMessage) :
m_pHeapMessage(&heapMessage),
m_iterator(m_pHeapMessage->Begin()),
m_heapMessageOffset(0),
m_messageImplOffset(0)
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void HeapMessage::StatefulCopyHelper::Initialize(const HeapMessage &heapMessage)
{
m_pHeapMessage = &heapMessage;
m_iterator = m_pHeapMessage->Begin();
m_heapMessageOffset = 0;
m_messageImplOffset = 0;
}
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
// NB out of place due to IHeap/HeapMessage inter-dependencies.
inline void IHeap::GetMessage(const void *pData, const size_t dataLength, HeapMessage &heapMessage)
{
GetMessage(dataLength, heapMessage);
if (ACTIV_LIKELY(heapMessage) && ACTIV_LIKELY(0 != pData))
{
if (ACTIV_UNLIKELY(STATUS_CODE_SUCCESS != heapMessage.Append(pData, dataLength)))
heapMessage.Return();
}
}
} // namespace Activ
#endif // (!defined ACTIV_FRAMEWORK_HEAP_MESSAGE_H)