#include "windows.h"
class CBuffer{
public:
CBuffer(DWORD size = 1024)
{
if(size == 0)
size = 1024;
m_TxDSize = 0;
m_TxStart = 0;
m_TxBufSz = size;
m_pTxBuf = new BYTE[size];
if(m_pTxBuf == NULL)
m_TxBufSz = 0;
}
~CBuffer(void)
{
delete m_pTxBuf;
}
public:
int Read(BYTE *pBuf, DWORD nSize)
{
DWORD Num, cpsz, left;
Num = min(nSize, m_TxDSize);
if(Num == 0)
return 0;
cpsz = m_TxBufSz - m_TxStart;
cpsz = min(Num, cpsz);
if(cpsz)
{
memcpy(pBuf, m_pTxBuf + m_TxStart, cpsz);
//memset(m_pTxBuf + m_TxStart, 0, cpsz);
}
left = Num - cpsz;
if(left)
{
memcpy(pBuf+cpsz, m_pTxBuf, left);
//memset(m_pTxBuf, 0, left);
}
m_TxDSize -= Num;
m_TxStart += Num;
m_TxStart %= m_TxBufSz;
return Num;
}
int Write(BYTE *pBuf, DWORD nSize)
{
DWORD Num, tail, cpsz, left;
Num = m_TxBufSz - m_TxDSize;
Num = min(nSize, Num);
if(Num == 0)
return 0;
tail = (m_TxStart + m_TxDSize) % m_TxBufSz;
cpsz = m_TxBufSz - tail;
cpsz = min(Num, cpsz);
if(cpsz)
memcpy(m_pTxBuf + tail, pBuf, cpsz);
left = Num - cpsz;
if(left)
memcpy(m_pTxBuf, pBuf+cpsz, left);
m_TxDSize += Num;
return Num;
}
private:
BYTE *m_pTxBuf;
DWORD m_TxBufSz;
DWORD m_TxDSize;
DWORD m_TxStart;
};
缓冲区的C++封装
最新推荐文章于 2024-03-30 16:27:29 发布