Kea 2.7.5
|
The OutputBuffer
class is a buffer abstraction for manipulating mutable data.
More...
#include <buffer.h>
Public Member Functions | |
OutputBuffer (const OutputBuffer &other) | |
Copy constructor. | |
OutputBuffer (size_t len) | |
Constructor. | |
~OutputBuffer ()=default | |
Destructor. | |
void | clear () |
Clear buffer content. | |
size_t | getCapacity () const |
Return the current capacity of the buffer. | |
const uint8_t * | getData () const |
Return a pointer to the head of the data stored in the buffer. | |
const void * | getDataAsVoidPtr () const |
Return data as a pointer to void. | |
size_t | getLength () const |
Return the length of data written in the buffer. | |
const std::vector< uint8_t > & | getVector () const |
Return the buffer. | |
OutputBuffer & | operator= (const OutputBuffer &other) |
Assignment operator. | |
uint8_t | operator[] (size_t position) const |
Return the value of the buffer at the specified position. | |
void | skip (size_t len) |
Insert a specified length of gap at the end of the buffer. | |
void | trim (size_t len) |
Trim the specified length of data from the end of the buffer. | |
void | writeData (const void *data, size_t len) |
Copy an arbitrary length of data into the buffer. | |
void | writeUint16 (uint16_t data) |
Write an unsigned 16-bit integer in host byte order into the buffer in network byte order. | |
void | writeUint16At (uint16_t data, size_t position) |
Write an unsigned 16-bit integer in host byte order at the specified position of the buffer in network byte order. | |
void | writeUint32 (uint32_t data) |
Write an unsigned 32-bit integer in host byte order into the buffer in network byte order. | |
void | writeUint64 (uint64_t data) |
Write an unsigned 64-bit integer in host byte order into the buffer in network byte order. | |
void | writeUint8 (uint8_t data) |
Write an unsigned 8-bit integer into the buffer. | |
void | writeUint8At (uint8_t data, size_t position) |
Write an unsigned 8-bit integer into the buffer. | |
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:
where the getData()
(in fact getDataAsVoidPtr()
) 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 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.
|
inlineexplicit |
|
inline |
|
default |
Destructor.
|
inline |
Clear buffer content.
Definition at line 466 of file buffer.h.
Referenced by isc::dns::AbstractMessageRenderer::clear(), isc::dhcp::Dhcp6to4Ipc::handler(), isc::dhcp::Pkt4::pack(), isc::dhcp::Pkt6::packUDP(), and isc::dhcp::Dhcp4o6IpcBase::send().
|
inline |
|
inline |
Return a pointer to the head of the data stored in the buffer.
The caller can assume that the subsequent getLength()
bytes are identical to the stored data of the buffer.
Note: The pointer returned by this method may be invalidated after a subsequent write operation.
Definition at line 395 of file buffer.h.
Referenced by getDataAsVoidPtr().
|
inline |
|
inline |
Return the length of data written in the buffer.
Definition at line 409 of file buffer.h.
Referenced by pkt4_send(), and isc::dns::AbstractMessageRenderer::setBuffer().
|
inline |
Return the buffer.
Definition at line 433 of file buffer.h.
Referenced by isc::dhcp::Pkt4o6::pack().
|
inline |
|
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 isc::OutOfRange
will be thrown.
position | The position in the buffer to be returned. |
Definition at line 420 of file buffer.h.
References isc_throw.
|
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().
len | The length of the gap to be inserted in bytes. |
|
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.
len | The length of data that should be trimmed. |
Definition at line 456 of file buffer.h.
References isc_throw.
|
inline |
Copy an arbitrary length of data into the buffer.
No conversion on the copied data is performed.
data | A pointer to the data to be copied into the buffer. |
len | The length of the data in bytes. |
Definition at line 556 of file buffer.h.
Referenced by isc::dhcp::Pkt4::pack(), isc::dhcp::Pkt6::packUDP(), and isc::dhcp::Pkt::repack().
|
inline |
Write an unsigned 16-bit integer in host byte order into the buffer in network byte order.
data | The 16-bit integer to be written into the buffer. |
Definition at line 498 of file buffer.h.
Referenced by isc::dhcp::Pkt4::pack(), and isc::dhcp::Pkt6::packUDP().
|
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::OutOfRange
will be thrown. Note also, that this method never extends the buffer.
data | The 16-bit integer to be written into the buffer. |
position | The beginning position in the buffer to write the data. |
Definition at line 514 of file buffer.h.
References isc_throw.
|
inline |
Write an unsigned 32-bit integer in host byte order into the buffer in network byte order.
data | The 32-bit integer to be written into the buffer. |
Definition at line 528 of file buffer.h.
Referenced by isc::dhcp::Pkt4::pack().
|
inline |
|
inline |
Write an unsigned 8-bit integer into the buffer.
data | The 8-bit integer to be written into the buffer. |
Definition at line 473 of file buffer.h.
Referenced by isc::dhcp::Pkt4::pack(), and isc::dhcp::Pkt6::packUDP().
|
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::OutOfRange
will be thrown.
data | The 8-bit integer to be written into the buffer. |
position | The position in the buffer to write the data. |
Definition at line 485 of file buffer.h.
References isc_throw.