在 Boost 1.66 中,Asio弃用了asio_handler_is_continuation钩子函数,促进了defer函数的使用。该 defer函数的行为似乎与postasio_handler_is_continuation==true 时的行为完全相同。但是,使用方式与使用defer方式不同asio_handler_is_continuation,我不确定如何正确使用defer。
编辑:我认为下面的示例过于冗长,无法清楚地表达我的意思。这是较短的示例:
async_read_until(stream, read_buffer, "\r\n",
[](boost::system::error_code ec, std::size_t bytes_transferred)
{
if(!ec)
async_write(stream, write_buffer, some_handler);
})
Run Code Online (Sandbox Code Playgroud)
现在async_read_until完成后,传递的 lambda 处理程序将使用某种等效于boost::asio::post. 但是async_write在 lambda 处理程序内部是上次异步任务的延续,所以我想调用 lambda 处理程序来利用defer优化。
有没有办法使用defer(而不是post)来调用上面示例中的 lambda 处理程序?
ORIGINAL POST:我正在尝试编写一个async_echo类似于野兽文档中的简单启动函数,除了调用的部分boost::asio::async_write将作为延续调用。为了实现这一点,先前的中间操作boost::asio::async_read_until必须调用处理程序*this作为延续。
这是我在野兽文档的 async_echo 示例中所指的部分:
template<class AsyncStream, …Run Code Online (Sandbox Code Playgroud)