async_resolve 取消操作

Operation Canceled on async_resolve

我对 C++ 的经验很少,并且在使用 boost-asio 时遇到了一些问题。 我想按以下方式重写标准的 boost-asio async-http-client 示例 (http://www.boost.org/doc/libs/1_58_0/doc/html/boost_asio/example/cpp03/http/client/async_client.cpp)。

我的目标是拥有 2 个 类;

  1. AsyncHttpClient(存储主机并具有将异步调用发送到指定路径的成员函数)。
  2. AsyncHttpConnection(以 io_service、主机、路径作为参数 并遵循 boost-asio async-http-client 中指定的流程 示例)

我有以下实现

using boost::asio::ip::tcp;

class AsyncHttpConnection {
    public:
        AsyncHttpConnection(
            boost::asio::io_service& io_service,
            std::string host,
            std::string path) : resolver_(io_service),
                                socket_(io_service),
                                host_(host),
                                path_(path)
        {
            tcp::resolver::query query(host_, "http");
            resolver_.async_resolve(query,
                boost::bind(&AsyncHttpConnection::handle_resolve,
                    this,
                    boost::asio::placeholders::error,
                    boost::asio::placeholders::iterator));
        }

    private:
        std::string host_;
        std::string path_;
        tcp::resolver resolver_;
        tcp::socket socket_;
        boost::asio::streambuf request_;
        boost::asio::streambuf response_;

        void handle_resolve(
            const boost::system::error_code& err,
            tcp::resolver::iterator endpoint_iterator)
        {
            if (!err) {
                // code here
            } else {
                std::cout << err.message() << std::endl; // GOT "Operation Canceled" here
            }
        }

        // list of other handlers

};

class AsyncHttpClient {
    public:
        AsyncHttpClient(
            boost::asio::io_service& io_service,
            std::string host) : host_(host)
        {
            io_service_ = &io_service; // store address of io_service
        }

        void async_call(std::string path)
        {
            AsyncHttpConnection(*io_service_, host_, path);
        }

    private:
        std::string host_;
        boost::asio::io_service* io_service_; // pointer, because io_service is uncopyable; 
};

int main(int argc, char* argv[])
{
    boost::asio::io_service io_service;
    AsyncHttpClient boost(io_service, "www.boost.org");
    boost.async_call("/doc/libs/1_51_0/doc/html/boost_asio/example/http/client/async_client.cpp");
    io_service.run();
}

我在这种特殊情况下遇到错误 "Operation Canceled"; 如果我按以下方式实例化 AsyncHttpConnection

int main(int argc, char* argv[])
{
    boost::asio::io_service io_service;
    AsyncHttpConnection(io_service, "www.boost.org", "path");
    io_service.run();
}

一切正常,我认为问题在于使用指向 io_service 的指针。如果 io_service 对象不可复制,我该如何解决这个问题?

void async_call(std::string path) {
    AsyncHttpConnection(*io_service_, host_, path); 
}

正文构造了一个 AsyncHttpConnection 类型的临时对象。因此,在语句完成之前,该类型的析构函数运行。

默认析构函数执行成员级析构。所以它触发了析构函数tcp::resolver resolver_。此 class 的文档指出,任何挂起的异步操作都将在这样做时被取消。

原则上 "alternative" main 有完全相同的问题(实际上它在我的盒子上显示 Operation canceled 失败了)。如果它不适合你,你会得到非常幸运的事件时机。