Protocols::Generics::PacketBase Class Reference

#include <PacketBase.h> [code]

Inherits Protocols::Generics::DataBase.

Inherited by Protocols::BitTorrent::Packets::PacketBase, Protocols::Generics::BadPacket, Protocols::Generics::Testing::PacketStub, Protocols::Gnutella::Packets::PacketBase, and Protocols::Kad::Packets::Packet.

Inheritance diagram for Protocols::Generics::PacketBase:

Inheritance graph
[legend]
Collaboration diagram for Protocols::Generics::PacketBase:

Collaboration graph
[legend]
List of all members.

Detailed Description

An abstract base class representing a generic packet.

This class provides a common interface for Packets of different kinds belonging to different protocols and sets some infrastructure that enables adding new subtypes with less effort.

This class implements reference counting of the private data, which makes copying Packet objects very efficient. It also takes care of rebuilding the raw packet bytes only if and when that is required.

Derived classes must basically implement some pure virtual functions that are invoked by the base class when a packet is parsed or written as raw bytes. Each protocol would define its own base class derived from Packet and implement functionality common to all of its concrete packet subclasses.

Todo:
In the rare occasions when a packet would have a trailer, the trailer could be considered part of the payload. The PacketBase specialization of such protocol could even handle the trailer.
Todo:
Consider moving all packet-related stuff to the Packets sub-package.

Definition at line 58 of file PacketBase.h.

Public Member Functions

virtual ~PacketBase ()
 Destructor.
virtual auto_ptr< DataBasecopy () const =0
 Virtual copy constructor.
QByteArray toRawBytes () const
 Returns the raw byte representation of the Packet object.
virtual QString protocol () const =0
 Returns the name of the protocol this PacketBase belongs to.
virtual QString name () const =0
 Returns the name of the PacketBase type the dynamic object belongs to.
bool parse (const QByteArray &rawHeader, const QByteArray &rawPayload)
 Parses a PacketBase from raw data.
QByteArray rawHeader () const
 Returns the ready for transmission raw header bytes.
QByteArray rawPayload () const
 Returns the ready for transmission raw payload bytes.
QByteArray rawPacket () const
 Returns rawHeader() + rawPayload().
quint32 headerLength () const
 Returns the length of the header in bytes.
quint32 payloadLength () const
 Returns the length of the payload in bytes.
quint32 packetLength () const
 Returns the length of the packet in bytes.

Protected Member Functions

 PacketBase (Data *d)
 Constructs a PacketBase object.
virtual void invalidateHeader ()
 Invalidate the header raw data.
virtual void invalidatePayload ()
 Invalidate the payload raw data.
virtual bool readHeader (const QByteArray &rawHeader)=0
virtual bool readPayload (const QByteArray &rawPayload)=0
virtual QByteArray writeHeader () const =0
virtual QByteArray writePayload () const =0

Protected Attributes

SharedDataPointer d_ptr
 Pointer to the implicitly shared private data.

Private Member Functions

 Q_DECLARE_SHARED_DATA (Data)
bool doParse (const QByteArray &rawHeader, const QByteArray &rawPayload)
 Actually does the parsing of a PacketBase from raw data.

Friends

class PacketFactory
class Testing::PacketTest

Classes

class  Data


Constructor & Destructor Documentation

PacketBase::~PacketBase  )  [virtual]
 

Destructor.

Destroys a PacketBase object.

Definition at line 59 of file PacketBase.cpp.

PacketBase::PacketBase Data dd  )  [protected]
 

Constructs a PacketBase object.

Constructs a PacketBase object. dd is a pointer to an object of a class derived from Data. This object contains all private data of a dynamic object and would be implicitly shared between copies of this PacketBase object.

Definition at line 47 of file PacketBase.cpp.


Member Function Documentation

PacketBase * PacketBase::copy  )  const [pure virtual]
 

Virtual copy constructor.

Implements Protocols::Generics::DataBase.

Implemented in Protocols::BitTorrent::Packets::AllowedFast, Protocols::BitTorrent::Packets::BadPacket, Protocols::BitTorrent::Packets::BitField, Protocols::BitTorrent::Packets::Cancel, Protocols::BitTorrent::Packets::Choke, Protocols::BitTorrent::Packets::Have, Protocols::BitTorrent::Packets::HaveAll, Protocols::BitTorrent::Packets::HaveNone, Protocols::BitTorrent::Packets::Interested, Protocols::BitTorrent::Packets::Keepalive, Protocols::BitTorrent::Packets::NotInterested, Protocols::BitTorrent::Packets::PacketBase, Protocols::BitTorrent::Packets::Piece, Protocols::BitTorrent::Packets::Port, Protocols::BitTorrent::Packets::RejectRequest, Protocols::BitTorrent::Packets::Request, Protocols::BitTorrent::Packets::SuggestPiece, Protocols::BitTorrent::Packets::Unchoke, Protocols::Generics::BadPacket, Protocols::Generics::Testing::PacketStub, Protocols::Gnutella::Packets::PacketBase, Protocols::Gnutella::Packets::Ping, Protocols::Gnutella::Packets::Pong, Protocols::Gnutella::Packets::Push, Protocols::Gnutella::Packets::Query, Protocols::Gnutella::Packets::QueryHits, Protocols::Kad::Packets::Packet, Protocols::Kad::Packets::PeerInfoPacket< type >, and Protocols::Kad::Packets::TcpPortPacket< type >.

