// 声明
class CCycleBuffer
{
public:
BOOL isFull();
BOOL isEmpty();
void Empty();
int GetLength();
CCycleBuffer(int size);
virtual ~CCycleBuffer();
int Write(char* buf, int count);
int Read(char* buf, int count);
private:
BOOL m_bEmpty, m_bFull;
char * m_pBuf;
int m_nBufSize;
int m_nReadPos;
int m_nWritePos;
};
// 定义
CCycleBuffer::CCycleBuffer(int size)
{
m_nBufSize = size;
m_nReadPos = 0;
m_nWritePos = 0;
m_pBuf = new char[m_nBufSize];
m_bEmpty = TRUE;
m_bFull = FALSE;
}
CCycleBuffer::~CCycleBuffer()
{
delete[] m_pBuf;
}
/************************************************************************/
/* 向缓冲区写入数据,返回实际写入的字节数 */
/************************************************************************/
int CCycleBuffer::Write(char* buf, int count)
{
if(count <= 0) return 0;
m_bEmpty = FALSE;
// 缓冲区已满,不能继续写入
if(m_bFull)
{
return 0;
}
else if(m_nReadPos == m_nWritePos) // 缓冲区为空时
{
/* == 内存模型 ==
(empty) m_nReadPos (empty)
|----------------------------------|-----------------------------------------|
&