【问题标题】:Boost::asio async_write_some vs async_sendBoost::asio async_write_some 与 async_send
【发布时间】:2016-12-26 14:06:31
【问题描述】:

我刚才才注意到boost::asio 中的async_write_someasync_send(第二次重载)函数完全一样:

async_write_some防御:

...
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 库中有两个相同的函数?有什么历史原因吗?

谢谢!

【问题讨论】:

    标签: c++ boost-asio send


    【解决方案1】:

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

    • 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 非常接近。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-07
      相关资源
      最近更新 更多