Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost asio and coroutine2 example

While reading the documentation of coroutine2 I found a nice snippet of code that shows how to use it with asio

For reference here's the code from the documentation:

void session(boost::asio::io_service& io_service){
    // construct TCP-socket from io_service
    boost::asio::ip::tcp::socket socket(io_service);

    try{
        for(;;){
            // local data-buffer
            char data[max_length];

            boost::system::error_code ec;

            // read asynchronous data from socket
            // execution context will be suspended until
            // some bytes are read from socket
            std::size_t length=socket.async_read_some(
                    boost::asio::buffer(data),
                    boost::asio::yield[ec]);
            if (ec==boost::asio::error::eof)
                break; //connection closed cleanly by peer
            else if(ec)
                throw boost::system::system_error(ec); //some other error

            // write some bytes asynchronously
            boost::asio::async_write(
                    socket,
                    boost::asio::buffer(data,length),
                    boost::asio::yield[ec]);
            if (ec==boost::asio::error::eof)
                break; //connection closed cleanly by peer
            else if(ec)
                throw boost::system::system_error(ec); //some other error
        }
    } catch(std::exception const& e){
        std::cerr<<"Exception: "<<e.what()<<"\n";
    }
}

However I can't find a working example on the asio documentation, and trying to compile this snippet on coliru gives me compiler errors related to yield

Are you aware of a minimal client/server implementation that uses coroutine2 as showed in the example above?

like image 393
dau_sama Avatar asked Aug 17 '16 02:08

dau_sama


1 Answers

AFAIK boost.asio supports only boost.coroutine, not boost.coroutine2

like image 118
xlrg Avatar answered Sep 25 '22 13:09

xlrg