代码拉取完成,页面将自动刷新
#include <string.h>
#include <assert.h>
#include "msgbuffer.h"
#pragma warning(disable: 4244)
CMsgBuffer::CMsgBuffer(const char *buffername, void(*ef)(const char *fmt, ...) /*= NULL*/)
{
m_pszBufferName = buffername;
m_pfnErrorFunc = ef;
m_bAllowOverflow = false;
m_bOverFlowed = false;
m_nMaxSize = NET_MAXMESSAGE;
m_nPushedCount = 0;
m_bPushed = false;
m_nReadCount = 0;
m_bBadRead = false;
Clear();
}
CMsgBuffer::~CMsgBuffer(void)
{
}
void CMsgBuffer::Push(void)
{
assert(!m_bPushed);
m_nPushedCount = m_nReadCount;
m_bPushed = true;
}
void CMsgBuffer::Pop(void)
{
assert(m_bPushed);
m_nReadCount = m_nPushedCount;
m_bPushed = false;
}
void CMsgBuffer::SetOverflow(bool allowed)
{
m_bAllowOverflow = allowed;
}
int CMsgBuffer::GetMaxSize(void)
{
return m_nMaxSize;
}
void * CMsgBuffer::GetData(void)
{
return m_rgData;
}
int CMsgBuffer::GetCurSize(void)
{
return m_nCurSize;
}
int CMsgBuffer::GetReadCount(void)
{
return m_nReadCount;
}
void CMsgBuffer::SetTime(float time)
{
m_fRecvTime = time;
}
float CMsgBuffer::GetTime()
{
return m_fRecvTime;
}
void CMsgBuffer::WriteByte(int c)
{
unsigned char *buf;
buf = (unsigned char *)GetSpace(1);
buf[0] = c;
}
void CMsgBuffer::WriteShort(int c)
{
unsigned char *buf;
buf = (unsigned char *)GetSpace(2);
buf[0] = c & 0xff;
buf[1] = c >> 8;
}
void CMsgBuffer::WriteLong(int c)
{
unsigned char *buf;
buf = (unsigned char *)GetSpace(4);
buf[0] = c & 0xff;
buf[1] = (c >> 8) & 0xff;
buf[2] = (c >> 16) & 0xff;
buf[3] = c >> 24;
}
void CMsgBuffer::WriteFloat(float f)
{
union
{
float f;
int l;
} dat;
dat.f = f;
Write(&dat.l, 4);
}
void CMsgBuffer::WriteString(const char *s)
{
if (!s)
{
Write("", 1);
}
else
{
Write(s, strlen(s) + 1);
}
}
void CMsgBuffer::WriteBuf(int iSize, void *buf)
{
if (!buf)
{
return;
}
Write(buf, iSize);
}
void CMsgBuffer::WriteEnd()
{
if (m_nCurSize <= NET_MAXMESSAGE - 4)
{
int nSize = m_nCurSize;
memcpy(&m_rgData[4], m_rgData, m_nCurSize);
memcpy(m_rgData, &nSize, sizeof(int));
m_nCurSize += 4;
}
else
{
if (m_pfnErrorFunc)
{
m_pfnErrorFunc("CMsgBuffer(%s), no room for %i bytes, %i / %i already in use\n", m_pszBufferName, 4, m_nCurSize, m_nMaxSize);
}
}
}
void CMsgBuffer::BeginReading(void)
{
m_nReadCount = 0;
m_bBadRead = false;
}
int CMsgBuffer::ReadByte(void)
{
int c;
if (m_nReadCount + 1 > m_nCurSize)
{
m_bBadRead = true;
return -1;
}
c = (unsigned char)m_rgData[m_nReadCount];
m_nReadCount++;
return c;
}
int CMsgBuffer::ReadShort(void)
{
int c;
if (m_nReadCount + 2 > m_nCurSize)
{
m_bBadRead = true;
return -1;
}
c = (short)(m_rgData[m_nReadCount] + (m_rgData[m_nReadCount + 1] << 8));
m_nReadCount += 2;
return c;
}
int CMsgBuffer::ReadLong(void)
{
int c;
if (m_nReadCount + 4 > m_nCurSize)
{
m_bBadRead = true;
return -1;
}
c = m_rgData[m_nReadCount]
+ (m_rgData[m_nReadCount + 1] << 8)
+ (m_rgData[m_nReadCount + 2] << 16)
+ (m_rgData[m_nReadCount + 3] << 24);
m_nReadCount += 4;
return c;
}
float CMsgBuffer::ReadFloat(void)
{
union
{
unsigned char b[4];
float f;
} dat;
dat.b[0] = m_rgData[m_nReadCount];
dat.b[1] = m_rgData[m_nReadCount + 1];
dat.b[2] = m_rgData[m_nReadCount + 2];
dat.b[3] = m_rgData[m_nReadCount + 3];
m_nReadCount += 4;
return dat.f;
}
int CMsgBuffer::ReadBuf(int iSize, void *pbuf)
{
if (m_nReadCount + iSize > m_nCurSize)
{
m_bBadRead = true;
return -1;
}
memcpy(pbuf, &m_rgData[m_nReadCount], iSize);
m_nReadCount += iSize;
return 1;
}
char *CMsgBuffer::ReadString(void)
{
static char string[NET_MAXMESSAGE];
int l, c;
l = 0;
do
{
c = (char)ReadByte();
if (c == -1 || c == 0)
break;
string[l] = c;
l++;
} while (l < sizeof(string) - 1);
string[l] = 0;
return string;
}
void CMsgBuffer::Clear(void)
{
m_nCurSize = 0;
m_bOverFlowed = false;
m_nReadCount = 0;
m_bBadRead = false;
memset(m_rgData, 0, sizeof(m_rgData));
}
void *CMsgBuffer::GetSpace(int length)
{
void *d;
if (m_nCurSize + length > m_nMaxSize)
{
if (!m_bAllowOverflow)
{
if (m_pfnErrorFunc)
{
(*m_pfnErrorFunc)("CMsgBuffer(%s), no room for %i bytes, %i / %i already in use\n", m_pszBufferName, length, m_nCurSize, m_nMaxSize);
}
return NULL;
}
if (length > m_nMaxSize)
{
if (m_pfnErrorFunc)
{
(*m_pfnErrorFunc)("CMsgBuffer(%s), no room for %i bytes, %i is max\n", m_pszBufferName, length, m_nMaxSize);
}
return NULL;
}
m_bOverFlowed = true;
Clear();
}
d = m_rgData + m_nCurSize;
m_nCurSize += length;
return d;
}
void CMsgBuffer::Write(const void *m_rgData, int length)
{
memcpy(GetSpace(length), m_rgData, length);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。