Game Development Reference
In-Depth Information
virtual ~BinaryPacket() { SAFE_DELETE(m_Data); }
virtual char const * const VGetData() const { return m_Data; }
virtual u_long VGetSize() const { return ntohl(*(u_long *)m_Data); }
inline void MemCpy(char const *const data, size_t size, int destOffset);
};
Here I ' ve defined two different constructors, both of which take the size of the buffer
as an expected parameter. The first one takes a pointer to a data buffer that the
BinaryPacket object will copy into its own buffer. The second expects the API pro-
grammer, that
'
s you, to make repeated calls to MemCpy() to fill the buffer.
Here ' s the implementation of the constructors and MemCpy() :
inline BinaryPacket::BinaryPacket(char const * const data, u_long size)
{
m_Data = GCC_NEW char[size + sizeof(u_long)];
assert(m_Data);
*(u_long *)m_Data = htonl(size+sizeof(u_long));
memcpy(m_Data+sizeof(u_long), data, size);
}
inline BinaryPacket::BinaryPacket(u_long size)
{
m_Data = GCC_NEW char[size + sizeof(u_long)];
assert(m_Data);
*(u_long *)m_Data = htonl(size+sizeof(u_long));
}
inline void BinaryPacket::MemCpy(char const *const data, size_t size, int destOffset)
{
assert(size+destOffset <= VGetSize()-sizeof(u_long));
memcpy(m_Data + destOffset + sizeof(u_long), data, size);
}
Core Socket Classes
As you might expect, I
ve written a class to encapsulate a socket handle. It has four
virtual functions that can be overridden by implementers of child classes, or the class
can even be used as-is.
'
#define MAX_PACKET_SIZE (256)
#define RECV_BUFFER_SIZE (MAX_PACKET_SIZE * 512)
class NetSocket
{
friend class BaseSocketManager;
typedef std::list< shared_ptr <IPacket> > PacketList;
 
 
Search WWH ::




Custom Search