Boost logo

Boost :

Subject: Re: [boost] [beast] Asynchronous examples
From: Vinnie Falco (vinnie.falco_at_[hidden])
Date: 2017-07-02 19:21:40

On Sun, Jul 2, 2017 at 12:07 PM, Phil Endecott via Boost
<boost_at_[hidden]> wrote:
> Do any of the Beast examples show its use in asynchronous mode?

The files marked "async_*" and also multi_port.hpp use asynchronous interfaces:

The use of Beast's asynchronous HTTP interfaces is virtually identical
to Asio's.:

Replace `my_socket.async_read_some` with `beast::http::async_read` and
adjust parameters.

Beast uses Asio's extensible asynchronous model so however you receive
notifications when your asynchronous Asio task completes, will also
work with asynchronous HTTP function signatures. The documentation
explains some of the return value customization (its written from the

> I'm wondering how difficult it is to achieve this:
> future< vector< byte > > data = download(url);

I won't candy-coat this: You're going to have to do some heavy lifting.

The `download` function is going to have to first do everything
necessary to establish the connection, using direct Asio interfaces.
If you want "https://" support you will need to write that too. In
fact, you better bring a URL parser if you want the ship to leave the
launch pad.

Once you have your socket established you'll have to fill out a
beast::http::request with the correct information and then call
beast::http::async_write to send it to the remote host. You'll need to
take care of proxy settings if any, as well as Basic Authentication if
the server requires it. If the remote host redirects you then you'll
have to handle that yourself too.

To make it work with futures, you will need to implement all of this
in the style of a "composed operation" which requires significant
boilerplate and expertise. Beast provides classes, functions,
examples, and a tutorial of this style to help you get it going:

The signature for your initiating function will look like this:

    template<class CompletionToken>
        void(error_code, std::vector<char>)>
        beast::string_view url;
        CompletionToken&& token);

You would call it this way:

    std::future<vector<char>> result =
async_download("", boost::asio::use_future);

Hope this helps!


Boost list run by bdawes at, gregod at, cpdaniel at, john at