OMNeT++ Simulation Library  5.6.1
cpacket.h
1 //==========================================================================
2 // CPACKET.H - header for
3 // OMNeT++/OMNEST
4 // Discrete System Simulation in C++
5 //
6 //==========================================================================
7 
8 /*--------------------------------------------------------------*
9  Copyright (C) 1992-2017 Andras Varga
10  Copyright (C) 2006-2017 OpenSim Ltd.
11 
12  This file is distributed WITHOUT ANY WARRANTY. See the file
13  `license' for details on this and other legal matters.
14 *--------------------------------------------------------------*/
15 
16 #ifndef __OMNETPP_CPACKET_H
17 #define __OMNETPP_CPACKET_H
18 
19 #include "cmessage.h"
20 
21 namespace omnetpp {
22 
23 
52 class SIM_API cPacket : public cMessage
53 {
54  private:
55  enum {
56  FL_ISRECEPTIONSTART = 8,
57  FL_BITERROR = 16,
58  };
59 
60  int64_t bitLength; // length of the packet in bits -- used for bit error and transmission delay modeling
61  simtime_t duration; // transmission duration on last channel with datarate
62  cPacket *encapsulatedPacket; // ptr to the encapsulated message
63  unsigned short shareCount; // num of messages MINUS ONE that have this message encapsulated.
64  // 0: not shared (not encapsulated or encapsulated in one message);
65  // 1: shared once (shared among two messages);
66  // 2: shared twice (shared among three messages); etc.
67  // on reaching max sharecount a new packet gets created
68 
69  private:
70  void copy(const cPacket& packet);
71 
72  public:
73  // internal: sets the message duration; called by channel objects and sendDirect
74  void setDuration(simtime_t d) {duration = d;}
75 
76  // internal: sets the isReceptionStart() flag
77  void setReceptionStart(bool b) {setFlag(FL_ISRECEPTIONSTART, b);}
78 
79  // internal convenience method: returns the getId() of the innermost encapsulated message,
80  // or itself if there is no encapsulated message
81  long getEncapsulationId() const;
82 
83  // internal convenience method: returns getTreeId() of the innermost encapsulated message,
84  // or itself if there is no encapsulated message
85  long getEncapsulationTreeId() const;
86 
87  cPacket *_getEncapMsg() { return encapsulatedPacket; }
88 
89  // internal: if encapmsg is shared (sharecount>0), creates a private copy for this packet,
90  // and in any case it sets encapmsg's owner to be this object. This method
91  // has to be called before any operation on encapmsg, to prevent trouble
92  // that may arise from accessing shared message instances. E.g. without calling
93  // _detachEncapMsg(), encapmsg's ownerp is unpredictable (may be any previous owner,
94  // possibly not even existing any more) which makes even a call to its getFullPath()
95  // method dangerous.
96  void _detachEncapMsg();
97 
98  // internal: delete encapmsg, paying attention to its sharecount (assumes encapmsg!=nullptr)
99  void _deleteEncapMsg();
100 
101  // internal: only to be used by test cases
102  int getShareCount() const {return shareCount;}
103 
104  public:
110  cPacket(const cPacket& packet);
111 
116  explicit cPacket(const char *name=nullptr, short kind=0, int64_t bitLength=0);
117 
121  virtual ~cPacket();
122 
127  cPacket& operator=(const cPacket& packet);
129 
132 
137  virtual cPacket *dup() const override {return new cPacket(*this);}
138 
143  virtual std::string str() const override;
144 
149  virtual void forEachChild(cVisitor *v) override;
150 
156  virtual void parsimPack(cCommBuffer *buffer) const override;
157 
163  virtual void parsimUnpack(cCommBuffer *buffer) override;
164 
168  virtual bool isPacket() const override {return true;}
169 
174  virtual const char *getDisplayString() const override;
176 
184  virtual void setBitLength(int64_t l);
185 
191  void setByteLength(int64_t l) {setBitLength(l<<3);}
192 
203  virtual void addBitLength(int64_t delta);
204 
211  void addByteLength(int64_t delta) {addBitLength(delta<<3);}
212 
216  virtual int64_t getBitLength() const {return bitLength;}
217 
222  int64_t getByteLength() const {return (getBitLength()+7)>>3;}
223 
227  virtual void setBitError(bool e) {setFlag(FL_BITERROR,e);}
228 
232  virtual bool hasBitError() const {return flags&FL_BITERROR;}
234 
237 
251  virtual void encapsulate(cPacket *packet);
252 
260  virtual cPacket *decapsulate();
261 
269  virtual cPacket *getEncapsulatedPacket() const;
270 
277  virtual bool hasEncapsulatedPacket() const;
279 
288  simtime_t_cref getDuration() const {return duration;}
289 
298  bool isReceptionStart() const {return flags & FL_ISRECEPTIONSTART;}
300 };
301 
302 } // namespace omnetpp
303 
304 #endif
305 
306 
The message class in OMNeT++. cMessage objects may represent events, messages, jobs or other entities...
Definition: cmessage.h:95
int64_t getByteLength() const
Definition: cpacket.h:222
int64_t-based, base-10 fixed-point simulation time.
Definition: simtime.h:66
virtual int64_t getBitLength() const
Definition: cpacket.h:216
void setByteLength(int64_t l)
Definition: cpacket.h:191
bool isReceptionStart() const
Definition: cpacket.h:298
A subclass of cMessage to represent packets, frames, datagrams, application messages, and similar data.
Definition: cpacket.h:52
virtual bool isPacket() const override
Definition: cpacket.h:168
simtime_t_cref getDuration() const
Definition: cpacket.h:288
Buffer for the communications layer of parallel simulation.
Definition: ccommbuffer.h:41
virtual void setBitError(bool e)
Definition: cpacket.h:227
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition: cvisitor.h:56
Definition: cabstracthistogram.h:21
virtual cPacket * dup() const override
Definition: cpacket.h:137
virtual bool hasBitError() const
Definition: cpacket.h:232
void addByteLength(int64_t delta)
Definition: cpacket.h:211