作者在 2010-01-17 12:41:22 发布以下内容
// Buffer.h: interface for the CBuffer class.
//
//////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_BUFFER_H__B477EDB0_45FF_4DCC_910F_1EF9BF4F94DF__INCLUDED_)
#define AFX_BUFFER_H__B477EDB0_45FF_4DCC_910F_1EF9BF4F94DF__INCLUDED_
#define AFX_BUFFER_H__B477EDB0_45FF_4DCC_910F_1EF9BF4F94DF__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define MAXLEN 1024*1024*30
#define ADDLEN 1024*1024
#define ADDLEN 1024*1024
class CBuffer
{
public:
CBuffer();
virtual ~CBuffer();
{
public:
CBuffer();
virtual ~CBuffer();
void AddData(char* pData, int length);
int ReadData(char* pData, int length);
void Reset();
private:
char* m_buffer;
char* m_pBegin;
char* m_pEnd;
long m_nCurLen;
long m_nMaxLen;
long m_nSpaceLen;
long m_nAddLen;
CRITICAL_SECTION cs;
};
int ReadData(char* pData, int length);
void Reset();
private:
char* m_buffer;
char* m_pBegin;
char* m_pEnd;
long m_nCurLen;
long m_nMaxLen;
long m_nSpaceLen;
long m_nAddLen;
CRITICAL_SECTION cs;
};
#endif // !defined(AFX_BUFFER_H__B477EDB0_45FF_4DCC_910F_1EF9BF4F94DF__INCLUDED_)
// Buffer.cpp: implementation of the CBuffer class.
//
//////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Buffer.h"
#include "Buffer.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CBuffer::CBuffer()
{
m_nAddLen = ADDLEN;
m_nMaxLen = MAXLEN;
m_buffer = new char[m_nMaxLen];
memset(m_buffer,0,m_nMaxLen);
m_nCurLen = 0;
m_nSpaceLen = m_nMaxLen;
m_pBegin = m_buffer;
m_pEnd = m_buffer;
{
m_nAddLen = ADDLEN;
m_nMaxLen = MAXLEN;
m_buffer = new char[m_nMaxLen];
memset(m_buffer,0,m_nMaxLen);
m_nCurLen = 0;
m_nSpaceLen = m_nMaxLen;
m_pBegin = m_buffer;
m_pEnd = m_buffer;
InitializeCriticalSection(&cs);
}
}
CBuffer::~CBuffer()
{
if(m_buffer != NULL)
{
delete[] m_buffer;
m_buffer = NULL;
}
m_nAddLen = 0;
m_nMaxLen = 0;
m_nCurLen = 0;
m_nSpaceLen = 0;
m_pBegin = NULL;
m_pEnd = NULL;
{
if(m_buffer != NULL)
{
delete[] m_buffer;
m_buffer = NULL;
}
m_nAddLen = 0;
m_nMaxLen = 0;
m_nCurLen = 0;
m_nSpaceLen = 0;
m_pBegin = NULL;
m_pEnd = NULL;
DeleteCriticalSection(&cs);
}
}
void CBuffer::Reset()
{
EnterCriticalSection(&cs);
__try
{
delete[] m_buffer;
m_buffer = NULL;
m_nAddLen = ADDLEN;
m_nMaxLen = MAXLEN;
m_buffer = new char[m_nMaxLen];
memset(m_buffer,0,m_nMaxLen);
m_nCurLen = 0;
m_nSpaceLen = m_nMaxLen;
m_pBegin = m_buffer;
m_pEnd = m_buffer;
}
__finally
{
LeaveCriticalSection(&cs);
}
}
{
EnterCriticalSection(&cs);
__try
{
delete[] m_buffer;
m_buffer = NULL;
m_nAddLen = ADDLEN;
m_nMaxLen = MAXLEN;
m_buffer = new char[m_nMaxLen];
memset(m_buffer,0,m_nMaxLen);
m_nCurLen = 0;
m_nSpaceLen = m_nMaxLen;
m_pBegin = m_buffer;
m_pEnd = m_buffer;
}
__finally
{
LeaveCriticalSection(&cs);
}
}
void CBuffer::AddData(char* pData, int length)
{
int addLen = 0;
int nTruncateLen = 0;
EnterCriticalSection(&cs);
__try
{
if(length >= m_nSpaceLen)
{
addLen = length-m_nSpaceLen;
addLen = max(addLen,m_nAddLen);
char* pNewBuffer = new char[m_nMaxLen + addLen];
memset(pNewBuffer,0,m_nMaxLen+addLen);
if(m_pBegin < m_pEnd)
{
memcpy(pNewBuffer,m_buffer,m_nCurLen);
delete[] m_buffer;
m_buffer = pNewBuffer;
m_pBegin = m_buffer;
m_pEnd = m_buffer + m_nCurLen;
}
if(m_pBegin > m_pEnd)
{
nTruncateLen = (m_buffer+m_nMaxLen)-m_pBegin;
memcpy(pNewBuffer,m_pBegin,nTruncateLen);
memcpy(pNewBuffer+nTruncateLen,m_buffer,m_pEnd-m_buffer);
delete[] m_buffer;
m_buffer = pNewBuffer;
m_pBegin = m_buffer;
m_pEnd = m_buffer+m_nCurLen;
}
m_nMaxLen += addLen;
m_nSpaceLen += addLen;
}
if(m_pBegin > m_pEnd)
{
memcpy(m_pEnd,pData,length);
m_pEnd += length;
}
if(m_pBegin <= m_pEnd)
{
nTruncateLen = m_buffer+m_nMaxLen-m_pEnd;
if(length > nTruncateLen)
{
memcpy(m_pEnd,pData,nTruncateLen);
m_pEnd = m_buffer;
memcpy(m_pEnd,pData+nTruncateLen,length-nTruncateLen);
m_pEnd += length-nTruncateLen;
}
else
{
memcpy(m_pEnd,pData,length);
m_pEnd += length;
}
}
m_nCurLen += length;
m_nSpaceLen -= length;
}
__finally
{
LeaveCriticalSection(&cs);
}
}
{
int addLen = 0;
int nTruncateLen = 0;
EnterCriticalSection(&cs);
__try
{
if(length >= m_nSpaceLen)
{
addLen = length-m_nSpaceLen;
addLen = max(addLen,m_nAddLen);
char* pNewBuffer = new char[m_nMaxLen + addLen];
memset(pNewBuffer,0,m_nMaxLen+addLen);
if(m_pBegin < m_pEnd)
{
memcpy(pNewBuffer,m_buffer,m_nCurLen);
delete[] m_buffer;
m_buffer = pNewBuffer;
m_pBegin = m_buffer;
m_pEnd = m_buffer + m_nCurLen;
}
if(m_pBegin > m_pEnd)
{
nTruncateLen = (m_buffer+m_nMaxLen)-m_pBegin;
memcpy(pNewBuffer,m_pBegin,nTruncateLen);
memcpy(pNewBuffer+nTruncateLen,m_buffer,m_pEnd-m_buffer);
delete[] m_buffer;
m_buffer = pNewBuffer;
m_pBegin = m_buffer;
m_pEnd = m_buffer+m_nCurLen;
}
m_nMaxLen += addLen;
m_nSpaceLen += addLen;
}
if(m_pBegin > m_pEnd)
{
memcpy(m_pEnd,pData,length);
m_pEnd += length;
}
if(m_pBegin <= m_pEnd)
{
nTruncateLen = m_buffer+m_nMaxLen-m_pEnd;
if(length > nTruncateLen)
{
memcpy(m_pEnd,pData,nTruncateLen);
m_pEnd = m_buffer;
memcpy(m_pEnd,pData+nTruncateLen,length-nTruncateLen);
m_pEnd += length-nTruncateLen;
}
else
{
memcpy(m_pEnd,pData,length);
m_pEnd += length;
}
}
m_nCurLen += length;
m_nSpaceLen -= length;
}
__finally
{
LeaveCriticalSection(&cs);
}
}
int CBuffer::ReadData(char* pData, int length)
{
int nTruncateLen = 0;
EnterCriticalSection(&cs);
__try
{
if(m_nCurLen < length)
return 0;
if(m_pEnd > m_pBegin)
{
memcpy(pData,m_pBegin,length);
m_pBegin += length;
}
else
{
nTruncateLen = m_buffer+m_nMaxLen-m_pBegin;
if(nTruncateLen >= length)
{
memcpy(pData,m_pBegin,length);
m_pBegin += length;
}
else
{
memcpy(pData,m_pBegin,nTruncateLen);
m_pBegin = m_buffer;
memcpy(pData+nTruncateLen,m_pBegin,length-nTruncateLen);
m_pBegin += length-nTruncateLen;
}
}
m_nCurLen -= length;
m_nSpaceLen += length;
return length;
}
__finally
{
LeaveCriticalSection(&cs);
}
return 0;
}
{
int nTruncateLen = 0;
EnterCriticalSection(&cs);
__try
{
if(m_nCurLen < length)
return 0;
if(m_pEnd > m_pBegin)
{
memcpy(pData,m_pBegin,length);
m_pBegin += length;
}
else
{
nTruncateLen = m_buffer+m_nMaxLen-m_pBegin;
if(nTruncateLen >= length)
{
memcpy(pData,m_pBegin,length);
m_pBegin += length;
}
else
{
memcpy(pData,m_pBegin,nTruncateLen);
m_pBegin = m_buffer;
memcpy(pData+nTruncateLen,m_pBegin,length-nTruncateLen);
m_pBegin += length-nTruncateLen;
}
}
m_nCurLen -= length;
m_nSpaceLen += length;
return length;
}
__finally
{
LeaveCriticalSection(&cs);
}
return 0;
}