#ifndef _CPacket #define _CPacket #define WIN32_LEAN_AND_MEAN #include #include #include #include #include #include "Socket/SocketIO.h" class CMemoryPool { public: CMemoryPool(size_t nPerAllocateSize, size_t PerAllocateNum); ~CMemoryPool(); void* Alloc(size_t size); void Free(void* ptr); private: CCSLock m_PoolLock; CACHE_PAD(PoolLockPadding, sizeof(CCSLock)); size_t m_nPerAllocateSize; size_t m_nPerAllocateNum; std::vector m_Pool; }; class CPacket { protected: volatile LONG m_nRefCount; char m_Buffer[BufferSize]; unsigned short m_Len; bool m_bWrap; public: CPacket(); CPacket(unsigned short Len_In, unsigned char Cmd_In, unsigned short State_In, unsigned short Error_In); CPacket(unsigned short Len_In, unsigned char Cmd_In, unsigned long Tick_In); ~CPacket(); WSABUF GetWSABuf() { WSABUF WSABuf = { m_Len, m_Buffer }; return WSABuf; } char * GetBuf(void) { return m_Buffer; }; inline LONG AddRef() { return InterlockedIncrement(&m_nRefCount); } inline LONG Release(); bool WrapPacket(bool Crypt_In); static CMemoryPool ms_PacketPool; static void* operator new(size_t size) { return ms_PacketPool.Alloc(size); } static void operator delete(void *ptr) { ms_PacketPool.Free(ptr); } }; typedef CPacket* LPCPacket; inline LONG CPacket::Release() { LONG nRefCount = InterlockedDecrement(&m_nRefCount); if(0 == nRefCount) { delete this; } return nRefCount; } #endif