A subclass of cMessage that can be used to represent packets (frames, datagrams, application messages, etc). More...
#include <cmessage.h>
Public Member Functions | |
Constructors, destructor, assignment | |
cPacket (const cPacket &packet) | |
cPacket (const char *name=NULL, short kind=0, int64 bitLength=0) | |
virtual | ~cPacket () |
cPacket & | operator= (const cPacket &packet) |
Redefined cObject member functions. | |
virtual cPacket * | dup () const |
virtual std::string | info () const |
virtual std::string | detailedInfo () const |
virtual void | forEachChild (cVisitor *v) |
virtual void | parsimPack (cCommBuffer *buffer) |
virtual void | parsimUnpack (cCommBuffer *buffer) |
virtual bool | isPacket () const |
Length and bit error flag | |
virtual void | setBitLength (int64 l) |
void | setByteLength (int64 l) |
virtual void | addBitLength (int64 delta) |
void | addByteLength (int64 delta) |
virtual int64 | getBitLength () const |
int64 | getByteLength () const |
virtual void | setBitError (bool e) |
virtual bool | hasBitError () const |
Message encapsulation. | |
virtual void | encapsulate (cPacket *packet) |
virtual cPacket * | decapsulate () |
virtual cPacket * | getEncapsulatedPacket () const |
_OPPDEPRECATED cPacket * | getEncapsulatedMsg () const |
virtual bool | hasEncapsulatedPacket () const |
Transmission state | |
simtime_t_cref | getDuration () const |
bool | isReceptionStart () const |
A subclass of cMessage that can be used to represent packets (frames, datagrams, application messages, etc).
cPacket adds length (measured in bits or bytes), bit error flag, and encapsulation capability to cMessage. Length and bit error flag are significant when the packet travels through a cDatarateChannel or another channel that supports data rate and/or error modelling.
cPacket is rarely used "as is". Typically the user wants to subclass it to create specific packet types for various protocols being modelled. The most convenient way to do that are via message declaration files (.msg), which are translated into C++ classes. An example message declaration:
packet Datagram { int destAddr = -1; // destination address int srcAddr = -1; // source address int ttl = 32; // time to live }
cPacket::cPacket | ( | const char * | name = NULL , |
|
short | kind = 0 , |
|||
int64 | bitLength = 0 | |||
) | [explicit] |
Constructor.
It takes the packet name, message kind value, and the packet length in bits; all optional.
virtual void cPacket::addBitLength | ( | int64 | delta | ) | [virtual] |
Changes packet length by the given value (bits).
This is useful for modeling encapsulation/decapsulation. (See also encapsulate() and decapsulate().) The caller must take care that the result does not overflow (i.e. fits into an int64).
The value may be negative (packet length may be decreased too). If the resulting length would be negative, the method throws a cRuntimeError.
void cPacket::addByteLength | ( | int64 | delta | ) | [inline] |
Changes packet length by the given value (bytes).
This is just a convenience function which invokes addBitLength() with 8*l as argument. The caller must take care that the result does not overflow (i.e. fits into an int64).
virtual cPacket* cPacket::decapsulate | ( | ) | [virtual] |
Decapsulates a packet from the packet object.
The length of this packet will be decreased by the length of the encapsulated packet, except if it was zero. If the length would become negative, cRuntimeError is thrown. If there is no encapsulated packet, the method returns NULL.
virtual std::string cPacket::detailedInfo | ( | ) | const [virtual] |
virtual cPacket* cPacket::dup | ( | ) | const [inline, virtual] |
virtual void cPacket::encapsulate | ( | cPacket * | packet | ) | [virtual] |
Encapsulates packet in the packet.
The packet length gets increased by the length of the encapsulated packet.
IMPORTANT NOTE: IT IS FORBIDDEN TO KEEP A POINTER TO A MESSAGE AFTER IT WAS ENCAPSULATED. For performance reasons, encapsulated packets are reference counted, meaning that the encapsulated packet is not duplicated when you duplicate a packet, but rather, both (all) copies share the same packet instance. Any change done to the encapsulated packet would affect other packets as well. Decapsulation (and even calling getEncapsulatedPacket()) will create an own (non-shared) copy of the packet.
virtual void cPacket::forEachChild | ( | cVisitor * | v | ) | [virtual] |
int64 cPacket::getByteLength | ( | ) | const [inline] |
Returns the packet length in bytes, that is, bitlength/8.
If bitlength is not a multiple of 8, the result is rounded up.
simtime_t_cref cPacket::getDuration | ( | ) | const [inline] |
Returns the transmission duration after the packet was sent through a channel with data rate.
_OPPDEPRECATED cPacket* cPacket::getEncapsulatedMsg | ( | ) | const [inline] |
DEPRECATED.
getEncapsulatedMsg() was renamed to getEncapsulatedPacket(), this method was left for backward compatibility.
virtual cPacket* cPacket::getEncapsulatedPacket | ( | ) | const [virtual] |
Returns a pointer to the encapsulated packet, or NULL if there is no encapsulated packet.
IMPORTANT: see notes at encapsulate() about reference counting of encapsulated packets.
virtual bool cPacket::hasEncapsulatedPacket | ( | ) | const [virtual] |
Returns true if the packet contains an encapsulated packet, and false otherwise.
This method is potentially more efficient than getEncapsulatedPacket()!=NULL
, because it does not need to unshare a shared encapsulated packet (see note at encapsulate()).
virtual std::string cPacket::info | ( | ) | const [virtual] |
bool cPacket::isReceptionStart | ( | ) | const [inline] |
Tells whether this packet represents the start or the end of the reception, provided the packet has nonzero length and it travelled through a channel with nonzero data rate.
This can be configured on the receiving gate (see cGate::setDeliverOnReceptionStart()).
Assignment operator.
Duplication and the assignment operator work all right with cPacket. The name member is not copied; see cNamedObject's operator=() for more details.
virtual void cPacket::parsimPack | ( | cCommBuffer * | buffer | ) | [virtual] |
virtual void cPacket::parsimUnpack | ( | cCommBuffer * | buffer | ) | [virtual] |
virtual void cPacket::setBitLength | ( | int64 | l | ) | [virtual] |
Sets packet length (in bits).
When the packet is sent through a channel, packet length affects the transmission duration and the probability of setting the bit error flag.
void cPacket::setByteLength | ( | int64 | l | ) | [inline] |
Sets packet length (bytes).
This is just a convenience function which invokes setBitLength() with 8*l as argument. The caller must take care that the result does not overflow (i.e. fits into an int64).