boostboost-asioboost-coroutine

Is there way to get the number of bytes transfered in Boost asio with coroutines


I am using boost asio with coroutine to get some data from tcp socket. In the exmaples shown in those document, the example looks like

http::async_read(socket, buffer, request, yield[ec]);

But how can I get number of bytes transferred in this case.

Without using coroutine we can bind a callback function.

void onReadDataComplete(boost::system::error_code ec, std::size_t bytes_transferred)

But it is not very clear how can I do the same thing with coroutine.


Solution

  • Read carefully this link.

    Your initiating function async_read can be called with handler or yield. Handler signature must be

    void handler(boost::system::error_code ec, result_type result);
    

    where result means how many bytes were read. When you call async_read with yield in place of handler, async_read returns result_type i.e. size_t - which means counter of read bytes. So you need only to check return value from async_read.