bool PacketBase::doParse const QByteArray &  rawHeader,
const QByteArray &  rawPayload
[private]
 

Actually does the parsing of a PacketBase from raw data.

The values of rawHeader and rawPayload are cached and returned by rawHeader() and rawPayload() respectively. Take a look at the documentation for these functions for more details on how the caching works.

The PacketBase is parsed in steps by calling readHeader() and readPayload() If any of the functions returns false, then doParse() will also return false.

You should discard the PacketBase derived object if doParse() returns false, as most probably some of the PacketBase fields are not set!

Note:
This function is supposed to be called only within parse(). If you need to call this function due to some reason, have in mind that the PacketBase object could be left in an invalid state (field values) if doParse() returned false.
See also:
rawHeader(), rawPayload(), parse()

Definition at line 180 of file PacketBase.cpp.

quint32 Protocols::Generics::PacketBase::headerLength  )  const [inline]
 

Returns the length of the header in bytes.

Note:
If the header has been invalidated, this function will cause it to be rebuilt.

Definition at line 145 of file PacketBase.h.

void PacketBase::invalidateHeader  )  [protected, virtual]
 

Invalidate the header raw data.

The raw header bytes will be rebuilt next time rawHeader() is called.

See also:
invalidatePayload()

Reimplemented in Protocols::BitTorrent::Packets::BadPacket.

Definition at line 69 of file PacketBase.cpp.

void PacketBase::invalidatePayload  )  [protected, virtual]
 

Invalidate the payload raw data.

The payload is invalidated, due to the fact that the payloadLength field may have changed, and the header is marked as invalid too. These will be rebuilt next time rawHeader() and rawPayload() are called.

See also:
invalidateHeader()

Reimplemented in Protocols::BitTorrent::Packets::BadPacket.

Definition at line 83 of file PacketBase.cpp.

QString PacketBase::name  )  const [pure virtual]
 

Returns the name of the PacketBase type the dynamic object belongs to.

Todo:
Better use PacketId (which possibly contains the ProtocolId)?

Implemented in Protocols::BitTorrent::Packets::AllowedFast, Protocols::BitTorrent::Packets::BadPacket, Protocols::BitTorrent::Packets::BitField, Protocols::BitTorrent::Packets::Cancel, Protocols::BitTorrent::Packets::Choke, Protocols::BitTorrent::Packets::Have, Protocols::BitTorrent::Packets::HaveAll, Protocols::BitTorrent::Packets::HaveNone, Protocols::BitTorrent::Packets::Interested, Protocols::BitTorrent::Packets::Keepalive, Protocols::BitTorrent::Packets::NotInterested, Protocols::BitTorrent::Packets::PacketBase, Protocols::BitTorrent::Packets::Piece, Protocols::BitTorrent::Packets::Port, Protocols::BitTorrent::Packets::RejectRequest, Protocols::BitTorrent::Packets::Request, Protocols::BitTorrent::Packets::SuggestPiece, Protocols::BitTorrent::Packets::Unchoke, Protocols::Generics::BadPacket, Protocols::Generics::Testing::PacketStub, Protocols::Gnutella::Packets::PacketBase, Protocols::Gnutella::Packets::Ping, Protocols::Gnutella::Packets::Pong, Protocols::Gnutella::Packets::Push, Protocols::Gnutella::Packets::Query, Protocols::Gnutella::Packets::QueryHits, Protocols::Kad::Packets::Packet, Protocols::Kad::Packets::PeerInfoPacket< type >, and Protocols::Kad::Packets::TcpPortPacket< type >.

quint32 Protocols::Generics::PacketBase::packetLength  )  const [inline]
 

Returns the length of the packet in bytes.

Note:
If the header or the payload has been invalidated, this function will cause the invalidated part to be rebuilt.

Definition at line 161 of file PacketBase.h.

bool PacketBase::parse const QByteArray &  rawHeader,
const QByteArray &  rawPayload
 

Parses a PacketBase from raw data.

