提高:: ASIO ::产卵产量回调 [英] boost::asio::spawn yield as callback
问题描述
我试图重写使用的boost ::支持ASIO ::产卵
协同程序的项目。该项目的某些部分不能被改变。例如,存储协议库也被写入与的boost :: ASIO
,但没有协同程序。
问题是如何 yield_context
转换成普通回调(A 的boost ::功能
对象或经典仿函数)。
这是我们所拥有的存储库API中:
无效async_request_data(uint64_t中ITEM_ID,提振::函数<无效(Request_result *)>回调);
当我们从实例知道,ASIO产量上下文可以使用这样的:
my_socket.async_read_some(提高:: ASIO ::缓冲区(数据),收益率);
在这种情况下,提高:: ASIO :: yield_context
对象充当 async_read_some 的回调。我想传递的收益对象作为第二个参数 async_request_data code>,这样我就可以以同步的方式使用它。
如何才能做到这一点?我认为它可能通过某些代理对象是可能的,可能使用的基础上的 asio_handler_invoke 的方法。但我有看到如何做到这一点的麻烦。
看起来像此功能的最好的文档可以在C ++由升压ASIO作者撰写标准提案中找到:
见9.1它说:
handler_type_t< CompletionToken,无效(错误_ code,为size_t)> #3
处理程序(的std ::向前< CompletionToken>(令牌));
3:完成令牌被转换成一个处理程序,即功能对象被调用
当异步操作完成。签名指定的参数
将被传递给处理程序。
块引用>我猜你的情况
CompletionToken
模板参数实际上是的boost ::支持ASIO :: yield_context
和handler_type
将其转换为回调对象。下面是部分9.1 code更新打电话给你的
async_request_data code>功能:
模板<类CompletionToken>
汽车async_foo(uint64_t中ITEM_ID,CompletionToken和放大器;&安培;令牌)
{
handler_type_t< CompletionToken,无效(Request_result *)>
处理程序(的std ::向前< CompletionToken>(令牌)); async_result< decltype(处理)GT;结果(处理); async_request_data(ITEM_ID,处理程序); 返回result.get();
}I'm trying to rewrite a project using
boost::asio::spawn
coroutines. Some parts of the project cannot be changed. For example, the storage protocol library is also written withboost::asio
, but without coroutines.The problem is how to convert
yield_context
into a normal callback (aboost::function
object or a classical functor).This is what we have in the storage library API:
void async_request_data(uint64_t item_id, boost::function< void(Request_result *) > callback);
As we know from examples, the asio yield context can be used like this:
my_socket.async_read_some(boost::asio::buffer(data), yield);
In this case a
boost::asio::yield_context
object serves as a callback for async_read_some. I would like to pass a yield object as the second argument toasync_request_data
, so i can use it in a synchronous manner.How can this be done? I think it may be possible via some proxy-object, possibly using an approach based on asio_handler_invoke. But I am having trouble seeing how to do this.
解决方案Looks like the best documentation for this feature can be found in a C++ standard proposal written by the boost asio author:
N4045 – Library Foundations for Asynchronous Operations, Revision 2
See section 9.1 which says:
handler_type_t<CompletionToken, void(error_code, size_t)> #3 handler(std::forward<CompletionToken>(token));
3: The completion token is converted into a handler, i.e. a function object to be called when the asynchronous operation completes. The signature specifies the arguments that will be passed to the handler.
I guess in your case the
CompletionToken
template argument will actually beboost::asio::yield_context
andhandler_type
converts it to a callback object.
Here is the code from section 9.1 updated to call your
async_request_data
function:template <class CompletionToken> auto async_foo(uint64_t item_id, CompletionToken&& token) { handler_type_t<CompletionToken, void(Request_result *)> handler(std::forward<CompletionToken>(token)); async_result<decltype(handler)> result(handler); async_request_data(item_id, handler); return result.get(); }
这篇关于提高:: ASIO ::产卵产量回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!