A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
main-packet-header.cc
Go to the documentation of this file.
1
#include "ns3/header.h"
2
#include "ns3/packet.h"
3
#include "ns3/ptr.h"
4
#include "ns3/simulator.h"
5
6
#include <iostream>
7
8
using namespace
ns3
;
9
14
class
MyHeader
:
public
Header
15
{
16
public
:
17
MyHeader
();
18
~
MyHeader
()
override
;
19
24
void
SetData(uint16_t
data
);
29
uint16_t GetData()
const
;
30
35
static
TypeId
GetTypeId();
36
TypeId
GetInstanceTypeId()
const override
;
37
void
Print
(std::ostream& os)
const override
;
38
void
Serialize(
Buffer::Iterator
start
)
const override
;
39
uint32_t Deserialize(
Buffer::Iterator
start
)
override
;
40
uint32_t GetSerializedSize()
const override
;
41
42
private
:
43
uint16_t
m_data
;
44
};
45
46
MyHeader::MyHeader
()
47
{
48
// we must provide a public default constructor,
49
// implicit or explicit, but never private.
50
}
51
52
MyHeader::~MyHeader
()
53
{
54
}
55
56
TypeId
57
MyHeader::GetTypeId
()
58
{
59
static
TypeId
tid =
TypeId
(
"ns3::MyHeader"
).
SetParent
<
Header
>().AddConstructor<MyHeader>();
60
return
tid;
61
}
62
63
TypeId
64
MyHeader::GetInstanceTypeId
()
const
65
{
66
return
GetTypeId();
67
}
68
69
void
70
MyHeader::Print
(std::ostream& os)
const
71
{
72
// This method is invoked by the packet printing
73
// routines to print the content of my header.
74
// os << "data=" << m_data << std::endl;
75
os <<
"data="
<< m_data;
76
}
77
78
uint32_t
79
MyHeader::GetSerializedSize
()
const
80
{
81
// we reserve 2 bytes for our header.
82
return
2;
83
}
84
85
void
86
MyHeader::Serialize
(
Buffer::Iterator
start
)
const
87
{
88
// we can serialize two bytes at the start of the buffer.
89
// we write them in network byte order.
90
start
.WriteHtonU16(m_data);
91
}
92
93
uint32_t
94
MyHeader::Deserialize
(
Buffer::Iterator
start
)
95
{
96
// we can deserialize two bytes from the start of the buffer.
97
// we read them in network byte order and store them
98
// in host byte order.
99
m_data =
start
.ReadNtohU16();
100
101
// we return the number of bytes effectively read.
102
return
2;
103
}
104
105
void
106
MyHeader::SetData
(uint16_t
data
)
107
{
108
m_data =
data
;
109
}
110
111
uint16_t
112
MyHeader::GetData
()
const
113
{
114
return
m_data;
115
}
116
117
int
118
main(
int
argc,
char
* argv[])
119
{
120
// Enable the packet printing through Packet::Print command.
121
Packet::EnablePrinting
();
122
123
// instantiate a header.
124
MyHeader
sourceHeader;
125
sourceHeader.
SetData
(2);
126
127
// instantiate a packet
128
Ptr<Packet>
p = Create<Packet>();
129
130
// and store my header into the packet.
131
p->
AddHeader
(sourceHeader);
132
133
// print the content of my packet on the standard output.
134
p->
Print
(std::cout);
135
std::cout << std::endl;
136
137
// you can now remove the header from the packet:
138
MyHeader
destinationHeader;
139
p->
RemoveHeader
(destinationHeader);
140
141
// and check that the destination and source
142
// headers contain the same values.
143
NS_ASSERT
(sourceHeader.
GetData
() == destinationHeader.
GetData
());
144
145
return
0;
146
}
MyHeader
A simple example of an Header implementation.
Definition:
main-packet-header.cc:15
MyHeader::m_data
uint16_t m_data
Header data.
Definition:
main-packet-header.cc:43
MyHeader::Serialize
void Serialize(Buffer::Iterator start) const override
Definition:
main-packet-header.cc:86
MyHeader::GetInstanceTypeId
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Definition:
main-packet-header.cc:64
MyHeader::MyHeader
MyHeader()
Definition:
main-packet-header.cc:46
MyHeader::GetData
uint16_t GetData() const
Get the header data.
Definition:
main-packet-header.cc:112
MyHeader::GetTypeId
static TypeId GetTypeId()
Get the type ID.
Definition:
main-packet-header.cc:57
MyHeader::GetSerializedSize
uint32_t GetSerializedSize() const override
Definition:
main-packet-header.cc:79
MyHeader::Print
void Print(std::ostream &os) const override
Definition:
main-packet-header.cc:70
MyHeader::SetData
void SetData(uint16_t data)
Set the header data.
Definition:
main-packet-header.cc:106
MyHeader::~MyHeader
~MyHeader() override
Definition:
main-packet-header.cc:52
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition:
buffer.h:100
ns3::Header
Protocol header serialization and deserialization.
Definition:
header.h:44
ns3::Header::Deserialize
virtual uint32_t Deserialize(Buffer::Iterator start)=0
Deserialize the object from a buffer iterator.
ns3::Packet::RemoveHeader
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition:
packet.cc:294
ns3::Packet::AddHeader
void AddHeader(const Header &header)
Add header to this packet.
Definition:
packet.cc:268
ns3::Packet::Print
void Print(std::ostream &os) const
Print the packet contents.
Definition:
packet.cc:456
ns3::Packet::EnablePrinting
static void EnablePrinting()
Enable printing packets metadata.
Definition:
packet.cc:596
ns3::Ptr< Packet >
ns3::TypeId
a unique identifier for an interface.
Definition:
type-id.h:60
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition:
type-id.cc:935
NS_ASSERT
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition:
assert.h:66
Print
void Print(ComponentCarrier cc)
Definition:
lena-cc-helper.cc:67
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
two-ray-to-three-gpp-ch-calibration.start
start
Definition:
two-ray-to-three-gpp-ch-calibration.py:474
data
uint8_t data[writeSize]
Definition:
socket-bound-tcp-static-routing.cc:52
src
network
examples
main-packet-header.cc
Generated on Fri Mar 31 2023 13:30:48 for ns-3 by
1.9.1