Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost::asio async_write_some vs async_send

I noticed only just now that async_write_some and async_send (second overload) functions in boost::asio are completely the same:

async_write_some defenition:

...
template <typename ConstBufferSequence, typename WriteHandler>
  BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
      void (boost::system::error_code, std::size_t))
  async_write_some(const ConstBufferSequence& buffers,
      BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
  {
    // If you get an error on the following line it means that your handler does
    // not meet the documented type requirements for a WriteHandler.
    BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;

    return this->get_service().async_send(this->get_implementation(),
        buffers, 0, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
  }
...

async_send definition:

...
template <typename ConstBufferSequence, typename WriteHandler>
  BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
      void (boost::system::error_code, std::size_t))
  async_send(const ConstBufferSequence& buffers,
      BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
  {
    // If you get an error on the following line it means that your handler does
    // not meet the documented type requirements for a WriteHandler.
    BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;

    return this->get_service().async_send(
        this->get_implementation(), buffers, 0,
        BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
  }
...

Why there is two identical functions in boost::asio library? Are there any historical reasons?

Thanks!

like image 212
Kirill Chernikov Avatar asked Aug 19 '16 11:08

Kirill Chernikov


1 Answers

They provide two different abstractions:

  • stream.async_write_some() allows one to generically write to asynchronous stream I/O objects. For example, this abstraction allows for the higher-level async_write() composed operation to generically write to ip::tcp::socket, ssl:stream, serial_port, etc. The async_write_some() member function is part of the AsyncWriteStream type requirement.
  • socket.async_send() allows one to generically write to sockets without regard to the protocol. For example, this abstraction allows for one to generically write to ip::tcp::socket, ip::udp::socket, local::*_protocol::socket, and generic::*_protocol::socket. The presence of socket.async_send() models closely to the established BSD socket API.
like image 106
Tanner Sansbury Avatar answered Nov 02 '22 04:11

Tanner Sansbury