操作在async_resolve上取消

时间:2015-06-12 12:14:07

标签: c++ c++11 boost-asio

我对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)。

我的目标是有两个班级;

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

    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();
    }
    

    我收到错误&#34;取消操作&#34;以这种特殊的方式; 如果我以下列方式实例化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对象不可复制,我该如何解决这个问题?

1 个答案:

答案 0 :(得分:4)

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

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

默认的析构函数执行成员方式的销毁。因此它会触发析构函数tcp::resolver resolver_。该类的文档声明任何挂起的异步操作都将被取消。

原则上&#34;替代&#34; main有完全相同的问题(实际上它在我的框中失败了Operation canceled)。如果它不适合你,你会得到非常幸运的事件时间。