Windows网络编程经验小结
2020-11-24 01:06
//仿照String 改写而成
//=====================================================================
// 二进制数据,主要用于收发网络缓冲区的数据
// CNetIOBuffer 以 MFC 类 CString 的源代码作为蓝本改写而成,用法与 CString 类似,
// 但是 CNetIOBuffer 中存放的是纯粹的二进制数据,‘/0‘ 并不作为它的结束标志。
// 其数据长度可以通过 GetLength() 获得,缓冲区地址可以通过运算符 LPBYTE 获得。
//=====================================================================
// Copyright (c) All-Vision Corporation. All rights reserved.
// Module: NetObject
// File: SimpleIOBuffer.h
// Author: gdy119
// Email : 8751webmaster@126.com
// Date: 2004.11.26
//=====================================================================
// NetIOBuffer.h
#ifndef _NETIOBUFFER_H
#define _NETIOBUFFER_H
//=====================================================================
#define MAX_BUFFER_LENGTH 1024*1024
//=====================================================================
//主要用来处理网络缓冲的数据
class CNetIOBuffer
{
protected:
LPBYTE m_pbinData;
int m_nLength;
int m_nTotalLength;
CRITICAL_SECTIONm_cs;
void Initvalibers();
public:
CNetIOBuffer();
CNetIOBuffer(const LPBYTE lbbyte, int nLength);
CNetIOBuffer(const CNetIOBuffer&binarySrc);
virtual ~CNetIOBuffer();
//=====================================================================
BOOL CopyData(const LPBYTE lbbyte, int nLength);
BOOL ConcatData(const LPBYTE lbbyte, int nLength);
void ResetIoBuffer();
int GetLength() const;
BOOL SetLength(int nLen);
LPBYTE GetCurPos();
int GetRemainLen();
BOOL IsEmpty() const;
operator LPBYTE() const;
static GetMaxLength() { return MAX_BUFFER_LENGTH; }
const CNetIOBuffer& operator=(const CNetIOBuffer& buffSrc);
};
#endif //
// NetOBuffer.cpp: implementation of the CNetIOBuffer class.
//=====================================================================
#include "stdafx.h"
#include "NetIOBuffer.h"
//=====================================================================
//=====================================================================
// Construction/Destruction
CNetIOBuffer::CNetIOBuffer()
{
Initvalibers();
}
CNetIOBuffer::CNetIOBuffer(const LPBYTE lbbyte, int nLength)
{
Initvalibers();
CopyData(lbbyte, nLength);
}
CNetIOBuffer::~CNetIOBuffer()
{
delete []m_pbinData;
m_pbinData=NULL;
DeleteCriticalSection(&m_cs);
}
CNetIOBuffer::CNetIOBuffer(const CNetIOBuffer&binarySrc)
{
Initvalibers();
CopyData(binarySrc,binarySrc.GetLength());
}
void CNetIOBuffer::Initvalibers()
{
m_pbinData = NULL;
m_nLength = 0;
m_nTotalLength = MAX_BUFFER_LENGTH;
if(m_pbinData==NULL)
{
m_pbinData=new BYTE[m_nTotalLength];
ASSERT(m_pbinData!=NULL);
}
InitializeCriticalSection(&m_cs);
}
void CNetIOBuffer::ResetIoBuffer()
{
EnterCriticalSection(&m_cs);
m_nLength = 0;
memset(m_pbinData,0,m_nTotalLength);
LeaveCriticalSection(&m_cs);
}
BOOL CNetIOBuffer::CopyData(const LPBYTE lbbyte, int nLength)
{
if( nLength > MAX_BUFFER_LENGTH )
return FALSE;
ResetIoBuffer();
EnterCriticalSection(&m_cs);
memcpy(m_pbinData, lbbyte, nLength );
m_nLength = nLength;
LeaveCriticalSection(&m_cs);
return TRUE;
}
BOOL CNetIOBuffer::ConcatData(const LPBYTE lbbyte, int nLength)
{
if( m_nLength + nLength > MAX_BUFFER_LENGTH )
return FALSE;
EnterCriticalSection(&m_cs);
memcpy(m_pbinData+m_nLength, lbbyte, nLength );
m_nLength += nLength;
LeaveCriticalSection(&m_cs);
return TRUE;
}
int CNetIOBuffer::GetLength() const
{
return m_nLength;
}
BOOL CNetIOBuffer::SetLength(int nLen)
{
if( nLen > MAX_BUFFER_LENGTH )
return FALSE;
EnterCriticalSection(&m_cs);
m_nLength = nLen;
LeaveCriticalSection(&m_cs);
return TRUE;
}
LPBYTE CNetIOBuffer::GetCurPos()
{
if( m_nLength
return (m_pbinData+m_nLength);
else
return NULL;
}
CNetIOBuffer::operator LPBYTE() const
{
return m_pbinData;
}
int CNetIOBuffer::GetRemainLen()
{
return MAX_BUFFER_LENGTH - m_nLength;
}
BOOL CNetIOBuffer::IsEmpty() const
{
return m_nLength == 0;
}
const CNetIOBuffer& CNetIOBuffer::operator=(const CNetIOBuffer& buffSrc)
{
if(&buffSrc!=this)
{
CopyData(buffSrc, buffSrc.GetLength());
}
return *this;
}
上一篇:web