Returns true if the PacketBase was parsed correctly, otherwise it returns false and the PacketBase object will be left unchanged.

See also:
doParse()

Definition at line 97 of file PacketBase.cpp.

quint32 Protocols::Generics::PacketBase::payloadLength  )  const [inline]
 

Returns the length of the payload in bytes.

Note:
If the payload has been invalidated, this function will cause it to be rebuilt.

Definition at line 153 of file PacketBase.h.

QString PacketBase::protocol  )  const [pure virtual]
 

Returns the name of the protocol this PacketBase belongs to.

Todo:
Better use ProtocolId?

Implemented in Protocols::BitTorrent::Packets::PacketBase, Protocols::Generics::BadPacket, Protocols::Generics::Testing::PacketStub, Protocols::Gnutella::Packets::PacketBase, and Protocols::Kad::Packets::Packet.

Protocols::Generics::PacketBase::Q_DECLARE_SHARED_DATA Data   )  [private]
 

QByteArray PacketBase::rawHeader  )  const
 

Returns the ready for transmission raw header bytes.

The function returns a cached QByteArray containing the raw header bytes, unless the header was marked as modified by calling invalidateHeader(). In this case, the header is rebuilt by calling writeHeader() and cached for future use.

Caching of the PacketBase's raw data improves performance especially when the same PacketBase is sent to more than one peer. In this case, due to reference counting, the raw data is generated just once and is also stored just once, although the packets are copied in multiple connections.

See also:
rawPayload()

Definition at line 125 of file PacketBase.cpp.

QByteArray Protocols::Generics::PacketBase::rawPacket  )  const [inline]
 

Returns rawHeader() + rawPayload().

Reimplemented in Protocols::Generics::Testing::PacketStub.

Definition at line 137 of file PacketBase.h.

QByteArray PacketBase::rawPayload  )  const
 

Returns the ready for transmission raw payload bytes.

The function returns a cached QByteArray containing the raw payload bytes, unless the payload was marked as modified by calling invalidatePayload(). In this case, the payload is rebuilt by calling writePayload() and cached for future use.

Caching of the PacketBase's raw data improves performance especially when the same PacketBase is sent to more than one peer. In this case, due to reference counting, the raw data is generated just once and is also stored just once, although the packets are copied in multiple connections.

See also:
rawHeader()

Definition at line 149 of file PacketBase.cpp.

virtual bool Protocols::Generics::PacketBase::readHeader const QByteArray &  rawHeader  )  [protected, pure virtual]
 

Implemented in Protocols::BitTorrent::Packets::BadPacket, Protocols::BitTorrent::Packets::PacketBase, Protocols::Generics::BadPacket, Protocols::Generics::Testing::PacketStub, Protocols::Gnutella::Packets::PacketBase, and Protocols::Kad::Packets::Packet.

virtual bool Protocols::Generics::PacketBase::readPayload const QByteArray &  rawPayload  )  [protected, pure virtual]
 

Implemented in Protocols::BitTorrent::Packets::BadPacket, Protocols::BitTorrent::Packets::PacketBase, Protocols::Generics::BadPacket, Protocols::Generics::Testing::PacketStub, Protocols::Gnutella::Packets::PacketBase, and Protocols::Kad::Packets::Packet.

QByteArray Protocols::Generics::PacketBase::toRawBytes  )  const [inline, virtual]
 

Returns the raw byte representation of the Packet object.

Implements Protocols::Generics::DataBase.

Definition at line 165 of file PacketBase.h.

virtual QByteArray Protocols::Generics::PacketBase::writeHeader  )  const [protected, pure virtual]
 

Implemented in Protocols::BitTorrent::Packets::BadPacket, Protocols::BitTorrent::Packets::PacketBase, Protocols::Generics::BadPacket, Protocols::Generics::Testing::PacketStub, Protocols::Gnutella::Packets::PacketBase, and Protocols::Kad::Packets::Packet.

virtual QByteArray Protocols::Generics::PacketBase::writePayload  )  const [protected, pure virtual]
 

Implemented in Protocols::BitTorrent::Packets::BadPacket, Protocols::BitTorrent::Packets::PacketBase, Protocols::Generics::BadPacket, Protocols::Generics::Testing::PacketStub, Protocols::Gnutella::Packets::PacketBase, and Protocols::Kad::Packets::Packet.


Friends And Related Function Documentation

friend class PacketFactory [friend]
 

Definition at line 129 of file PacketBase.h.

friend class Testing::PacketTest [friend]
 

Definition at line 130 of file PacketBase.h.


Member Data Documentation

SharedDataPointer Protocols::Generics::PacketBase::d_ptr [protected]
 

Pointer to the implicitly shared private data.

Definition at line 124 of file PacketBase.h.


The documentation for this class was generated from the following files: