348 lines
9.7 KiB
C++
348 lines
9.7 KiB
C++
/**
|
|
* @file include/ActivMiddleware/ActivBase/Posix/Ip/IoVecHelper.h
|
|
*
|
|
* @brief Header file for iovec helper classes.
|
|
*/
|
|
|
|
#if (!defined ACTIV_BASE_POSIX_IP_IO_VEC_HELPER_H)
|
|
#define ACTIV_BASE_POSIX_IP_IO_VEC_HELPER_H
|
|
|
|
#include "ActivMiddleware/ActivBase/External.h"
|
|
|
|
#include "ActivMiddleware/CxxYZ/vector.h"
|
|
|
|
#include <cassert>
|
|
#include <sys/uio.h>
|
|
#include <sys/socket.h>
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
/**
|
|
* @brief iovec helper class.
|
|
*/
|
|
class IoVec : public iovec
|
|
{
|
|
public:
|
|
//! Get the buffer pointer.
|
|
void *GetData();
|
|
|
|
//! Get the buffer pointer.
|
|
const void *GetData() const;
|
|
|
|
//! Get the buffer length.
|
|
size_t GetLength() const;
|
|
|
|
// Don't add member variables or virtuals!
|
|
};
|
|
|
|
ACTIV_BOOST_STATIC_ASSERT(sizeof(IoVec) == sizeof(iovec));
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void *IoVec::GetData()
|
|
{
|
|
return reinterpret_cast<void *>(iov_base);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline const void *IoVec::GetData() const
|
|
{
|
|
return reinterpret_cast<const void *>(iov_base);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline size_t IoVec::GetLength() const
|
|
{
|
|
return static_cast<size_t>(iov_len);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief iovec array helper class.
|
|
*/
|
|
class IoVecHelper
|
|
{
|
|
private:
|
|
typedef CxxYZ::vector<IoVec> IoVecArray;
|
|
|
|
public:
|
|
typedef IoVecArray::iterator iterator; ///< Iterator to buffers.
|
|
typedef IoVecArray::const_iterator const_iterator; ///< Iterator to buffers.
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param initialSize the number of struct iovec to allocate initially.
|
|
*
|
|
* @throw Exception.
|
|
*/
|
|
IoVecHelper(const size_t initialSize = 10);
|
|
|
|
/**
|
|
* @brief Move constructor.
|
|
*
|
|
* @param rhs
|
|
*/
|
|
IoVecHelper(ACTIV_BOOST_RV_REF(IoVecHelper) rhs) :
|
|
m_initialSize(rhs.m_initialSize),
|
|
m_ioVecArray(CxxYZ::move(rhs.m_ioVecArray)),
|
|
m_numberOfBuffersAvailable(rhs.m_numberOfBuffersAvailable),
|
|
m_totalBytesAvailable(rhs.m_totalBytesAvailable),
|
|
m_totalBytesInUse(rhs.m_totalBytesInUse)
|
|
{
|
|
rhs.Reset();
|
|
}
|
|
|
|
//! Destructor.
|
|
~IoVecHelper();
|
|
|
|
/**
|
|
* @brief Move assignment operator.
|
|
*
|
|
* @param rhs
|
|
*
|
|
* @return *this
|
|
*/
|
|
IoVecHelper &operator=(ACTIV_BOOST_RV_REF(IoVecHelper) rhs)
|
|
{
|
|
m_ioVecArray = CxxYZ::move(rhs.m_ioVecArray);
|
|
m_numberOfBuffersAvailable = rhs.m_numberOfBuffersAvailable;
|
|
m_totalBytesAvailable = rhs.m_totalBytesAvailable;
|
|
m_totalBytesInUse = rhs.m_totalBytesInUse;
|
|
|
|
rhs.Reset();
|
|
|
|
return *this;
|
|
}
|
|
|
|
//! Reset the number of buffers in use.
|
|
void Reset();
|
|
|
|
/**
|
|
* @brief Append a new buffer, expand the iovec array if required.
|
|
*
|
|
* @brief pData pointer to the data.
|
|
* @brief dataLength the length of the buffer.
|
|
*
|
|
* @throw Exception if the vector couldn't be resized.
|
|
*/
|
|
void Append(const void * const pData, const size_t dataLength);
|
|
|
|
//! Get a pointer to the first iovec.
|
|
IoVec *GetFirstBuffer();
|
|
|
|
//! Get a const pointer to the first iovec.
|
|
const IoVec *GetFirstBuffer() const;
|
|
|
|
/**
|
|
* @brief Get buffer at an offset.
|
|
*
|
|
* @param offset
|
|
* @param startOffset offset at which buffer \a index begins.
|
|
* @param index index of the returned buffer.
|
|
*
|
|
* @return iovec pointer or 0.
|
|
*/
|
|
IoVec *GetBuffer(const size_t offset, size_t &startOffset, size_t &index);
|
|
|
|
//! Get the number of buffers available (the number of Append()s since the last Reset()).
|
|
size_t GetNumberOfBuffersAvailable() const;
|
|
|
|
//! Get the total number of bytes in the iovec arary.
|
|
size_t GetTotalBytesAvailable() const;
|
|
|
|
//! Get the total number of bytes in use.
|
|
size_t GetTotalBytesInUse() const;
|
|
|
|
//! Set the total number of bytes in use.
|
|
void SetTotalBytesInUse(const size_t totalBytesInUse);
|
|
|
|
//! Get terator to first buffer.
|
|
iterator begin();
|
|
|
|
//! Get iterator to past the last buffer.
|
|
iterator end();
|
|
|
|
//! Get const_iterator to first buffer.
|
|
const_iterator begin() const;
|
|
|
|
//! Get const_iterator to past the last buffer.
|
|
const_iterator end() const;
|
|
|
|
private:
|
|
ACTIV_BOOST_MOVABLE_BUT_NOT_COPYABLE(IoVecHelper)
|
|
|
|
/**
|
|
* @brief Extend the iovec array.
|
|
*
|
|
* @throw Exception if the iovec couldn't be resized.
|
|
*/
|
|
void Extend();
|
|
|
|
const size_t m_initialSize; ///< Initial size of the iovec array.
|
|
IoVecArray m_ioVecArray;
|
|
size_t m_numberOfBuffersAvailable; ///< Number of buffers in m_ioVecArray currently available.
|
|
size_t m_totalBytesAvailable; ///< Total number of bytes in iovec array.
|
|
size_t m_totalBytesInUse; ///< Number of bytes out of m_totalBytes that are in use (e.g. after a receive).
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline IoVecHelper::IoVecHelper(const size_t initialSize) :
|
|
m_initialSize(initialSize),
|
|
m_ioVecArray(initialSize)
|
|
{
|
|
assert(0 != initialSize);
|
|
Reset();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline IoVecHelper::~IoVecHelper()
|
|
{
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void IoVecHelper::Reset()
|
|
{
|
|
m_numberOfBuffersAvailable = 0;
|
|
m_totalBytesAvailable = 0;
|
|
m_totalBytesInUse = 0;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void IoVecHelper::Append(const void * const pData, const size_t dataLength)
|
|
{
|
|
// Do we need to extend the array?
|
|
if (m_numberOfBuffersAvailable == m_ioVecArray.size())
|
|
Extend();
|
|
|
|
m_ioVecArray[m_numberOfBuffersAvailable].iov_base = reinterpret_cast<char *>(const_cast<void *>(pData));
|
|
m_ioVecArray[m_numberOfBuffersAvailable].iov_len = dataLength;
|
|
|
|
m_totalBytesAvailable += dataLength;
|
|
++m_numberOfBuffersAvailable;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline IoVec *IoVecHelper::GetFirstBuffer()
|
|
{
|
|
return &m_ioVecArray[0];
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline const IoVec *IoVecHelper::GetFirstBuffer() const
|
|
{
|
|
return &m_ioVecArray[0];
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline IoVec *IoVecHelper::GetBuffer(const size_t offset, size_t &startOffset, size_t &index)
|
|
{
|
|
size_t currentPosition = 0, nextPosition = 0;
|
|
|
|
for (size_t i = 0; i < m_numberOfBuffersAvailable; ++i)
|
|
{
|
|
nextPosition += m_ioVecArray[i].GetLength();
|
|
|
|
if (offset < nextPosition)
|
|
{
|
|
index = i;
|
|
startOffset = currentPosition;
|
|
|
|
return &m_ioVecArray[i];
|
|
}
|
|
|
|
currentPosition = nextPosition;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline size_t IoVecHelper::GetNumberOfBuffersAvailable() const
|
|
{
|
|
return m_numberOfBuffersAvailable;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline size_t IoVecHelper::GetTotalBytesAvailable() const
|
|
{
|
|
return m_totalBytesAvailable;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline size_t IoVecHelper::GetTotalBytesInUse() const
|
|
{
|
|
return m_totalBytesInUse;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void IoVecHelper::SetTotalBytesInUse(const size_t totalBytesInUse)
|
|
{
|
|
m_totalBytesInUse = totalBytesInUse;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline IoVecHelper::iterator IoVecHelper::begin()
|
|
{
|
|
return m_ioVecArray.begin();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline IoVecHelper::iterator IoVecHelper::end()
|
|
{
|
|
return m_ioVecArray.begin() + m_numberOfBuffersAvailable;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline IoVecHelper::const_iterator IoVecHelper::begin() const
|
|
{
|
|
return m_ioVecArray.begin();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline IoVecHelper::const_iterator IoVecHelper::end() const
|
|
{
|
|
return m_ioVecArray.begin() + m_numberOfBuffersAvailable;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void IoVecHelper::Extend()
|
|
{
|
|
size_t newSize = m_ioVecArray.size() + m_initialSize;
|
|
|
|
// TODO exceptions, catch and rethrow as Exception?
|
|
m_ioVecArray.resize(newSize);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
typedef IoVec IpBuffer;
|
|
typedef IoVecHelper IpBufferHelper; ///< Common name for vectored/scatter-gather i/o buffer helper.
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_BASE_POSIX_IP_IO_VEC_HELPER_H)
|