307 lines
8.9 KiB
C++
307 lines
8.9 KiB
C++
/**
|
|
* @file include/ActivMiddleware/ActivBase/Windows/Ip/WsaBufHelper.h
|
|
*
|
|
* @brief Header file for WSABUF helper classes.
|
|
*/
|
|
|
|
#if (!defined ACTIV_BASE_WINDOWS_IP_WSA_BUF_HELPER_H)
|
|
#define ACTIV_BASE_WINDOWS_IP_WSA_BUF_HELPER_H
|
|
|
|
#include "ActivMiddleware/ActivBase/External.h"
|
|
#include "ActivMiddleware/ActivBase/CrtException.h"
|
|
|
|
#include "ActivMiddleware/CxxYZ/vector.h"
|
|
|
|
#include <cassert>
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
/**
|
|
* @brief WSABUF helper class.
|
|
*/
|
|
class WsaBuf : public WSABUF
|
|
{
|
|
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(WsaBuf) == sizeof(WSABUF));
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void *WsaBuf::GetData()
|
|
{
|
|
return reinterpret_cast<void *>(buf);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline const void *WsaBuf::GetData() const
|
|
{
|
|
return reinterpret_cast<const void *>(buf);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline size_t WsaBuf::GetLength() const
|
|
{
|
|
return static_cast<size_t>(len);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief WSABUF array helper class.
|
|
*/
|
|
class WsaBufHelper
|
|
{
|
|
private:
|
|
typedef CxxYZ::vector<WsaBuf> WsaBufArray;
|
|
|
|
public:
|
|
typedef WsaBufArray::iterator iterator; ///< Iterator to buffers.
|
|
typedef WsaBufArray::const_iterator const_iterator; ///< Iterator to buffers.
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param initialSize the number of WSABUFs to allocate initially.
|
|
*
|
|
* @throw Exception.
|
|
*/
|
|
WsaBufHelper(const size_t initialSize = 10);
|
|
|
|
/**
|
|
* @brief Move constructor.
|
|
*
|
|
* @param rhs
|
|
*/
|
|
WsaBufHelper(ACTIV_BOOST_RV_REF(WsaBufHelper) rhs) :
|
|
m_initialSize(rhs.m_initialSize),
|
|
m_wsaBufArray(CxxYZ::move(rhs.m_wsaBufArray)),
|
|
m_numberOfBuffersAvailable(rhs.m_numberOfBuffersAvailable),
|
|
m_totalBytesAvailable(rhs.m_totalBytesAvailable),
|
|
m_totalBytesInUse(rhs.m_totalBytesInUse)
|
|
{
|
|
rhs.Reset();
|
|
}
|
|
|
|
//! Destructor.
|
|
~WsaBufHelper();
|
|
|
|
/**
|
|
* @brief Move assignment operator.
|
|
*
|
|
* @param rhs
|
|
*
|
|
* @return *this
|
|
*/
|
|
WsaBufHelper &operator=(ACTIV_BOOST_RV_REF(WsaBufHelper) rhs)
|
|
{
|
|
m_wsaBufArray = CxxYZ::move(rhs.m_wsaBufArray);
|
|
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 WSABUF array if required.
|
|
*
|
|
* @brief pData pointer to the data.
|
|
* @brief dataLength the length of the buffer.
|
|
*
|
|
* @throw Exception if the WSABUF couldn't be resized.
|
|
*/
|
|
void Append(const void * const pData, const size_t dataLength);
|
|
|
|
//! Get a pointer to the first WSABUF.
|
|
WsaBuf *GetFirstBuffer();
|
|
|
|
//! Get a const pointer to the first WSABUF.
|
|
const WsaBuf *GetFirstBuffer() const;
|
|
|
|
/**
|
|
* @brief Get the number of buffers available (the number of Append()s since the last Reset()).
|
|
*
|
|
* @return The number of buffers available.
|
|
*/
|
|
size_t GetNumberOfBuffersAvailable() const;
|
|
|
|
//! Get the total number of bytes in the WSABUF 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 iterator 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(WsaBufHelper)
|
|
|
|
/**
|
|
* @brief Extend the WSABUF array.
|
|
*
|
|
* @throw Exception if the WSABUF couldn't be resized.
|
|
*/
|
|
ACTIV_BASE_API void Extend();
|
|
|
|
const size_t m_initialSize; ///< Initial size of the WSABUF array.
|
|
WsaBufArray m_wsaBufArray; ///< WSABUF array.
|
|
size_t m_numberOfBuffersAvailable; ///< Number of buffers in m_wsaBufArray currently available.
|
|
size_t m_totalBytesAvailable; ///< Total number of bytes in WSABUF array.
|
|
size_t m_totalBytesInUse; ///< Number of bytes out of m_totalBytes that are in use (e.g. after a receive).
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline WsaBufHelper::WsaBufHelper(const size_t initialSize) :
|
|
m_initialSize(initialSize),
|
|
m_wsaBufArray(initialSize)
|
|
{
|
|
assert(0 != initialSize);
|
|
Reset();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline WsaBufHelper::~WsaBufHelper()
|
|
{
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void WsaBufHelper::Reset()
|
|
{
|
|
m_numberOfBuffersAvailable = 0;
|
|
m_totalBytesAvailable = 0;
|
|
m_totalBytesInUse = 0;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void WsaBufHelper::Append(const void * const pData, const size_t dataLength)
|
|
{
|
|
// Do we need to extend the WSABUF array?
|
|
if (m_numberOfBuffersAvailable == m_wsaBufArray.size())
|
|
Extend();
|
|
|
|
m_wsaBufArray[m_numberOfBuffersAvailable].buf = reinterpret_cast<char *>(const_cast<void *>(pData));
|
|
m_wsaBufArray[m_numberOfBuffersAvailable].len = static_cast<DWORD>(dataLength);
|
|
|
|
m_totalBytesAvailable += dataLength;
|
|
++m_numberOfBuffersAvailable;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline WsaBuf *WsaBufHelper::GetFirstBuffer()
|
|
{
|
|
return &m_wsaBufArray[0];
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline const WsaBuf *WsaBufHelper::GetFirstBuffer() const
|
|
{
|
|
return &m_wsaBufArray[0];
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline size_t WsaBufHelper::GetNumberOfBuffersAvailable() const
|
|
{
|
|
return m_numberOfBuffersAvailable;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline size_t WsaBufHelper::GetTotalBytesAvailable() const
|
|
{
|
|
return m_totalBytesAvailable;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline size_t WsaBufHelper::GetTotalBytesInUse() const
|
|
{
|
|
return m_totalBytesInUse;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void WsaBufHelper::SetTotalBytesInUse(const size_t totalBytesInUse)
|
|
{
|
|
m_totalBytesInUse = totalBytesInUse;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline WsaBufHelper::iterator WsaBufHelper::begin()
|
|
{
|
|
return m_wsaBufArray.begin();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline WsaBufHelper::iterator WsaBufHelper::end()
|
|
{
|
|
return m_wsaBufArray.begin() + m_numberOfBuffersAvailable;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline WsaBufHelper::const_iterator WsaBufHelper::begin() const
|
|
{
|
|
return m_wsaBufArray.begin();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline WsaBufHelper::const_iterator WsaBufHelper::end() const
|
|
{
|
|
return m_wsaBufArray.begin() + m_numberOfBuffersAvailable;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
typedef WsaBuf IpBuffer;
|
|
typedef WsaBufHelper IpBufferHelper; ///< Common name for vectored/scatter-gather i/o buffer helper.
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_BASE_WINDOWS_IP_WSA_BUF_HELPER_H)
|