gpt4 book ai didi

c++ - Boost::asio async_write_some 与 async_send

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:23:33 27 4
gpt4 key购买 nike

我刚才才注意到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 库中有两个相同的函数?有什么历史原因吗?

谢谢!

最佳答案

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

  • stream.async_write_some() 允许一般写入异步流 I/O 对象。例如,此抽象允许更高级别的 async_write()一般写入 ip::tcp::socket 的组合操作, ssl:stream , serial_port等。async_write_some() 成员函数是 AsyncWriteStream 的一部分类型要求。
  • socket.async_send() 允许在不考虑协议(protocol)的情况下向套接字写入数据。例如,这种抽象允许一般写入 ip::tcp::socketip::udp::socketlocal::*_protocol::socketgeneric::*_protocol::socketsocket.async_send() 的出现与已建立的 BSD 套接字 API 非常接近。

关于c++ - Boost::asio async_write_some 与 async_send,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39038585/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com