Boost :: asio async_write_some vs async_send

时间:2016-08-19 11:59:29

标签: c++ boost-asio send

我刚才注意到async_write_some中的async_sendboost::asio(第二次重载)函数完全相同:

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定义:

...
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));
  }
...

为什么boost::asio库中有两个相同的函数?有历史原因吗?

谢谢!

1 个答案:

答案 0 :(得分:8)

它们提供两种不同的抽象:

  • stream.async_write_some()允许一般性地写入异步流I / O对象。例如,此抽象允许更高级别的async_write()组合操作一般写入ip::tcp::socketssl:streamserial_port等。async_write_some()成员函数是AsyncWriteStream类型要求的一部分。
  • socket.async_send()允许一个人一般地写入套接字而不考虑协议。例如,这种抽象允许一个人一般写入ip::tcp::socketip::udp::socketlocal::*_protocol::socketgeneric::*_protocol::socketsocket.async_send()模型的存在与已建立的BSD套接字API紧密相关。