我刚才注意到async_write_some
中的async_send
和boost::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
库中有两个相同的函数?有历史原因吗?
谢谢!
答案 0 :(得分:8)
它们提供两种不同的抽象:
stream.async_write_some()
允许一般性地写入异步流I / O对象。例如,此抽象允许更高级别的async_write()
组合操作一般写入ip::tcp::socket
,ssl:stream
,serial_port
等。async_write_some()
成员函数是AsyncWriteStream类型要求的一部分。socket.async_send()
允许一个人一般地写入套接字而不考虑协议。例如,这种抽象允许一个人一般写入ip::tcp::socket
,ip::udp::socket
,local::*_protocol::socket
和generic::*_protocol::socket
。 socket.async_send()
模型的存在与已建立的BSD套接字API紧密相关。