#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:


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.
Definition at line 58 of file PacketBase.h.
Public Member Functions | |
| virtual | ~PacketBase () |
| Destructor. | |
| virtual auto_ptr< DataBase > | copy () 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 |
|
|
Destructor. Destroys a PacketBase object. Definition at line 59 of file PacketBase.cpp. |
|
|
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. |
|
|
||||||||||||
|
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!
Definition at line 180 of file PacketBase.cpp. |
|
|
Returns the length of the header in bytes.
Definition at line 145 of file PacketBase.h. |
|
|
Invalidate the header raw data. The raw header bytes will be rebuilt next time rawHeader() is called.
Reimplemented in Protocols::BitTorrent::Packets::BadPacket. Definition at line 69 of file PacketBase.cpp. |
|
|
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.
Reimplemented in Protocols::BitTorrent::Packets::BadPacket. Definition at line 83 of file PacketBase.cpp. |
|
|
|
Returns the length of the packet in bytes.
Definition at line 161 of file PacketBase.h. |
|
||||||||||||
|
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.
Definition at line 97 of file PacketBase.cpp. |
|
|
Returns the length of the payload in bytes.
Definition at line 153 of file PacketBase.h. |
|
|
Returns the name of the protocol this PacketBase belongs to.
Implemented in Protocols::BitTorrent::Packets::PacketBase, Protocols::Generics::BadPacket, Protocols::Generics::Testing::PacketStub, Protocols::Gnutella::Packets::PacketBase, and Protocols::Kad::Packets::Packet. |
|
|
|
|
|
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.
Definition at line 125 of file PacketBase.cpp. |
|
|
Returns rawHeader() + rawPayload().
Reimplemented in Protocols::Generics::Testing::PacketStub. Definition at line 137 of file PacketBase.h. |
|
|
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.
Definition at line 149 of file PacketBase.cpp. |
|
|
|
|
Returns the raw byte representation of the Packet object.
Implements Protocols::Generics::DataBase. Definition at line 165 of file PacketBase.h. |
|
|
|
|
Definition at line 129 of file PacketBase.h. |
|
|
Definition at line 130 of file PacketBase.h. |
|
|
Pointer to the implicitly shared private data.
Definition at line 124 of file PacketBase.h. |