Protocol Packet Design Pattern
Simplify buffer management in protocol stacks by supporting a single buffer that allows addition and extraction of different protocol layers.
Also Known As
- Protocol Buffer Design Pattern
- Multi-Layer Buffer
Motivation
A protocol stack generally handles multiple layers of a protocol. Each layer of the protocol adds its own headers and trailers. Due to these headers and trailers, the size of the buffer containing the message keeps changing. In most implementations this results in each layer allocating a new buffer to adjust to the changed buffer size. The Protocol Packet pattern addresses this issue with a simple and efficient buffering architecture.
Applicability
This design pattern can be applied in any protocol scenario involving multiple layers.
Structure
This pattern is implemented by just one class, the Protocol Packet. This class works on a raw buffer that is capable of holding the entire packet with protocol headers added for all the layers in the protocol stack. The raw buffer is dynamically partitioned into three regions:
- Header
- Body
- Trailer
As the message moves from one layer to the other the location of the different regions is adjusted. The region adjustments are described below:
Transmitting a Packet
Layer 1 Header Region | Transmitted Bytes Body Region | |||
Layer 2 Header Region | Layer 1 Body Region | |||
Layer 3 Header Region | Layer 2 Body Region | |||
Application Body Region | Layer 3 Body Region | |||
Layer 3 Trailer Region | ||||
Layer 2 Trailer Region | ||||
Layer 1 Trailer Region |
The figure above shows the adjustments to the region definition as an application packet moves through the different layers for transmission:
- The Protocol Packet object is constructed with just the application body. Notice that the body does not start from the first byte of the buffer. The application body is placed at an offset, leaving enough space for the protocol headers. At this point, the header and trailer regions are of zero size.
- The packet is passed to Layer 3. This layer adds its own headers and trailers regions into the same buffer.
- Layer 2 adds its own headers and trailers regions. The previous header and trailer regions get merged into the body.
- Layer 1 adds headers and trailers. Again, the body region grows to accommodate the headers and trailers for Layer 2.
- Finally, zero length header and trailers are added, resulting in the entire packet moving to the body region. At this point the header and trailer regions are of 0 length.
Receiving a Packet
Received Bytes Body Region | Layer 1 Header Region | |||
Layer 1 Body Region | Layer 2 Header Region | |||
Layer 2 Body Region | Layer 3 Header Region | |||
Layer 3 Body Region | Application Body Region | |||
Layer 3 Trailer Region | ||||
Layer 2 Trailer Region | ||||
Layer 1 Trailer Region |
The figure above shows how the region definition changes for a received packet:
- The received packet is created with all the bytes in the body region of the message. At this point, the header and trailer regions are of zero length.
- Layer 1 extracts its headers and trailer regions. The two regions have been carved out of the received body region. The size of the body region is reduced.
- Layer 2 also extracts its own header and trailer regions. Again shrinking the body region.
- Layer 3 similarly extracts its header and trailer regions. Shrinking the body to the original application body.
- The application extracts a zero length header and trailer. This leaves only the packet with only the body region.
Participants
This pattern is implemented by just one class, the Protocol Packet. The class has three internal constituent regions. These regions are defined by the Region private structure.
Collaboration
The Protocol Packet class contains the header, body and trailer regions. This relationship is shown in the following collaboration graph:
Consequences
Using this pattern provides allows efficient handling of packets as different layers are added or extracted. A single buffer is used across layers. This reduces the overhead in buffer processing. In addition, this pattern brings uniformity to the design of the protocol stack.
Implementation
The Protocol Packet class consists of the following methods:
- Add_Header: Add the header to the transmit packet.
- Add_Trailer: Add the trailer to the transmit packet.
- Extract_Header: Extract the header from the received packet.
- Extract_Trailer: Extract the trailer from the received packet.
- Get_Header: Get a pointer to the current packet header.
- Get_Body: Get a pointer to the current packet body.
- Get_Trailer: Get a pointer to the current packet trailer.
- Get_Length: Get the total length. The length includes the header, body and trailer.
Sample Code and Usage
The source code for the Protocol_Packet class is presented below:
Protocol Packet
Known Uses
- Buffer management in protocol stack implementation.