Search code examples
c++boost-asioc++20asioc++-coroutine

Boost.Asio async_read a string from a socket


I'm trying to write a function async_read_string_n to asynchronously read a string of exactly n bytes from a socket with Boost.Asio 1.78 (and GCC 11.2).

This is how I want to use the function async_read_string_n:

void run() {
  co_spawn (io_context_, [&]() -> awaitable<void> {
    auto executor = io_context_.get_executor();
    tcp::acceptor acceptor(executor, listen_endpoint_);

    auto [ec, socket] = co_await acceptor.async_accept(as_tuple(use_awaitable));
    co_spawn(executor, [&]() -> awaitable<void> {

      auto [ec, header] = co_await async_read_string_n(socket, 6, as_tuple(use_awaitable));
      std::cerr << "received string " << header << "\n";
      co_return;
    }
    , detached);
    co_return;
  }
  , detached);
}
  

Here is my attempt to write async_read_string_n, following the advice in

(I don't care about memory copying. This isn't supposed to be fast; it's supposed to have a nice API.)

template<class CompletionToken> auto async_read_string_n(tcp::socket& socket, int n, CompletionToken&& token) {
  async_completion<CompletionToken, void(boost::system::error_code, std::string)> init(token);
  asio::streambuf b;
  asio::streambuf::mutable_buffers_type bufs = b.prepare(n);
  auto [ec, bytes_transferred] = co_await asio::async_read(socket, bufs, asio::transfer_exactly(n), as_tuple(use_awaitable));
  b.commit(n);
  std::istream is(&b);
  std::string s;
  is >> s;
  b.consume(n);
  init.completion_handler(ec, s);
  return init.result.get();
}

Edit

(I had a syntax error and I fixed it.) Here is the compiler error in async_read_string_n which I'm stuck on:

GCC error:

error: 'co_await' cannot be used in a function with a deduced return type

How can I write the function async_read_string_n?


Solution

  • You don't have to use streambuf. Regardless, using the >> extraction will not reliably extract the string (whitespace stops the input).

    The bigger problem is that you have to choose whether you want to use

    • co_await (which requires another kind of signature as your second link correctly shows)
    • or the async result protocol, which implies that the caller will decide what mechanism to use (a callback, future, group, awaitable etc).

    So either make it:

    Using Async Result Protocol:

    #include <boost/asio.hpp>
    #include <boost/asio/awaitable.hpp>
    #include <boost/asio/experimental/as_tuple.hpp>
    #include <boost/asio/use_awaitable.hpp>
    #include <iostream>
    #include <iomanip>
    namespace net = boost::asio;
    using net::ip::tcp;
    using boost::system::error_code;
    
    template <typename CompletionToken>
    auto async_read_string_n(tcp::socket& socket, int n, CompletionToken&& token)
    {
        struct Op {
            net::async_completion<CompletionToken, void(error_code, std::string)>
                init;
            std::string buf;
            Op(CompletionToken token) : init(token) {}
        };
        auto op = std::make_shared<Op>(token);
    
        net::async_read(socket, net::dynamic_buffer(op->buf),
                        net::transfer_exactly(n), [op](error_code ec, size_t n) {
                            op->init.completion_handler(ec, std::move(op->buf));
                        });
        return op->init.result.get();
    }
    
    int main() {
        net::io_context ioc;
        tcp::socket s(ioc);
        s.connect({{}, 8989});
    
        async_read_string_n(s, 10, [](error_code ec, std::string s) {
            std::cout << "Read " << ec.message() << ": " << std::quoted(s)
                      << std::endl;
        });
    
        ioc.run();
    }
    

    Prints

    enter image description here

    NOTE This version affords you the calling semantics that you desire in your sample run() function.

    OR Use co_await

    Analogous to the sample here:

    boost::asio::awaitable<void> echo(tcp::socket socket)
    {
      char data[1024];
      for (;;)
      {
        auto [ec, n] = co_await socket.async_read_some(boost::asio::buffer(data),
            boost::asio::experimental::as_tuple(boost::asio::use_awaitable));
        if (!ec)
        {
          // success
        }
    
        // ...
      }
    }