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

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:

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:

  1. 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.
  2. The packet is passed to Layer 3. This layer adds its own headers and trailers regions into the same buffer.
  3. Layer 2 adds its own headers and trailers regions. The previous header and trailer regions get merged into the body.
  4. Layer 1 adds headers and trailers. Again, the body region grows to accommodate the headers and trailers for Layer 2.
  5. 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:

  1. 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.
  2. 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.
  3. Layer 2 also extracts its own header and trailer regions. Again shrinking the body region.
  4. Layer 3 similarly extracts its header and trailer regions. Shrinking the body to the original application body.
  5. 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:

Protocol Packet 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:

Sample Code and Usage

The source code for the Protocol_Packet class is presented below:

Protocol Packet

Known Uses

Related Patterns