Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

ssobjects::PacketBuffer Class Reference

Packet object used for storing data that is sent or received. More...

#include <packetbuffer.h>

Inheritance diagram for ssobjects::PacketBuffer::

ssobjects::Communicable ssobjects::NetFile List of all members.

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 unsigned8getBuffer () const
 Returns a pointer to the begining of the packets buffer. More...

virtual unsigned8getPointer () const
 Returns the pointer to the buffer contents. More...

virtual unsigned8getHeader () const
 Returns a pointer to the first byte of the header for this packet. More...

virtual unsigned8resizeBuffer (unsigned32 nNewSize)
 Change the size of the packet buffer. More...

 PacketBuffer (const PacketBuffer &)
 copy constructor. More...

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...


Public Attributes

PacketBufferHeader m_packetHeader
 Contains information about this packet. More...


Static Public Methods

unsigned32 getHeaderSize ()

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

unsigned8m_Buffer
 Buffer that holds the data holds data that will be sent. More...

unsigned8m_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...


Detailed Description

Packet object used for storing data that is sent or received.

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; 
   ... 
   


Member Enumeration Documentation

anonymous enum
 

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); 
       ... 
       
Enumeration values:
pcInvalid   0 - Invalid packet or not initialized.
pcLogin   1 - server wants client to login.
pcLoginOkay   2 - user was validated okay.
pcLoginBad   3 - user was not validated.
pcLogout   4 - client is logging out.
pcGetVersion   5 - version info.
pcVersion   6 - version info.
pcPing   7 - we are expecting a pong back.
pcPong   8 - reply to a ping.
pcStatus   9 - generic status query/result (18).
pcNetFileStart   10 - file being sent over network contains: filesize, filename.
pcNetFileData   11 - data for network file contains: size (size of contained data).
pcNetFileEnd   12 - terminater contains: nothing.
pcClosed   13 - a connection was closed.
pcNewConnection   14 - a new connection was made.
pcAuthenticate   15 - client is authenticating.
pcAuthReply   16 - server is replying to authentication request.
pcNoop   100 - no op. Do nothing with this, except to have a case in msg handler for it.
pcUser   256 (100H) - user defined packets start here, first 55H are reserved.

anonymous enum
 

Enumeration values:
pkCookie   Constant value used to make sure a packet is valid.

anonymous enum
 

Enumeration values:
DefaultPacketBufferSize   Default size of a packet.


Constructor & Destructor Documentation

ssobjects::PacketBuffer::PacketBuffer ( const PacketBuffer & packet )
 

copy constructor.

ssobjects::PacketBuffer::PacketBuffer ( unsigned16 wCmd,
unsigned32 nSize = DefaultPacketBufferSize )
 

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 
   }; 
   

ssobjects::PacketBuffer::PacketBuffer ( )
 

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.

ssobjects::PacketBuffer::~PacketBuffer ( ) [virtual]
 


Member Function Documentation

void ssobjects::PacketBuffer::append ( unsigned8 * pdata,
unsigned32 nSize )
 

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.

Parameters:
pdata   Pointer to the data you will be copying into the packet buffer.
nSize   The number of bytes you will be copying into the packet buffer.
Exceptions:
PacketBufferException   If there is not enough room left in the buffer.

Note:
There is no extraction method to compliment this method. Using this method is discouraged.

unsigned16 ssobjects::PacketBuffer::cookie ( ) const [inline]
 

Returns the cookie; the always constant value.

unsigned8 * ssobjects::PacketBuffer::getBuffer ( ) const [virtual]
 

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:
A pointer to the begining of the buffer.
Exceptions:
PacketBufferException   If the buffer is not allocated.

unsigned32 ssobjects::PacketBuffer::getBufferSize ( ) const [inline, virtual]
 

Returns the size of the buffer.

unsigned32 ssobjects::PacketBuffer::getBufferSizeMax ( ) const [inline, virtual]
 

Returns the maximum size of the packet.

unsigned16 ssobjects::PacketBuffer::getCmd ( ) const [inline]
 

Returns the command (type) of this packet.

unsigned8 * ssobjects::PacketBuffer::getHeader ( ) const [inline, virtual]
 

Returns a pointer to the first byte of the header for this packet.

unsigned32 ssobjects::PacketBuffer::getHeaderSize ( ) [inline, static]
 

unsigned32 ssobjects::PacketBuffer::getID ( ) [inline]
 

Returns the unique ID of this packet.

unsigned32 ssobjects::PacketBuffer::getPacketSize ( ) const [inline, virtual]
 

Returns the size of the packet buffer plus the size of the header.

unsigned8 * ssobjects::PacketBuffer::getPointer ( ) const [virtual]
 

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.

Returns:
A pointer to the index pointer.
Exceptions:
PacketBufferException   If there is no buffer allocated.

void ssobjects::PacketBuffer::makeHostReady ( bool bOverrideFailsave = false ) [virtual]
 

Convert header from network byte order (ntohl).

Puts the header into host-byte order.

Parameters:
bOverrideFailsafe   [in] false (default) will not reorder the bytes if you have already. true if you want to anyway.

void ssobjects::PacketBuffer::makeNetworkReady ( bool bOverrideFailsave = false ) [virtual]
 

Convert header to network byte order (htonl).

Puts the header into network-byte order.

Parameters:
bOverrideFailsafe   [in] false (default) will not reorder the bytes if you have already. true if you want to anyway.

