Kea  2.1.6-git
isc::util::OutputBuffer Class Reference

The OutputBuffer class is a buffer abstraction for manipulating mutable data. More...

#include <buffer.h>

Public Member Functions

OutputBufferoperator= (const OutputBuffer &other)
 Assignment operator. More...
 
Constructors and Destructor
 OutputBuffer (size_t len)
 Constructor from the initial size of the buffer. More...
 
 OutputBuffer (const OutputBuffer &other)
 Copy constructor. More...
 
 ~OutputBuffer ()
 Destructor. More...
 
Getter Methods
size_t getCapacity () const
 Return the current capacity of the buffer. More...
 
const void * getData () const
 Return a pointer to the head of the data stored in the buffer. More...
 
size_t getLength () const
 Return the length of data written in the buffer. More...
 
uint8_t operator[] (size_t pos) const
 Return the value of the buffer at the specified position. More...
 
Methods for writing data into the buffer.
void skip (size_t len)
 Insert a specified length of gap at the end of the buffer. More...
 
void trim (size_t len)
 Trim the specified length of data from the end of the buffer. More...
 
void clear ()
 Clear buffer content. More...
 
void wipe ()
 Wipe buffer content. More...
 
void writeUint8 (uint8_t data)
 Write an unsigned 8-bit integer into the buffer. More...
 
void writeUint8At (uint8_t data, size_t pos)
 Write an unsigned 8-bit integer into the buffer. More...
 
void writeUint16 (uint16_t data)
 Write an unsigned 16-bit integer in host byte order into the buffer in network byte order. More...
 
void writeUint16At (uint16_t data, size_t pos)
 Write an unsigned 16-bit integer in host byte order at the specified position of the buffer in network byte order. More...
 
void writeUint32 (uint32_t data)
 Write an unsigned 32-bit integer in host byte order into the buffer in network byte order. More...
 
void writeUint64 (uint64_t data)
 Write an unsigned 64-bit integer in host byte order into the buffer in network byte order. More...
 
void writeData (const void *data, size_t len)
 Copy an arbitrary length of data into the buffer. More...
 

Detailed Description

The OutputBuffer class is a buffer abstraction for manipulating mutable data.

The main purpose of this class is to provide a safe workplace for constructing wire-format data to be sent out to a network. Here, safe means that it automatically allocates necessary memory and avoid buffer overrun.

Like for the InputBuffer class, applications normally use this class only in a limited situation. One common usage of this class for an application would be something like this:

OutputBuffer buffer(4096); // give a sufficiently large initial size
// pass the buffer to a DNS message object to construct a wire-format
// DNS message.
struct sockaddr to;
sendto(s, buffer.getData(), buffer.getLength(), 0, &to, sizeof(to));

where the getData() method gives a reference to the internal memory region stored in the buffer object. This is a suboptimal design in that it exposes an encapsulated "handle" of an object to its user. Unfortunately, there is no easy way to avoid this without involving expensive data copy if we want to use this object with a legacy API such as a BSD socket interface. And, indeed, this is one major purpose for this object. Applications should use this method only under such a special circumstance. It should also be noted that the memory region returned by getData() may be invalidated after a subsequent write operation.

An OutputBuffer class object automatically extends its memory region when data is written beyond the end of the current buffer. However, it will involve performance overhead such as reallocating more memory and copying data. It is therefore recommended to construct the buffer object with a sufficiently large initial size. The getCapacity() method provides the current maximum size of data (including the portion already written) that can be written into the buffer without causing memory reallocation.

Methods for writing data into the buffer generally work like an output stream: it begins with the head of the buffer, and once some length of data is written into the buffer, the next write operation will take place from the end of the buffer. Other methods to emulate "random access" are also provided (e.g., writeUint16At()). The normal write operations are normally exception-free as this class automatically extends the buffer when necessary. However, in extreme cases such as an attempt of writing multi-GB data, a separate exception (e.g., std::bad_alloc) may be thrown by the system. This also applies to the constructor with a very large initial size.

Note to developers: it may make more sense to introduce an abstract base class for the OutputBuffer and define the simple implementation as a a concrete derived class. That way we can provide flexibility for future extension such as more efficient buffer implementation or allowing users to have their own customized version without modifying the source code. We in fact considered that option, but at the moment chose the simpler approach with a single concrete class because it may make the implementation unnecessarily complicated while we were still not certain if we really want that flexibility. We may revisit the class design as we see more applications of the class. The same considerations apply to the InputBuffer and MessageRenderer classes.

Definition at line 294 of file buffer.h.

Constructor & Destructor Documentation

◆ OutputBuffer() [1/2]

isc::util::OutputBuffer::OutputBuffer ( size_t  len)
inline

Constructor from the initial size of the buffer.

Parameters
lenThe initial length of the buffer in bytes.

Definition at line 303 of file buffer.h.

◆ OutputBuffer() [2/2]

isc::util::OutputBuffer::OutputBuffer ( const OutputBuffer other)
inline

Copy constructor.

Parameters
otherSource object from which to make a copy.
Note
It is assumed that the source object is consistent, i.e. size_ <= allocated_, and that if allocated_ is greater than zero, buffer_ points to valid memory.

