Move git root from Client/ to src/ to track all source code: - Client: Game client source (moved to Client/Client/) - Server: Game server source - GameTools: Development tools - CryptoSource: Encryption utilities - database: Database scripts - Script: Game scripts - rylCoder_16.02.2008_src: Legacy coder tools - GMFont, Game: Additional resources 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
109 lines
2.3 KiB
C++
109 lines
2.3 KiB
C++
#include "stdafx.h"
|
|
#include "Packet.h"
|
|
|
|
#include <Network/Packet/PacketBase.h>
|
|
#include <Network/XORCrypt/XORCrypt.h>
|
|
|
|
#include <algorithm>
|
|
|
|
CMemoryPool::CMemoryPool(size_t nPerAllocateSize, size_t nPerAllocateNum)
|
|
: m_nPerAllocateSize(nPerAllocateSize), m_nPerAllocateNum(nPerAllocateNum)
|
|
{
|
|
for(size_t nCount = 0; nCount < m_nPerAllocateNum; ++nCount)
|
|
{
|
|
m_Pool.push_back(new char[m_nPerAllocateSize]);
|
|
}
|
|
}
|
|
|
|
struct FnPoolDeleteArray
|
|
{
|
|
bool operator () (char* lpPtr)
|
|
{
|
|
if(NULL != lpPtr)
|
|
{
|
|
delete [] lpPtr;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
};
|
|
|
|
CMemoryPool::~CMemoryPool()
|
|
{
|
|
CCSLock::Syncronize sync(m_PoolLock);
|
|
std::for_each(m_Pool.begin(), m_Pool.end(), FnPoolDeleteArray());
|
|
}
|
|
|
|
void* CMemoryPool::Alloc(size_t size)
|
|
{
|
|
CCSLock::Syncronize sync(m_PoolLock);
|
|
|
|
if(m_Pool.empty())
|
|
{
|
|
for(size_t nCount = 0; nCount < m_nPerAllocateNum; ++nCount)
|
|
{
|
|
m_Pool.push_back(new char[m_nPerAllocateSize]);
|
|
}
|
|
}
|
|
|
|
char* lpResult = m_Pool.back();
|
|
m_Pool.pop_back();
|
|
return lpResult;
|
|
}
|
|
|
|
void CMemoryPool::Free(void* ptr)
|
|
{
|
|
CCSLock::Syncronize sync(m_PoolLock);
|
|
m_Pool.push_back(reinterpret_cast<char*>(ptr));
|
|
}
|
|
|
|
|
|
CMemoryPool CPacket::ms_PacketPool(sizeof(CPacket), 20);
|
|
|
|
CPacket::CPacket()
|
|
: m_Len(0), m_nRefCount(1), m_bWrap(false)
|
|
{
|
|
}
|
|
|
|
CPacket::CPacket(unsigned short Len_In, unsigned char Cmd_In, unsigned short State_In, unsigned short Error_In)
|
|
: m_Len(0), m_nRefCount(1), m_bWrap(false)
|
|
{
|
|
((PktBase* )m_Buffer)->InitPtHead(Len_In, Cmd_In, State_In, Error_In);
|
|
}
|
|
|
|
CPacket::CPacket(unsigned short Len_In, unsigned char Cmd_In, unsigned long Tick_In)
|
|
: m_Len(0), m_nRefCount(1), m_bWrap(false)
|
|
{
|
|
((PktBase* )m_Buffer)->InitPtHead(Len_In, Cmd_In, Tick_In);
|
|
}
|
|
|
|
CPacket::~CPacket()
|
|
{
|
|
}
|
|
|
|
bool CPacket::WrapPacket(bool Crypt_In)
|
|
{
|
|
CXORCrypt& Crypt = CXORCrypt::GetInstance();
|
|
|
|
DWORD dwCodePage = Crypt.GetCodePage();
|
|
PktBase* lpBasePt = (PktBase* )m_Buffer;
|
|
|
|
m_Len = lpBasePt->GetLen();
|
|
|
|
if(m_bWrap)
|
|
return false;
|
|
|
|
// ÀÎÄÚµù ÆÐŶ
|
|
if(Crypt_In)
|
|
{
|
|
Crypt.EncodePacket(m_Buffer + PktBaseSize, m_Len - PktBaseSize, dwCodePage);
|
|
|
|
lpBasePt->SetCodePage(dwCodePage);
|
|
lpBasePt->SetCrypt();
|
|
}
|
|
|
|
// ÀÎÄÚµù ÇØ´õ
|
|
Crypt.EncodeHeader(m_Buffer + 1, PktBaseSize - 1, 0, 0);
|
|
return m_bWrap = true;
|
|
}
|