chriskohlhoff / asio

Asio C++ Library
http://think-async.com/Asio
4.96k stars 1.22k forks source link

async_write with Future && async result #684

Open ghost opened 3 years ago

ghost commented 3 years ago

@basiliscos commented on Apr 19, 2019, 10:10 PM UTC:

Hi,

the following code works on boost 1.70, but on 1.69 it throws with std::future_error: Future already retrieved .

I cannot see any reference to in the changelog that it was fixed, and still not completely sure the code sample is correct.

Could you, please, leave a comment?

#include <boost/asio.hpp>
#include <boost/asio/use_future.hpp>
#include <future>
#include <iostream>

namespace asio = boost::asio;

template<typename Stream, typename Buff, typename CompletionToken>
auto async_op(Stream &s, Buff&& buff, CompletionToken&& ct) {
    using Signature = void(const boost::system::error_code&, std::size_t);
    using Callback = std::decay_t<CompletionToken>;
    using AsyncResult = asio::async_result<Callback, Signature>;
    using CompletionHandler = typename AsyncResult::completion_handler_type;

    CompletionHandler handler(std::forward<CompletionToken>(ct));
    AsyncResult result(handler);
    async_write(s, std::move(buff), std::move(handler));
    return result.get();
}

int main(int argc, char** argv) {
    using socket_t = asio::ip::tcp::socket;

    asio::io_service io_service;

    asio::ip::tcp::resolver resolver(io_service);
    socket_t sock(io_service);
    auto endpoints = resolver.resolve("google.com", "https");
    sock.connect(*endpoints.begin());
    char buff[] = "does-not-matter";

    auto f_write = async_op(sock, asio::buffer(buff), asio::use_future);
    //auto f_write = async_write(sock, asio::buffer(buff), asio::use_future);
    std::thread thread([&io_service]() { io_service.run(); });

    std::cout << "r = " << f_write.get() << "\n";

    io_service.stop();
    thread.join();

    return  0;
}

This issue was moved by chriskohlhoff from boostorg/asio#226.

ghost commented 3 years ago

@vinniefalco commented on Apr 20, 2019, 5:33 PM UTC:

hmm.... I have no idea!