Definition at line 325 of file buffer.h.

◆ ~OutputBuffer()

isc::util::OutputBuffer::~OutputBuffer ( )
inline

Destructor.

Definition at line 340 of file buffer.h.

Member Function Documentation

◆ clear()

void isc::util::OutputBuffer::clear ( )
inline

Clear buffer content.

This method can be used to re-initialize and reuse the buffer without constructing a new one. Note it must keep current content.

Definition at line 451 of file buffer.h.

Referenced by isc::dns::AbstractMessageRenderer::clear(), isc::dhcp::Dhcp6to4Ipc::handler(), isc::perfdhcp::PktTransform::pack(), isc::dhcp::Pkt4::pack(), isc::dhcp::Pkt6::packUDP(), and isc::util::io::SocketSessionForwarder::push().

◆ getCapacity()

size_t isc::util::OutputBuffer::getCapacity ( ) const
inline

Return the current capacity of the buffer.

Definition at line 393 of file buffer.h.

Referenced by isc::dhcp::Option6Auth::pack(), and isc::dhcp::Option6Auth::packHashInput().

◆ getData()

const void* isc::util::OutputBuffer::getData ( ) const
inline

◆ getLength()

◆ operator=()

OutputBuffer& isc::util::OutputBuffer::operator= ( const OutputBuffer other)
inline

Assignment operator.

Parameters
otherObject to copy into "this".
Note
It is assumed that the source object is consistent, i.e. size_ <= allocated_, and that if allocated_ is greater than zero, buffer_ points to valid memory.

Definition at line 352 of file buffer.h.

◆ operator[]()

uint8_t isc::util::OutputBuffer::operator[] ( size_t  pos) const
inline

Return the value of the buffer at the specified position.

pos must specify the valid position of the buffer; otherwise an exception class of InvalidBufferPosition will be thrown.

Parameters
posThe position in the buffer to be returned.

Definition at line 410 of file buffer.h.

References isc_throw.

◆ skip()

void isc::util::OutputBuffer::skip ( size_t  len)
inline

Insert a specified length of gap at the end of the buffer.

The caller should not assume any particular value to be inserted. This method is provided as a shortcut to make a hole in the buffer that is to be filled in later, e.g, by writeUint16At().

Parameters
lenThe length of the gap to be inserted in bytes.

Definition at line 429 of file buffer.h.

Referenced by isc::dns::AbstractMessageRenderer::getLength(), and isc::util::io::SocketSessionForwarder::push().

◆ trim()

void isc::util::OutputBuffer::trim ( size_t  len)
inline

Trim the specified length of data from the end of the buffer.

The specified length must not exceed the current data size of the buffer; otherwise an exception of class isc::OutOfRange will be thrown.

Parameters
lenThe length of data that should be trimmed.

Definition at line 441 of file buffer.h.

References isc_throw.

Referenced by isc::dns::AbstractMessageRenderer::trim().

◆ wipe()

void isc::util::OutputBuffer::wipe ( )
inline

Wipe buffer content.

This method is the destructive alternative to clear().

Definition at line 456 of file buffer.h.

◆ writeData()

◆ writeUint16()

◆ writeUint16At()

void isc::util::OutputBuffer::writeUint16At ( uint16_t  data,
size_t  pos 
)
inline

Write an unsigned 16-bit integer in host byte order at the specified position of the buffer in network byte order.

The buffer must have a sufficient room to store the given data at the given position, that is, pos + 2 < getLength(); otherwise an exception of class isc::dns::InvalidBufferPosition will be thrown. Note also that this method never extends the buffer.

Parameters
dataThe 16-bit integer to be written into the buffer.
posThe beginning position in the buffer to write the data.

Definition at line 507 of file buffer.h.

References isc_throw.

Referenced by isc::util::io::SocketSessionForwarder::push(), isc::dhcp::writeIpUdpHeader(), and isc::dns::AbstractMessageRenderer::writeUint16At().

◆ writeUint32()

◆ writeUint64()

void isc::util::OutputBuffer::writeUint64 ( uint64_t  data)
inline

Write an unsigned 64-bit integer in host byte order into the buffer in network byte order.

Parameters
dataThe 64-bit integer to be written into the buffer.

Definition at line 532 of file buffer.h.

Referenced by isc::dhcp::Option6Auth::pack(), and isc::dhcp::Option6Auth::packHashInput().

◆ writeUint8()

◆ writeUint8At()

void isc::util::OutputBuffer::writeUint8At ( uint8_t  data,
size_t  pos 
)
inline

Write an unsigned 8-bit integer into the buffer.

The position must be lower than the size of the buffer, otherwise an exception of class isc::dns::InvalidBufferPosition will be thrown.

Parameters
dataThe 8-bit integer to be written into the buffer.
posThe position in the buffer to write the data.

Definition at line 479 of file buffer.h.

References isc_throw.

Referenced by isc::perfdhcp::PktTransform::pack(), and isc::perfdhcp::PktTransform::unpack().


The documentation for this class was generated from the following file: