async_resolve和async_connect参数生存期 [英] async_resolve and async_connect params lifetime

查看:304
本文介绍了async_resolve和async_connect参数生存期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道传递给上述方法的对象的寿命是什么.

I would like to know what is lifetime of objects passed to mentioned methods.

ip::basic_resolver::async_resolve(const query & q, ResolveHandler handler);

(1)在调用处理程序之前,我是否需要保持解析器处于活动状态? (是)
(2)async_resolve是否复制query对象? (我正在传递在堆栈上创建的一个-是的)

(1) Do I need to keep resolver alive until handler is called? (yes)
(2) Does async_resolve copy query object? (I am passing one created on the stack - yes)

{
   boost::asio::ip::tcp::resolver::query query(host_, port_);
   resolver_.async_resolve(query, );
}

(3)处理程序中的boost::asio::ip::tcp::resolver::iterator是否按值返回? (是)

(3) Is boost::asio::ip::tcp::resolver::iterator returned in handler by value? (yes)

template<..> void async_connect(basic_socket<Protocol, SocketService> & s,
Iterator begin, ComposedConnectHandler h);

(4)begin是否按值传递? (是)
(5)我是否需要保持resolver的生命? (否)

(4) Is begin passed by value? (yes)
(5) Do I need to keep resolver alive? (no)

推荐答案

对于Boost.Asio,一般规则是:

With Boost.Asio, the general rule is that:

  • 将复制const-reference或value传递的参数.如果支持,它们可能会被移动.
  • 由非常量引用传递的参数必须保持有效,直到同步操作完成或调用异步操作的处理程序为止.

如果规则有例外情况,文档将进行说明,例如

When there are exceptions to the rule, the documentation will indicate it, such as the case of the buffers argument for boost::asio::async_write, which must remain valid until the handler is called.

对于 ip::basic_resolver :

  • 与其他服务对象一样,如果服务对象被未完成的异步操作破坏,则异步操作处理程序将由boost::asio::error::operation_aborted调用.如果您不想管理服务对象的寿命,请使用 shard_ptr 并将shared_ptr的副本绑定到处理程序中.
  • 对于 async_resolve query对象通过const引用传递,并最终复制到基础操作的构造函数中(

  • As with other service objects, if the service object is destroyed with outstanding asynchronous operations, then the asynchronous operation handlers will be invoked with boost::asio::error::operation_aborted. If you do not want to manage the lifespan of the service object, then manage it with a shard_ptr and bind a copy of the shared_ptr into the handler.
  • For async_resolve the query object is passed by const-reference and eventually copied in the underlying operation's constructor (resolve_endpoint_op). This also permits using a temporary query object as well.

{
  typedef boost::asio::ip::tcp::resolver::query query;
  resolver_.async_resolve(query(host_, port_), );
}

  • async_resolve 希望 handler 符合

  • async_resolve expects handler to meet the ResolverHandler requirements. It documents that the iterator argument is taken by value.

    对于 boost::asio::async_connect :

    • 根据文档,开始参数是按值传递的.
    • resolver不需要保持活动状态,因为迭代器具有查询结果的共享所有权. ip::basic_resolver_iterator shared_ptr维持为 ip::basic_resolver_entry 对象. basic_resolver_entry对象具有用于端点,主机名和服务名的成员变量.
    • Per the documentation, the begin argument is passed by value.
    • The resolver does not need to remain alive because the iterator's have shared ownership of the query results. While not in the documentation, the ip::basic_resolver_iterator maintains a shared_ptr to a std::vector of ip::basic_resolver_entry objects. The basic_resolver_entry objects have member variables for endpoint, host name, and service name.

    这篇关于async_resolve和async_connect参数生存期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