void ssobjects::PacketBuffer::operator<< ( float Number )
 

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.

Exceptions:
PacketBufferException   If there is not enough room left in the buffer.

Note:
This has not been tested on big-indian machines. You may want to convert Number to a string, and pass it as a zero terminated string instead to be safe.

void ssobjects::PacketBuffer::operator<< ( double Number )
 

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.

Exceptions:
PacketBufferException   If there is not enough room left in the buffer.

Note:
This has not been tested on big-indian machines. You may want to convert Number to a string, and pass it as a zero terminated string instead to be safe.

void ssobjects::PacketBuffer::operator<< ( unsigned8 Number )
 

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.

Exceptions:
PacketBufferException   If there is not enough room left in the buffer.

void ssobjects::PacketBuffer::operator<< ( unsigned16 Number )
 

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.

Exceptions:
PacketBufferException   If there is not enough room left in the buffer.

void ssobjects::PacketBuffer::operator<< ( signed16 Number )
 

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.

Exceptions:
PacketBufferException   If there is not enough room left in the buffer.

void ssobjects::PacketBuffer::operator<< ( unsigned32 Number )
 

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.

Exceptions:
PacketBufferException   If there is not enough room left in the buffer.

void ssobjects::PacketBuffer::operator<< ( signed32 iNumber )
 

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.

Exceptions:
PacketBufferException   If there is not enough room left in the buffer.

void ssobjects::PacketBuffer::operator<< ( CStr & String )
 

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.

Exceptions:
PacketBufferException   If there is not enough room left in the buffer.

void ssobjects::PacketBuffer::operator<< ( char * pString )
 

Add a zero terminated string.

Copies the zero terminated string pointed to by pString

Exceptions:
PacketBufferException   If there is not enough room left in the buffer.

void ssobjects::PacketBuffer::operator<< ( const char * pString )
 

Add a zero terminated string.

Copies the zero terminated string pointed to by pString

Exceptions:
PacketBufferException   If there is not enough room left in the buffer.

PacketBuffer & ssobjects::PacketBuffer::operator= ( const PacketBuffer & packet )
 

assignment operator.

void ssobjects::PacketBuffer::operator>> ( float & Number )
 

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.

Note:
This has not been tested on big-indian machines. You may want to convert the float number to a string, and pass it as a zero terminated string instead to be safe.

void ssobjects::PacketBuffer::operator>> ( double & Number )
 

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.

Note:
This has not been tested on big-indian machines. You may want to convert the double number to a string, and pass it as a zero terminated string instead to be safe.

void ssobjects::PacketBuffer::operator>> ( unsigned8 & Number )
 

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.

void ssobjects::PacketBuffer::operator>> ( unsigned16 & Number )
 

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.

void ssobjects::PacketBuffer::operator>> ( signed16 & Number )
 

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.

void ssobjects::PacketBuffer::operator>> ( unsigned32 & Number )
 

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.

void ssobjects::PacketBuffer::operator>> ( signed32 & Number )
 

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.

void ssobjects::PacketBuffer::operator>> ( CStr & String )
 

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.

void ssobjects::PacketBuffer::operator>> ( char * pString )
 

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.

void ssobjects::PacketBuffer::process ( ) [inline, protected, virtual]
 

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.

void ssobjects::PacketBuffer::receive ( BufferedSocket * psocket ) [protected, virtual]
 

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.

Exceptions:
PacketBufferException   If the socket is invalid.

Reimplemented in ssobjects::NetFile.

void ssobjects::PacketBuffer::reset ( )
 

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.

unsigned8 * ssobjects::PacketBuffer::resizeBuffer ( unsigned32 nNewSize ) [virtual]
 

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.

Exceptions:
PacketBufferException   if there was no memory to allocate new packet. If this happens, the old memory is left untouched, and is still valid.

Returns:
Pointer to the new memory.

void ssobjects::PacketBuffer::rewind ( ) [inline]
 

Puts the index pointer to the beginning of the buffer.

void ssobjects::PacketBuffer::transmit ( BufferedSocket * psocket ) [protected, virtual]
 

Called to send itself.

Sends the packet over the socket psocket

Exceptions:
PacketBufferException   if the socket is not valid.

Reimplemented in ssobjects::NetFile.


Member Data Documentation

BufferedSocket [protected]
 

Friend of BufferedSocket class.

ClientConnector [protected]
 

Friend of ClientConnector class.

unsigned8 * ssobjects::PacketBuffer::m_Buffer [protected]
 

Buffer that holds the data holds data that will be sent.

bool ssobjects::PacketBuffer::m_bUsed [protected]
 

Is this packet in use?

unsigned32 ssobjects::PacketBuffer::m_nBufferSizeMax [protected]
 

How big the buffer can get.

unsigned32 ssobjects::PacketBuffer::m_nID [protected]
 

Unique ID.

unsigned32 ssobjects::PacketBuffer::m_nUniqueID = 1 [static, protected]
 

Unique ID to keep all packets unique.

unsigned8 * ssobjects::PacketBuffer::m_pPointer [protected]
 

Current position in the buffer.

PacketBufferHeader ssobjects::PacketBuffer::m_packetHeader
 

Contains information about this packet.


The documentation for this class was generated from the following files:
Generated at Tue Sep 25 00:26:37 2001 for SimpleServerObjects by doxygen1.2.7 written by Dimitri van Heesch, © 1997-2001