asio 0.3.8rc3 Home | Reference | Tutorial | Examples | Design
Design

Buffers

To allow the development of efficient network applications, the asio library provides support for scatter-gather operations. These operations involve one or more buffers (where each buffer is a contiguous region of memory):

Therefore we require an abstraction to represent a collection of buffers. The approach asio uses is to define a type (actually two types) to represent a single buffer. These can be stored in a container, which may be passed to the scatter-gather operations.

A buffer, as a contiguous region of memory, can be represented by an address and size in bytes. There is a distinction between modifiable memory (called mutable in asio) and non-modifiable memory (where the latter is created from the storage for a const-qualified variable). These two types could therefore be defined as follows:

typedef std::pair<void*, std::size_t> mutable_buffer;
typedef std::pair<const void*, std::size_t> const_buffer;

Here, a mutable_buffer would be convertible to a const_buffer, but conversion in the opposite direction is not valid.

However, the asio library does not use the above definitions, but instead defines two classes: asio::mutable_buffer and asio::const_buffer. The goal of these is to provide an opaque representation of contiguous memory, where:

Finally, multiple buffers can be passed to scatter-gather operations (such as asio::read or asio::write) by putting the buffer objects into a container. The Mutable_Buffers and Const_Buffers concepts have been defined so that containers such as std::vector, std::list, std::vector or boost::array can be used. See the documentation for asio::buffer for examples.
asio 0.3.8rc3 Home | Reference | Tutorial | Examples | Design