2013-09-29 51 views
0

我在升壓:: ASIO新手和有第一自己的煩惱。 我創建了一個簡單的主機解析器(請參閱下面的完整代碼)。的boost ::支持ASIO :: io_service對象的析構函數運行很長一段時間

問題1. 在沒有網絡連接的情況下,我的主機解析器停止解析後的第一個進入deadline_timer。 我的假設是,「localhost」必須隨時解決。但是在解析google.us期間(例如,我們拔掉以太網插口)超時後,「localhost」沒有解決。 解決未知TLD時的相同行爲(例如,google.usd代替google.us)。

問題2: 在互聯網連接丟失的情況下,析構函數io_service運行時間很長(通常爲5秒)。

怎麼了?

我用VS2012,提高1.54

文件hostresolver.h

pragma once 

#include <set> 

#include <boost/system/error_code.hpp> 
#include <boost/asio.hpp> 
#include <boost/asio/ip/basic_resolver.hpp> 
#include <boost/asio/ip/basic_resolver_iterator.hpp> 

typedef std::set<unsigned long> hostresolver_result_container; 

class hostresolver 
{ 
public: 
    hostresolver(boost::asio::io_service* io_service); 
    ~hostresolver(void); 

    boost::asio::io_service* ios_ptr; 
    boost::asio::ip::tcp::resolver resolver_; 
    boost::asio::deadline_timer timer_; 

    volatile bool is_completed; 
    bool is_timeout; 
    std::string hostname; 
    hostresolver_result_container result; 

    void on_timeout(const boost::system::error_code &err); 
    void start_resolve(const char* hostname, int timeout_seconds); 
    void finish_resolve(const boost::system::error_code& err, boost::asio::ip::tcp::resolver::iterator endpoint_iterator); 

private: 
    void stop(); 
}; 

文件hostresolver.cpp

#include "stdafx.h" 
#include "hostresolver.h" 

#include <boost/bind.hpp> 

hostresolver::hostresolver(boost::asio::io_service* io_service) : 
    resolver_(*io_service), timer_(*io_service), is_completed(false), is_timeout(false) 
{ 
    ios_ptr = io_service; 
} 

hostresolver::~hostresolver(void) 
{ 
} 

void hostresolver::start_resolve(const char* hostname, int timeout_second) 
{ 
    this->hostname.assign(hostname); 

    timer_.expires_from_now(boost::posix_time::seconds(timeout_second)); 
    timer_.async_wait(boost::bind(&hostresolver::on_timeout, this, _1)); 

    boost::asio::ip::tcp::resolver::query query(hostname, "http"); 
    resolver_.async_resolve(query, 
          boost::bind(&hostresolver::finish_resolve, this, 
          boost::asio::placeholders::error, 
          boost::asio::placeholders::iterator)); 

    do 
    { 
     ios_ptr->run_one(); 
    } 
    while (!is_completed); 
} 

void hostresolver::stop() 
{ 
    resolver_.cancel(); 
    timer_.cancel(); 
    is_completed = true; 
} 

void hostresolver::on_timeout(const boost::system::error_code &err) 
{ 
    if ((!err) && (err != boost::asio::error::operation_aborted)) 
    { 
     is_timeout = true; 
     stop(); 
    } 
} 

void hostresolver::finish_resolve(const boost::system::error_code& err, boost::asio::ip::tcp::resolver::iterator endpoint_iterator) 
{ 
    if (!err) 
    { 
     while (endpoint_iterator != boost::asio::ip::tcp::resolver::iterator()) 
     { 
      boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator; 
      if (endpoint.address().is_v4()) 
      { 
       result.insert(endpoint.address().to_v4().to_ulong()); 
      } 
      endpoint_iterator++; 
     } 
    } 

    stop(); 
} 

文件main.cpp中

#include "stdafx.h" 

#include "hostresolver.h" 


int _tmain(int argc, _TCHAR* argv[]) 
{ 
    boost::asio::io_service ios; 

    for (int i = 0; i < 2; i++) 
    { 
     std::cout << "iteration: " << i << std::endl; 

     { 
      hostresolver hres(&ios); 
      hres.start_resolve("localhost", 1); 
      if (hres.result.size() == 0) 
       std::cout << "failed" << std::endl; 
     } 

     { 
      hostresolver hres(&ios); 
      hres.start_resolve("google.usd", 1); 
     } 
    } 


    return 0; 
} 

回答

0
+0

io_service對象::復位之前必須調用的任何第二或稍後設置)運行的調用(,run_one(),輪詢()或poll_one的() 。 – user1215838

+0

@ user1215838「...當由於io_service被停止或不再使用而返回先前調用這些函數時」 –

相關問題