-
Notifications
You must be signed in to change notification settings - Fork 26
Tip: Sending and Receiving a Packet
In KENS, there are sendPacket and packetArrived methods to send and receive packets. There is a packetArrived
method in TCPAssignment.cpp
.
void TCPAssignment::packetArrived(std::string fromModule, Packet &&packet) {
// Remove below
(void)fromModule;
(void)packet;
}
Every correct formed packet is transferred to this method. You can extract data from the received packet via Packet interfaces. For example, you can read its raw data from 0 to 16 by calling readData
method. Note that after handling the received packet, you may have to return a blocked system call as described in a previous section.
Sending a packet uses the sendPacket method. It receives a packet object to send and transfers it to the next module. In KENS, "IPv4"
is the only next module, so you can safely set the toModule parameter to "IPv4"
.
We strongly recommend you to use R-value for sending packets. This prevents reuse of sent packets and their UUIDs.
sendPacket("IPv4", packet); // OK, but not recommended
sendPacket("IPv4", std::move(packet)); // Recommended
You can create a new packet from scratch or by cloning an existing packet object. The cloning creates a new packet which has the same data with origin but not UUID. Note that copying (using = operator) just creates a new packet object which has the same data and UUID.
// Creating a new packet from scratch
size_t packet_size = 100;
Packet pkt (packet_size);
pkt.writeData(0, data, 20);
...
// Cloning an existing packet
Packet pkt2 = pkt.clone(); // cloning pkt. pkt2 has different UUID
Packet pkt3 = pkt; // copying pkt. pkt3 has same UUID