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

The base packet class, used for dealing with all types of packets at a fundamental level. More...

#include <Packet.h>

Inheritance diagram for GNE::Packet:

Inheritance graph
[legend]
List of all members.

Public Types

typedef SmartPtr< Packetsptr
typedef WeakPtr< Packetwptr

Public Member Functions

PacketmakeClone () const
 Returns a newly allocated exact copy of this packet, using the PacketParser::clonePacket function.
int getType () const
 Returns the type of this instance.
virtual int getSize () const
 Returns the current size of this packet in bytes.
virtual void writePacket (Buffer &raw) const
 Writes the packet to the given Buffer.
virtual void readPacket (Buffer &raw)
 Reads this packet from the given Buffer.

Protected Member Functions

 Packet (int id)
 Constructs a packet with the given ID.
 Packet (const Packet &o)
 Copy constructor.
Packetoperator= (const Packet &rhs)
 Copy operator you can use to help you in creating your own.

Detailed Description

The base packet class, used for dealing with all types of packets at a fundamental level.

All types of packets must inherit from this class to be recognized by GNE. GNE already implements some types of packets.

When you create a new packet you MUST define your own versions of all virtual functions or the program will fail.

See the example expacket on how to properly derive from a Packet class, or look at the code for the other GNE packets.


Constructor & Destructor Documentation

GNE::Packet::Packet ( int  id  )  [explicit, protected]

Constructs a packet with the given ID.

If you pass no ID, the ID for an empty packet is assumed. Normally sending blank packets are not useful, however. If you are wanting to use a blank packet for some purpose such as a end-of-data marker or for sending some message that requires no data, it is suggested that you simply derive a class from Packet that adds no data, but has a unique ID so it can be "recognized" easier.

Parameters:
id a number from PacketParser::MIN_USER_ID to PacketParser::MAX_USER_ID, inclusive. GNE packet id's are less than MIN_USER_ID.

GNE::Packet::Packet ( const Packet o  )  [protected]

Copy constructor.

If your Packet is using the default packet clone function in registration, remember that it uses the copy constructor so if you need to override the default implementation, you must do it.


Member Function Documentation

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

Returns the current size of this packet in bytes.

When overloading this function, call getSize on the parent class then add the sizes of your additional variables. If the size cannot be determined, then getSize should return a value <= Buffer::RAW_PACKET_LEN but >= its possible size -- so in other words if the size cannot be determined, it should return the largest possible size that given packet could be. This is discouraged as much as possible since GNE allocates packets in the data stream based on this value, and large values will hinder performance.

Reimplemented in GNE::ChannelPacket, GNE::CustomPacket, GNE::ExitPacket, GNE::ObjectBrokerPacket, GNE::PingPacket, GNE::RateAdjustPacket, and GNE::WrapperPacket.

int GNE::Packet::getType (  )  const

Returns the type of this instance.

This allows you to identify the type of packet you have when you only have a Packet*.

Packet & GNE::Packet::operator= ( const Packet rhs  )  [protected]

Copy operator you can use to help you in creating your own.

There is a debug assert in this function that checks to make sure the types match. Call this operator first from your copy operator. Many GNE packets may not support this operation, so check the documentation first -- if no operator = exists, then assume you cannot copy packets this way, unless the documentation says otherwise.

If you can't use operator= on a packet, you can use makeClone to achieve a nearly equivalent result.

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

Reads this packet from the given Buffer.

When overloading this function, call readPacket on the parent class then read your own variables.

This method is allowed to throw any subclass of Error. This can happen if accessing the Packet causes a buffer underflow, but an error might occur in the format or consistency of the data for the user's derived class. If you cannot construct a proper Packet of your type from the data in raw, then you should throw a subclass of Error.

Reimplemented in GNE::ChannelPacket, GNE::CustomPacket, GNE::ExitPacket, GNE::ObjectBrokerPacket, GNE::PingPacket, GNE::RateAdjustPacket, and GNE::WrapperPacket.

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

Writes the packet to the given Buffer.

You can continue writing more packets to the Buffer after this method. You must make sure there is enough space in the Buffer to fit this new packet. When overloading this function, call writePacket on the parent class then write your own variables.

No more than getSize() bytes may be written to the Buffer.

This method is allowed to throw any subclass of Error. Typically this only happens if accesses to Packet case a buffer overflow.

Reimplemented in GNE::ChannelPacket, GNE::CustomPacket, GNE::ExitPacket, GNE::ObjectBrokerPacket, GNE::PingPacket, GNE::RateAdjustPacket, and GNE::WrapperPacket.


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