Protocols::BitTorrent::Packets::PacketBase Class Reference

#include <PacketBase.h> [code]

Inherits Protocols::Generics::PacketBase.

Inherited by 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::Piece, Protocols::BitTorrent::Packets::Port, Protocols::BitTorrent::Packets::RejectRequest, Protocols::BitTorrent::Packets::Request, Protocols::BitTorrent::Packets::SuggestPiece, and Protocols::BitTorrent::Packets::Unchoke.

Inheritance diagram for Protocols::BitTorrent::Packets::PacketBase:

Inheritance graph
[legend]
Collaboration diagram for Protocols::BitTorrent::Packets::PacketBase:

Collaboration graph
[legend]
List of all members.

Detailed Description

Base class of all BitTorrent packets.

This class takes reads and writes the header of a BitTorrent packet. The derived classes must implement reading and writing the payload.

The header of BitTorrent packets contains a 4 byte big-endian integer which says how many bytes follow it (rest of header plus payload). If all four bytes are null, than the packet is of type KeepalivePacket. Otherwise the header contains one further byte specifying the PacketType of the packet.

Check the derived classes for more info about the payload of the different PacketBase types.

See also:
Keepalive, Choke, Unchoke, Interested, NonInterested, Have, BitField, Request, Piece, Cancel

Definition at line 74 of file PacketBase.h.

Public Member Functions

virtual QString protocol () const
 Returns the string "BitTorrent".
virtual QString name () const =0
 Returns the name of the PacketBase type the dynamic object belongs to.
virtual auto_ptr< DataBasecopy () const =0
 Virtual copy constructor.
PacketType packetType () const
 Returns the PacketType of the PacketBase object.

Protected Member Functions

 PacketBase (Data *d, PacketType packetType)
 Constructs a PacketBase object.
virtual bool readHeader (const QByteArray &rawHeader)
 Reads the header fields from raw data.
virtual bool readPayload (const QByteArray &rawPayload)
 Reads the payload fields from raw bytes.
virtual QByteArray writeHeader () const
 Writes the PacketBase header and returns the raw bytes.
virtual QByteArray writePayload () const
 Returns the raw bytes representation of the payload fields.
virtual void readPayload (BinaryReader &)=0
 Reads the payload fields from binaryReader.
virtual void writePayload (BinaryWriter &) const =0

Private Member Functions

 Q_DECLARE_SHARED_DATA (Data)

Classes

class  Data


Constructor & Destructor Documentation

PacketBase::PacketBase Data dd,
PacketType  packetType
[protected]
 

Constructs a PacketBase object.

Parameters:
dd is the pointer to the derived class's private data struct, which must be derived from Data.
packetType is the PacketType corresponding to the derived class that calls this ctor. The corresponding header field is set.
Note:
Make sure packetType matches the packet type you are implementing in the derived class.

Definition at line 53 of file PacketBase.cpp.


Member Function Documentation

virtual auto_ptr<DataBase> Protocols::BitTorrent::Packets::PacketBase::copy  )  const [pure virtual]
 

Virtual copy constructor.

Implements Protocols::Generics::PacketBase.

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::Piece, Protocols::BitTorrent::Packets::Port, Protocols::BitTorrent::Packets::RejectRequest, Protocols::BitTorrent::Packets::Request, Protocols::BitTorrent::Packets::SuggestPiece, and Protocols::BitTorrent::Packets::Unchoke.

virtual QString Protocols::BitTorrent::Packets::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)?

Implements Protocols::Generics::PacketBase.

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::Piece, Protocols::BitTorrent::Packets::Port, Protocols::BitTorrent::Packets::RejectRequest, Protocols::BitTorrent::Packets::Request, Protocols::BitTorrent::Packets::SuggestPiece, and Protocols::BitTorrent::Packets::Unchoke.

PacketType Protocols::BitTorrent::Packets::PacketBase::packetType  )  const [inline]
 

Returns the PacketType of the PacketBase object.

Note:
This must match the dynamic type of the PacketBase object!

Definition at line 118 of file PacketBase.h.

QString PacketBase::protocol  )  const [virtual]
 

Returns the string "BitTorrent".

Implements Protocols::Generics::PacketBase.

Definition at line 28 of file PacketBase.cpp.

Protocols::BitTorrent::Packets::PacketBase::Q_DECLARE_SHARED_DATA Data   )  [private]
 

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

Reads the header fields from raw data.

Parameters:
rawHeader is the raw header buffer from which to parse the header fields.
Returns:
true if the format of the header was correct and the fields were parsed.

false otherwise.

The header of PacketBase of type KeepalivePacket would contain four zero bytes. For all other PacketBase types the header contains a 4 byte big-endian integer that contains the length of the payload + 1 and a single byte denoting the PacketType of this PacketBase object.

Note:
The header fields could have changed even if the function returns false.

Implements Protocols::Generics::PacketBase.

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

Definition at line 77 of file PacketBase.cpp.

void PacketBase::readPayload BinaryReader binaryReader  )  [protected, pure virtual]
 

Reads the payload fields from binaryReader.

Parameters:
binaryReader is the BinaryReader object from which to read the payload fields.
Note:
This function is called by the implementation of bool PacketBase::readPayload (const QByteArray &). You could chose to implement either of them.

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::Piece, Protocols::BitTorrent::Packets::Port, Protocols::BitTorrent::Packets::RejectRequest, Protocols::BitTorrent::Packets::Request, Protocols::BitTorrent::Packets::SuggestPiece, and Protocols::BitTorrent::Packets::Unchoke.

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

Reads the payload fields from raw bytes.

Parameters:
rawPayload are the raw payload bytes to parse.
Returns:
true if the payload could be correctly parsed and all read fields were set.

false otherwise.

A derived class can either choose to override this function or override PacketBase::readPayload (BinaryReader& ), which makes parsing using the BinaryReader object much easier.

Note:
Some or all payload fields may have changed even if parsing failed and the function returned false.

Implements Protocols::Generics::PacketBase.

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

Definition at line 129 of file PacketBase.cpp.

QByteArray PacketBase::writeHeader  )  const [protected, virtual]
 

Writes the PacketBase header and returns the raw bytes.

If the PacketBase is of type KeepalivePacket, then 4 zero bytes are returned. Otherwise the length of the payload + 1 will be written as 4 byte big-endian integer followed by a single byte denoting the PacketType.

Implements Protocols::Generics::PacketBase.

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

Definition at line 101 of file PacketBase.cpp.

virtual void Protocols::BitTorrent::Packets::PacketBase::writePayload BinaryWriter  )  const [protected, pure virtual]
 

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::Piece, Protocols::BitTorrent::Packets::Port, Protocols::BitTorrent::Packets::RejectRequest, Protocols::BitTorrent::Packets::Request, Protocols::BitTorrent::Packets::SuggestPiece, and Protocols::BitTorrent::Packets::Unchoke.

QByteArray PacketBase::writePayload  )  const [protected, virtual]
 

Returns the raw bytes representation of the payload fields.

Returns:
A byte array containing the raw bytes representation of the packet's payload.
Note:
A derived class can either choose to override this function or override PacketBase::writePayload (BinaryWriter &), which wakes writing fields into a QByteArray buffer much easier.

Implements Protocols::Generics::PacketBase.

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

Definition at line 158 of file PacketBase.cpp.


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