GNE::CustomPacket Class Reference
[Mid-Level API]

Many times, especially during connection, you want to send some more "free-form" data that may or may not all be related and will only be sent once. More...

#include <CustomPacket.h>

Inheritance diagram for GNE::CustomPacket:

Inheritance graph
[legend]
Collaboration diagram for GNE::CustomPacket:

Collaboration graph
[legend]
List of all members.

Public Types

typedef SmartPtr< CustomPacketsptr
typedef WeakPtr< CustomPacketwptr

Public Member Functions

 CustomPacket (const CustomPacket &o)
BuffergetBuffer ()
 Returns the Buffer for reading or writing.
void clear ()
 If you want to reuse a CustomPacket after using it for reading or writing, you should call clear which will reset this object as if it were newly constructed with the default constructor.
virtual int getSize () const
virtual void writePacket (Buffer &raw) const
 Writes a CustomPacket to the given Buffer.
virtual void readPacket (Buffer &raw)
 Reads a CustomPacket from the given Buffer.

Static Public Member Functions

static int getMaxUserDataSize ()
 Returns the capacity of Buffers created by CustomPacket made for writing.

Static Public Attributes

static const int ID = 1
 The ID for this type of packet.

Detailed Description

Many times, especially during connection, you want to send some more "free-form" data that may or may not all be related and will only be sent once.

It may not make sense to create a completely new packet type just to send a few things one time. This packet type will allow you to send whatever you want. Basically, CustomPacket is just a packet that contains a Buffer. You can use the Buffer to put in data and pull it out. Remember Buffer does endian and processor-type conversions for you.

The maximum amount of data that can be stored in the CustomPacket is defined by its Buffer's capacity, which at the current time is 3 bytes smaller than Buffer::RAW_PACKET_LEN.

See the documentation for Packet for more info on some of these functions.


Member Function Documentation

Buffer & GNE::CustomPacket::getBuffer (  ) 

Returns the Buffer for reading or writing.

You should not keep the returned reference longer than the packet's destruction, or a reset or readPacket call.

When writing data to the buffer, do not call "flip" after writing data. The position should be left at the point after your last write call.

After a CustomPacket has been read, and you are pulling data out of it, the position will be the number of bytes stored in this CustomPacket. You need to call flip on the buffer before pulling data from it, or a buffer overflow exception will result.

You can use the return from this method directly to modify the buffer, or you can store the returned reference (for a very short time) and work on that. Here are two examples:

 CustomPacket cp;
 cp.getBuffer() << 15 << "whatever";
 //or
 Buffer& buf( cp.getBuffer() );
 buf << 15 << "whatever";

int GNE::CustomPacket::getSize (  )  const [virtual]

See also:
Packet::getSize()

Reimplemented from GNE::Packet.

void GNE::CustomPacket::readPacket ( Buffer raw  )  [virtual]

Reads a CustomPacket from the given Buffer.

The data in this packet will be erased and replaced by the new data. Then flip is called on the Buffer, so position is 0 and the bytes this CustomPacket contains is the Buffer's limit.

Reimplemented from GNE::Packet.

void GNE::CustomPacket::writePacket ( Buffer raw  )  const [virtual]

Writes a CustomPacket to the given Buffer.

Flip will be called on the Buffer then it will be written to the passed Buffer, so the final result will be an unchanged position, but limit == position.

Reimplemented from GNE::Packet.


The documentation for this class was generated from the following files:
Generated on Tue Sep 5 23:47:47 2006 for GNE by  doxygen 1.4.7