提高:: ASIO :: async_resolve问题 [英] boost::asio::async_resolve Problem

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

问题描述

我在建造使用的boost :: ASIO一个Socket类的过程。首先,我做了一个连接那了主机和端口,并将其解析为IP地址的方法。这个工作很好,所以我决定在仰望 async_resolve 。然而,我的回调总是得到一个错误code 995 (使用相同的目标主机/端口时,它的工作同步)。

I'm in the process of constructing a Socket class that uses boost::asio. To start with, I made a connect method that took a host and a port and resolved it to an IP address. This worked well, so I decided to look in to async_resolve. However, my callback always gets an error code of 995 (using the same destination host/port as when it worked synchronously).

code

这将启动分辨率功能:

  // resolve a host asynchronously
  template<typename ResolveHandler>
  void resolveHost(const String& _host, Port _port, ResolveHandler _handler) const
  {
   boost::asio::ip::tcp::endpoint ret;
   boost::asio::ip::tcp::resolver::query query(_host, boost::lexical_cast<std::string>(_port));
   boost::asio::ip::tcp::resolver r(m_IOService);
   r.async_resolve(query, _handler);
  }; // eo resolveHost

code调用此函数:

Code that calls this function:

  void Socket::connect(const String& _host, Port _port)
  {
   // Anon function for resolution of the host-name and asynchronous calling of the above
   auto anonResolve = [this](const boost::system::error_code& _errorCode, 
           boost::asio::ip::tcp::resolver_iterator _epIt)
   {
    // raise event
    onResolve.raise(SocketResolveEventArgs(*this, !_errorCode ? (*_epIt).host_name() : String(""), _errorCode));

    // perform connect, calling back to anonymous function
    if(!_errorCode)
     connect(*_epIt);
   };

   // Resolve the host calling back to anonymous function
   Root::instance().resolveHost(_host, _port, anonResolve);

  }; // eo connect

消息()功能错误_ code 是:

The I/O operation has been aborted because of either a thread exit or an application request

和我的的main.cpp 是这样的:

int _tmain(int argc, _TCHAR* argv[])
{
 morse::Root root;
 TextSocket s;
 s.connect("somehost.com", 1234);
 while(true)
 {
  root.performIO(); // calls io_service::run_one()
 }
 return 0;
}

在此先感谢!

推荐答案

解析目标是走出去的范围,将其移动到<$ C $的成员C>插座类,并让 resolveHost 的方法,而不是免费的功能。

Your resolver object is going out of scope, move it to a member of the Socket class and make resolveHost a method rather than free function.

这是因为的boost ::支持ASIO ::知识产权:: TCP ::解析是的一个 basic_resolver ,<一个一个typedef href=\"http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/reference/ip__basic_resolver.html\">which继承 basic_io_object 。当解析器超出范围,〜basic_io_object() destroys在你的<一个底层的解析服务href=\"http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/reference/ip__basic_resolver/async_resolve/overload1.html\">handler可以发布。

This happens because boost::asio::ip::tcp::resolver is a typedef of a basic_resolver, which inherits from basic_io_object. When the resolver goes out of scope, ~basic_io_object() destroys the underlying resolver service before your handler can be posted.

不管异步
  操作完成后立即或
  不是,则处理程序将不被调用
  从这个函数中。调用
  处理程序将在一个被执行
  以等效于使用
  提高:: ASIO :: io_service对象::后()。

Regardless of whether the asynchronous operation completes immediately or not, the handler will not be invoked from within this function. Invocation of the handler will be performed in a manner equivalent to using boost::asio::io_service::post().

这篇关于提高:: ASIO :: async_resolve问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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