boost :: asio :: io_service析构函数运行很长时间

时间:2013-09-29 10:57:09

标签: c++ boost boost-asio

我是boost :: asio的新手,并且遇到了第一个麻烦。 我创建了一个简单的主机解析器(请参阅下面的完整代码)。

问题1。 如果Internet连接丢失,我的主机解析器会在第一次进入deadline_timer后停止解析。 我的假设是,“localhost”必须随时解决。但在解决google.us过程中超时后“localhost”未解决(例如,我们拔掉了以太网插孔)。 解决未使用的TLD时的行为相同(例如,google.usd而不是google.us)。

问题2。 如果Internet连接丢失,析构函数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;
}

1 个答案:

答案 0 :(得分:0)