Protocol Packet Design Pattern

来源:百度文库 编辑:神马文学网 时间:2024/04/29 18:08:16
Protocol Packet Documentation
Protocol Packet Source Code
Intent
Simplify buffer management in protocol stacks by supporting a single bufferthat 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 layerof the protocol adds its own headers and trailers. Due to these headers andtrailers, the size of the buffer containing the message keeps changing. In mostimplementations this results in each layer allocating a new buffer to adjust tothe changed buffer size. The Protocol Packet pattern addresses this issue with asimple and efficient buffering architecture.
Applicability
This design pattern can be applied in any protocol scenario involvingmultiple layers.
Structure
This pattern is implemented by just one class, the Protocol Packet. Thisclass works on a raw buffer that is capable of holding the entire packet withprotocol headers added for all the layers in the protocol stack. The raw bufferis dynamically partitioned into three regions:
Header
Body
Trailer
As the message moves from one layer to the the other the location of thedifferent 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 anapplication 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 receivedpacket:
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 classhas three internal constituent regions. These regions are defined by the Regionprivate structure.
Collaboration
The Protocol Packet class contains the header, body and trailer regions. Thisrelationship is shown in the following collaboration graph:

Consequences
Using this pattern provides allows efficient handling of packets as differentlayers are added or extracted. A single buffer is used across layers. Thisreduces the overhead in buffer processing. In addition, this pattern bringsuniformity 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
00033 { 00036 enum { MAXIMUM_PACKET_LENGTH = 1500}; 00037 00040 {00043 intoffset; 0004400046 intlength; 00047 }; 0004800051Regionm_header; 0005200055Regionm_body; 0005600059Regionm_trailer; 0006000063 charm_buffer[MAXIMUM_PACKET_LENGTH]; 00064 public: 00065 0009100092Protocol_Packet(int body_Length, int body_Offset) 00093 { 00094m_header.length = 0; 00095m_trailer.length = 0; 00096m_body.offset = body_Offset; 00097m_body.length = body_Length; 00098 } 00099 0010900110 voidAdd_Header(int length) 00111 { 00112 // Consider the header of the higher layer to be a part of 00113 // the body for the this layer. 00114m_body.offset -=m_header.length; 00115m_body.length +=m_header.length; 00116 00117 // Save the length and header offset of the new layer. Addition 00118 // of the header would move up the header offset. 00119m_header.length = length; 00120m_header.offset =m_body.offset - length; 00121 } 00122 0013100132 voidAdd_Trailer(int length) 00133 { 00134 // Consider the trailer of the higher layer to be a part of 00135 // the body of this layer. 00136m_body.length +=m_trailer.length; 00137 00138 // Now add the trailer at the end of the updated body. 00139m_trailer.length = length; 00140m_trailer.offset =m_body.offset +m_body.length; 00141 } 00142 0015300154 voidExtract_Header(int length) 00155 { 00156 // Update the new header. The header begins at current 00157 // body start offset. 00158m_header.offset =m_body.offset; 00159m_header.length = length; 00160 00161 // Reduce the body size to account for the removed header. 00162m_body.offset += length; 00163m_body.length -= length; 00164 } 00165 0017400175 voidExtract_Trailer(int length) 00176 { 00177 // Reduce the length to adjust for the extracted trailer. 00178m_body.length -= length; 00179 00180 // Setup the trailer to start at the end of the body. 00181m_trailer.offset =m_body.offset +m_body.length; 00182m_trailer.length = length; 00183 } 0018400186 char *Get_Header() 00187 { 00188 return (&m_buffer[m_header.offset]); 00189 } 0019000192 char *Get_Body() 00193 { 00194 return (&m_buffer[m_body.offset]); 00195 } 0019600198 char *Get_Trailer() 00199 { 00200 return (&m_buffer[m_trailer.offset]); 00201 } 0020200205 intGet_Length() 00206 { 00207 return (m_header.length +m_body.length +m_trailer.length); 00208 } 00209 };
Known Uses
Buffer management in protocol stack implementation.
Related Patterns
Transmit Protocol Handler Pattern
Receive Protocol Handler Pattern
Serial Port Design Pattern
High Speed Serial Port Design Pattern
Explore More...
Protocol Packet Documentation
Protocol Packet Source Code