| Home | Libraries | People | FAQ | More

#### flat_stream

Stream wrapper to improve write performance.

##### Synopsis

Defined in header `<boost/beast/core/flat_stream.hpp>`

    template<
        class NextLayer>
    class flat_stream

##### Types

Name | Description
--- | ---
executor_type | The type of the executor associated with the object.
next_layer_type | The type of the next layer.

##### Member Functions

Name | Description
--- | ---
async_read_some | Start an asynchronous read.
async_write_some | Start an asynchronous write.
flat_stream | Constructor.
get_executor | Get the executor associated with the object.
next_layer | Get a reference to the next layer.
operator= |
read_some | Read some data from the stream.
write_some | Write some data to the stream.
~flat_stream | Destructor.

##### Description

This wrapper flattens writes for buffer sequences having length greater than 1 and total size below a predefined amount, using a dynamic memory allocation. It is primarily designed to overcome a performance limitation of the current version of `net::ssl::stream`, which does not use OpenSSL's scatter/gather interface for its low-level read some and write some operations. It is normally not necessary to use this class directly if you are already using `ssl_stream`. The following examples shows how to use this class with the ssl stream that comes with networking:

##### Example

To use the `flat_stream` template with SSL streams, declare a variable of the correct type. Parameters passed to the constructor will be forwarded to the next layer's constructor:

    flat_stream<net::ssl::stream<ip::tcp::socket>> fs{ioc, ctx};

Alternatively you can write

    ssl::stream<ip::tcp::socket> ss{ioc, ctx};
    flat_stream<net::ssl::stream<ip::tcp::socket>&> fs{ss};

The resulting stream may be passed to any stream algorithms which operate on synchronous or asynchronous read or write streams, examples include:

- `net::read`, `net::async_read`
- `net::write`, `net::async_write`
- `net::read_until`, `net::async_read_until`

The stream may also be used as a template parameter in other stream wrappers, such as for websocket:

    websocket::stream<flat_stream<net::ssl::stream<ip::tcp::socket>>> ws{ioc, ctx};

##### Template Parameters

Type | Description
--- | ---
`NextLayer` | The type representing the next layer, to which data will be read and written during operations. For synchronous operations, the type must support the SyncStream concept. For asynchronous operations, the type must support the AsyncStream concept. This type will usually be some variation of `net::ssl::stream`.

##### Concepts

- SyncStream
- AsyncStream

##### See Also

- https://github.com/boostorg/asio/issues/100
- https://github.com/boostorg/beast/issues/1108
- https://stackoverflow.com/questions/38198638/openssl-ssl-write-from-multiple-buffers-ssl-writev
- https://stackoverflow.com/questions/50026167/performance-drop-on-port-from-beast-1-0-0-b66-to-boost-1-67-0-beast

Convenience header `<boost/beast/core.hpp>`