azmq icon indicating copy to clipboard operation
azmq copied to clipboard

Add support for extensible async_ overloads

Open rodgert opened this issue 10 years ago • 5 comments
trafficstars

Newer versions of Asio implement an extensible async_ form that allows async operations to work with std::future, among other things.

Will require bumping the minimum version of boost to 1.56 or later

rodgert avatar Nov 22 '14 23:11 rodgert

AFAIK, std::future support was added to ASIO in Boost 1.55. But I'm not 100% sure.

oliora avatar Nov 24 '14 00:11 oliora

I can confirm with Chris Kohlhoff, but I was thinking of just making 1.56 the base Boost required version unless there is a compelling reason not to.

On Sunday, November 23, 2014, Andrey Upadyshev [email protected] wrote:

AFAIK, std::future support was added to ASIO in Boost 1.55. But I'm not 100% sure.

— Reply to this email directly or view it on GitHub https://github.com/zeromq/aziomq/issues/11#issuecomment-64142397.

rodgert avatar Nov 24 '14 01:11 rodgert

Chris confirms 1.54 introduced std::future support. I'm happy to make that the minimum version if there is a compelling reason.

On Sunday, November 23, 2014, Thomas Rodgers [email protected] wrote:

I can confirm with Chris Kohlhoff, but I was thinking of just making 1.56 the base Boost required version unless there is a compelling reason not to.

On Sunday, November 23, 2014, Andrey Upadyshev <[email protected] javascript:_e(%7B%7D,'cvml','[email protected]');> wrote:

AFAIK, std::future support was added to ASIO in Boost 1.55. But I'm not 100% sure.

— Reply to this email directly or view it on GitHub https://github.com/zeromq/aziomq/issues/11#issuecomment-64142397.

rodgert avatar Nov 24 '14 01:11 rodgert

Yes please. It's better to keep minimum Boost version as low as possible because many companies too cautious to upgrade it often.

oliora avatar Nov 25 '14 23:11 oliora

In case you guys are still looking for a solution, this is what I use (inside a util namespace):

template<class T, class ConstBufferSequence, class CompletionToken>
BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken,
                              void(boost::system::error_code, std::size_t))
async_send(T &socket, const ConstBufferSequence& buffers,
           CompletionToken &&token)
{
    boost::asio::async_completion<
        CompletionToken, void(boost::system::error_code, std::size_t)
    > init{token};

    socket.async_send(
        buffers,
        [handler=std::move(init.completion_handler)](
            const boost::system::error_code &ec, std::size_t bytes_transferred
        ) mutable {
            handler.get_executor().dispatch(
                [ec,bytes_transferred,handler]() mutable {
                    handler(ec, bytes_transferred);
                }, std::allocator<void>{}
            );
        }
    );

    return init.result.get();
}

handler.get_executor() can be replaced with get_associated_executor(handler, socket.get_executor()) if you handler doesn't always have an associated executor.

vinipsmaker2 avatar Dec 14 '18 14:12 vinipsmaker2