Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Multiple-datagram write token requirements

A multiple-datagram write token is a completion token for completion signature void(error_code, size_t), where the second argument is the number of datagrams sent.

Examples

A free function as a multiple-datagram write token:

void send_handler(
    const boost::system::error_code& ec,
    std::size_t messages_transferred)
{
  ...
}

A multiple-datagram write token function object:

struct send_handler
{
  ...
  void operator()(
      const boost::system::error_code& ec,
      std::size_t messages_transferred)
  {
    ...
  }
  ...
};

A lambda as a multiple-datagram write token:

socket.async_send_multiple(...,
    [](const boost::system::error_code& ec,
      std::size_t messages_transferred)
    {
      ...
    });

A non-static class member function adapted to a multiple-datagram write token using std::bind():

void my_class::send_handler(
    const boost::system::error_code& ec,
    std::size_t messages_transferred)
{
  ...
}
...
socket.async_send_multiple(...,
    std::bind(&my_class::send_handler,
      this, std::placeholders::_1,
      std::placeholders::_2));

A non-static class member function adapted to a multiple-datagram write token using boost::bind():

void my_class::send_handler(
    const boost::system::error_code& ec,
    std::size_t messages_transferred)
{
  ...
}
...
socket.async_send_multiple(...,
    boost::bind(&my_class::send_handler,
      this, boost::asio::placeholders::error,
      boost::asio::placeholders::messages_transferred));

Using use_future as a multiple-datagram write token:

std::future<std::size_t> f =
  socket.async_send_multiple(..., boost::asio::use_future);
...
try
{
  std::size_t n = f.get();
  ...
}
catch (const system_error& e)
{
  ...
}

Using use_awaitable as a multiple-datagram write token:

boost::asio::awaitable<void> my_coroutine()
{
  try
  {
    ...
    std::size_t n =
      co_await socket.async_send_multiple(
          ..., boost::asio::use_awaitable);
    ...
  }
  catch (const system_error& e)
  {
    ...
  }
}

PrevUpHomeNext