Files

229 lines
6.3 KiB
C++

/**
* @file include/ActivOneApi/Messages/StreamMessage.h
*
* @brief Header for the StreamMessage class.
*/
#if (!defined ACTIV_ONE_API_MESSAGES_STREAM_MESSAGE_H)
#define ACTIV_ONE_API_MESSAGES_STREAM_MESSAGE_H
#include "ActivOneApi/External.h"
#include "ActivOneApi/TypeDefinitions.h"
//! Activ namespace.
namespace Activ
{
class IpAddress;
//! OneApi namespace.
namespace OneApi
{
class StreamMessagePrivate;
//! The StreamMessage class is used to receive messages in the IStreamHandler class callbacks.
class StreamMessage
{
public:
//! Default constructor.
ACTIV_ONE_API StreamMessage();
/**
* @brief Move constructor.
*
* Messages can be stored for later processing by moving them.
*
* Moving a message received inside an IStreamHandler callback will cause the message data to be copied. Pointers returned by
* GetData() before the move will be valid for the duration of the callback.
*
* @param rhs StreamMessage object which will be moved into this object.
*/
StreamMessage(ACTIV_BOOST_RV_REF(StreamMessage) rhs) :
m_pPrivate(ACTIV_NULLPTR)
{
// Inline here for G++.
InternalMove(rhs);
}
//! Destructor.
ACTIV_ONE_API ~StreamMessage();
/**
* @brief Move assignment operator.
*
* Messages can be stored for later processing by moving them.
*
* Moving a message received inside an IStreamHandler callback will cause the message data to be copied. Pointers returned by
* GetData() before the move will be valid for the duration of the callback.
*
* @param rhs a StreamMessage object which will be moved into this object.
*
* @return a reference to this object.
*/
StreamMessage &operator=(ACTIV_BOOST_RV_REF(StreamMessage) rhs)
{
// Inline here for G++.
InternalMove(rhs);
return *this;
}
/**
* @brief Is the message initialized?
*
* @retval true if this object is initialized.
*/
bool IsInitialized() const;
/**
* @brief Reset the message.
*
* Any resources used by the message will be returned.
*/
void Reset();
/**
* @brief Is this message a valid request?
*
* This will always be true inside the IStreamHandler::OnStreamRequest() callback.
*
* If a request message is moved and responded to after the OnStreamRequest() callback then the application should check that
* the request is still valid before posting a response.
*
* @return Whether this object is a valid request message.
*/
ACTIV_ONE_API bool IsValidRequest() const;
/**
* @brief Get the status code of a response message.
*
* For response messages this will indicate whether the request was successfully processed.
*
* For any other type of message this will always be STATUS_CODE_SUCCESS.
*
* @retval STATUS_CODE_SUCCESS the request was successfully delivered, and responded to, or this is not a response message.
* @retval STATUS_CODE_CONNECTION_REFUSED the connection was refused by the service.
* @retval STATUS_CODE_IGNORED the request was successfully delivered but was ignored by the remote application.
* @retval STATUS_CODE_INTERRUPTED the connection was lost while waiting for the response.
* @retval STATUS_CODE_HOST_UNREACHABLE the service host is unreachable.
* @retval STATUS_CODE_NOT_CONNECTED the session is not connected to the platform.
* @retval STATUS_CODE_NOT_FOUND the service could not be found.
* @retval STATUS_CODE_REJECTED the request was successfully delivered, but the remote application failed to process the
* request.
* @retval STATUS_CODE_TIMEOUT the request timed out.
*/
StatusCode GetStatusCode() const;
/**
* @brief Get the message type.
*
* @return The type of this message.
*/
MessageType GetType() const;
/**
* @brief Get pointer to the message data.
*
* Messages received inside an IStreamHandler callback will be valid for the duration of the callback. See the move
* constructor for notes about moving messages.
*
* @return The pointer to the data stored in this message.
*/
const void *GetData() const;
/**
* @brief Get length of data.
*
* @return The length of the data return by GetData().
*/
size_t GetLength() const;
/**
* @brief Get the ip address that this message originated from.
*
* @return The ip address that this message originated from.
*
* @throw Exception
*/
ACTIV_ONE_API IpAddress GetIpAddress() const;
private:
ACTIV_BOOST_MOVABLE_BUT_NOT_COPYABLE(StreamMessage)
/**
* @brief Internal move function.
*
* @param rhs
*/
ACTIV_ONE_API void InternalMove(StreamMessage &rhs);
friend class StreamMessagePrivate;
StreamMessagePrivate * m_pPrivate;
StatusCode m_statusCode;
MessageType m_type;
const void * m_pData;
size_t m_length;
};
// ---------------------------------------------------------------------------------------------------------------------------------
inline StreamMessage::StreamMessage() :
m_pPrivate(ACTIV_NULLPTR),
m_statusCode(STATUS_CODE_SUCCESS),
m_type(MESSAGE_TYPE_UNDEFINED),
m_pData(ACTIV_NULLPTR),
m_length(0)
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool StreamMessage::IsInitialized() const
{
return ACTIV_NULLPTR != m_pPrivate;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void StreamMessage::Reset()
{
StreamMessage emptyMessage;
InternalMove(emptyMessage);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode StreamMessage::GetStatusCode() const
{
return m_statusCode;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline MessageType StreamMessage::GetType() const
{
return m_type;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline const void *StreamMessage::GetData() const
{
return m_pData;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline size_t StreamMessage::GetLength() const
{
return m_length;
}
} // namespace OneApi
} // namespace Activ
#endif // (!defined ACTIV_ONE_API_MESSAGES_STREAM_MESSAGE_H)