#include <packetbuffer.h>
Inheritance diagram for ssobjects::PacketBuffer::
Public Types | |
enum | { pcInvalid = 0, pcLogin, pcLoginOkay, pcLoginBad, pcLogout, pcGetVersion, pcVersion, pcPing, pcPong, pcStatus, pcNetFileStart, pcNetFileData, pcNetFileEnd, pcClosed, pcNewConnection, pcAuthenticate, pcAuthReply, pcNoop = 100, pcUser = 256 } |
enum | { pkCookie = 0x4C50 } |
enum | { DefaultPacketBufferSize = 1000 } |
Public Methods | |
virtual void | makeNetworkReady (bool bOverrideFailsave=false) |
Convert header to network byte order (htonl). More... | |
virtual void | makeHostReady (bool bOverrideFailsave=false) |
Convert header from network byte order (ntohl). More... | |
virtual unsigned32 | getPacketSize () const |
Returns the size of the packet buffer plus the size of the header. More... | |
virtual unsigned32 | getBufferSizeMax () const |
Returns the maximum size of the packet. More... | |
virtual unsigned32 | getBufferSize () const |
Returns the size of the buffer. More... | |
virtual unsigned8 * | getBuffer () const |
Returns a pointer to the begining of the packets buffer. More... | |
virtual unsigned8 * | getPointer () const |
Returns the pointer to the buffer contents. More... | |
virtual unsigned8 * | getHeader () const |
Returns a pointer to the first byte of the header for this packet. More... | |
virtual unsigned8 * | resizeBuffer (unsigned32 nNewSize) |
Change the size of the packet buffer. More... | |
PacketBuffer (const PacketBuffer &) | |
copy constructor. More... | |
PacketBuffer & | PacketBuffer::operator= (const PacketBuffer &packet) |
assignment operator. More... | |
PacketBuffer (unsigned16 wCmd, unsigned32 nSize=DefaultPacketBufferSize) | |
Most commonly used constructor. More... | |
PacketBuffer () | |
Construct an empty packet. More... | |
virtual | ~PacketBuffer () |
unsigned32 | getID () |
Returns the unique ID of this packet. More... | |
unsigned16 | getCmd () const |
Returns the command (type) of this packet. More... | |
unsigned16 | cookie () const |
Returns the cookie; the always constant value. More... | |
void | rewind () |
Puts the index pointer to the beginning of the buffer. More... | |
void | reset () |
Resets the packet and the packets header. More... | |
void | append (unsigned8 *pdata, unsigned32 nSize) |
Add data to the end of the buffer. More... | |
void | operator<< (const char *) |
Add a zero terminated string. More... | |
void | operator<< (char *) |
Add a zero terminated string. More... | |
void | operator<< (CStr &) |
Add a zero terminated string. More... | |
void | operator<< (signed32) |
Add a signed 32-bit number. More... | |
void | operator<< (unsigned32) |
Add a unsigned 32-bit number. More... | |
void | operator<< (signed16) |
Add a signed 16-bit number. More... | |
void | operator<< (unsigned16) |
Add a unsigned 16-bit number. More... | |
void | operator<< (unsigned8) |
Add a signed 8-bit number. More... | |
void | operator<< (double) |
Add a double number. More... | |
void | operator<< (float) |
Add a float number. More... | |
void | operator>> (char *) |
Extract a zero terminated string. More... | |
void | operator>> (CStr &) |
Extract a zero terminated string. More... | |
void | operator>> (signed32 &) |
Extract a signed 32-bit number. More... | |
void | operator>> (unsigned32 &) |
Extract a unsigned 32-bit number. More... | |
void | operator>> (signed16 &) |
Extract a signed 16-bit number. More... | |
void | operator>> (unsigned16 &) |
Extract a unsigned 16-bit number. More... | |
void | operator>> (unsigned8 &) |
Extract a unsigned 8-bit number. More... | |
void | operator>> (double &) |
Extract a double number. More... | |
void | operator>> (float &) |
Extract a float number. More... | |
Static Public Methods | |
unsigned32 | getHeaderSize () |
Public Attributes | |
PacketBufferHeader | m_packetHeader |
Contains information about this packet. More... | |
Protected Methods | |
virtual void | receive (BufferedSocket *) |
Called to fill itself with data. More... | |
virtual void | transmit (BufferedSocket *) |
Called to send itself. More... | |
virtual void | process () |
Called when data is read in from ccClient. This is an empty implimentation. You need to override it if you want this functionality. More... | |
Protected Attributes | |
unsigned8 * | m_Buffer |
Buffer that holds the data holds data that will be sent. More... | |
unsigned8 * | m_pPointer |
Current position in the buffer. More... | |
bool | m_bUsed |
Is this packet in use? More... | |
unsigned32 | m_nID |
Unique ID. More... | |
unsigned32 | m_nBufferSizeMax |
How big the buffer can get. More... | |
friend | ClientConnector |
Friend of ClientConnector class. More... | |
friend | BufferedSocket |
Friend of BufferedSocket class. More... | |
Static Protected Attributes | |
unsigned32 | m_nUniqueID = 1 |
Unique ID to keep all packets unique. More... |
The packet object is the base class for storing any information you indend to send, and when data is received, it is put into a PacketBuffer object. The object has operators for storing and extracting 8, 16 or 32 bit numbers, as well as zero terminated strings and CStr objects.
To store data in the packet, you would typically construct the packet with the command type, and then insert data into it via the "<<" operator. For example:
... PacketBuffer ping(PacketBuffer::pcAuthenticate); unsigned32 nVersion = 100; //version 1.00 ping << "lpatterson"; //user name ping << "mypassword"; //password ping << 100; //version ... The server would extract the data like this: ... pPacket = psocket->recvPacket(); CStr sUserName; CStr sPassword; unsigned32 nVersion; *pPacket >> sUserName; *pPacket >> sPassword; *pPacket >> nVersion; ...
|
These enums are provided for convience. When you construct a new Packet object, you will pass a packet command listed here, or from one you create. For instance, you would create a new packet like this:
PacketBuffer login(PacketBuffer::pcLogin); When you want to create your own packet commands, you should create a new class and dirive it from PacketBuffer, and start your enumeration from pcUser. All ones below pcUser are reserved for furture use. "pc" part stands for Packet Command.
class ChessPacket : public PacketBuffer { public: pcNewGame = pcUser, //start your packets at pcUser and up pcQuitGame, pcStartGame }; ... //create a new game packet PacketBuffer newGame(ChessPacket::pcNewGame); ...
|
|
|
|
|
|
copy constructor.
|
|
Most commonly used constructor. The most commonly used packet constructor. You usually construct passing in the packet command. The command may also be refered to as the packet type. Basically, the command is how you tell what you are suppose to do with the packet once you receive it. An example of creating one would be
PacketBuffer ping(PacketBuffer::pcPing); "pc" part of "pcPing" stands for Packet Command. Normally, if you need additional packet commands, you create a new class, and dirive it from PacketBuffer. For instance:
From mypackts.h: class mypackets : public PacketBuffer { public: pcBackflip = pcUser, //start at user, don't start at anything below, pcCartWheel //as they are reserved for ssobjects }; |
|
Construct an empty packet. An empty packet is created with a default packet buffer size of PacketBuffer::DefaultPacketBufferSize. You normally only create an empty packet if you are using it to receive data. However, the most common way to receive data is to let BufferedSocket or SimpleServer create the packet for you. |
|
|
|
assignment operator.
|
|
Add data to the end of the buffer. Adds data to the end of the buffer, and increases the buffer size. Good way to add misc data. The size of the data that was appended is not stored however. This means that when you are extracting the data, you will some way to know how much data to extract.
|
|
Returns the cookie; the always constant value.
|
|
Returns a pointer to the begining of the packets buffer. A PacketBuffer object contains a buffer to hold all the information you will be transmitting or receiving. This returns the actual buffer.
|
|
Returns the size of the buffer.
|
|
Returns the maximum size of the packet.
|
|
Returns the command (type) of this packet.
|
|
Returns a pointer to the first byte of the header for this packet.
|
|
|
|
Returns the unique ID of this packet.
|
|
Returns the size of the packet buffer plus the size of the header.
|
|
Returns the pointer to the buffer contents. To keep track of where the PacketBuffer object last put data, a index pointer is maintained that points one byte past the last data that was appended to the buffer. This returns that index.
|
|
Convert header from network byte order (ntohl). Puts the header into host-byte order.
|
|
Convert header to network byte order (htonl). Puts the header into network-byte order.
|
|
Add a float number. Copies Number to the buffer. The number is not converted to network-byte order, as there are no network conversion routines for this.
|
|
Add a double number. Copies Number to the buffer. The number is not converted to network-byte order, as there are no network conversion routines for this.
|
|
Add a signed 8-bit number. Copies Number to the buffer. The number is converted to network-byte order when it is stored in the buffer.
|
|
Add a unsigned 16-bit number. Copies Number to the buffer. The number is converted to network-byte order when it is stored in the buffer.
|
|
Add a signed 16-bit number. Copies Number to the buffer. The number is converted to network-byte order when it is stored in the buffer.
|
|
Add a unsigned 32-bit number. Copies Number to the buffer. The number is converted to network-byte order when it is stored in the buffer.
|
|
Add a signed 32-bit number. Copies Number to the buffer. The number is converted to network-byte order when it is stored in the buffer.
|
|
Add a zero terminated string. Copies String to the buffer. String is stared as a zero terminated string. If String has no buffer, an empty string is stored. An empty string consists of a single zero byte.
|
|
Add a zero terminated string. Copies the zero terminated string pointed to by pString
|
|
Add a zero terminated string. Copies the zero terminated string pointed to by pString
|
|
Extract a float number. Copies the float number in the buffer to Number. Number will be in host-byte order. The number in the buffer is NOT expected to be in network-byte order as there is no network conversion routines for this.
|
|
Extract a double number. Copies the double number in the buffer to Number. Number will be in host-byte order. The number in the buffer is NOT expected to be in network-byte order as there is no network conversion routines for this.
|
|
Extract a unsigned 8-bit number. Copies the 8-bit (BYTE) number in the buffer to Number. Number will be in host-byte order. The number in the buffer is expected to be in network-byte order. When the insertion operator "<<" is used, it will be. |
|
Extract a unsigned 16-bit number. Copies the 16-bit (WORD) number in the buffer to Number. Number will be in host-byte order. The number in the buffer is expected to be in network-byte order. When the insertion operator "<<" is used, it will be. |
|
Extract a signed 16-bit number. Copies the 16-bit number in the buffer to Number. Number will be in host-byte order. The number in the buffer is expected to be in network-byte order. When the insertion operator "<<" is used, it will be. |
|
Extract a unsigned 32-bit number. Copies the 32-bit (DWORD) number in the buffer to Number. Number will be in host-byte order. The number in the buffer is expected to be in network-byte order. When the insertion operator "<<" is used, it will be. |
|
Extract a signed 32-bit number. Copies the 32-bit number in the buffer to Number. Number will be in host-byte order. The number in the buffer is expected to be in network-byte order. When the insertion operator "<<" is used, it will be. |
|
Extract a zero terminated string. Copies the zero terminated string in the buffer to String. This is a safer way to extract strings then the char* operator. |
|
Extract a zero terminated string. Copies the data from the buffer to pString. pString is assumed to have enough space to hold the string. If pString is not large enough, the results are undefined. It's a good idea to either use a CStr object instead, or allocate a string the same size as the PacketBuffers max buffer size. pString will be zero terminated. |
|
Called when data is read in from ccClient. This is an empty implimentation. You need to override it if you want this functionality.
Reimplemented in ssobjects::Communicable. |
|
Called to fill itself with data. Calls the sockets recvPacket to read data into this packet object. Once the data has been received, process() is called. If this object is part of a Communicable object, then process is actually a call to Communicable::process which will in turn call Communicable::extract method to start the attribute populating process.
Reimplemented in ssobjects::NetFile. |
|
Resets the packet and the packets header. Puts header back to an initial state by making the index pointer point to the beginning of the buffer, and calling the headers reset method which sets the buffer size to 0, and puts header into host byte order. |
|
Change the size of the packet buffer. If you wish to increase or decrease the size of your packets buffer, use this. It allocates new memory, copies the existing buffer data to the new memory location, then deletes the old memory. This isn't very efficent.
|
|
Puts the index pointer to the beginning of the buffer.
|
|
Called to send itself. Sends the packet over the socket psocket
Reimplemented in ssobjects::NetFile. |
|
Friend of BufferedSocket class.
|
|
Friend of ClientConnector class.
|
|
Buffer that holds the data holds data that will be sent.
|
|
Is this packet in use?
|
|
How big the buffer can get.
|
|
Unique ID.
|
|
Unique ID to keep all packets unique.
|
|
Current position in the buffer.
|
|
Contains information about this packet.
|