使用cpp-netlib进行异步客户端响应?

时间:2016-05-05 17:22:29

标签: c++ boost cpp-netlib

我正在考虑将cpp netlib用于新项目。所有示例都显示以阻塞方式读取响应的正文:

client::response response_ = client_.get(request_);
std::string body_ = body(response_);

如果我使用async标签构建客户端对象:

basic_client<http_async_8bit_udp_resolve_tags, 1, 1> client_();

它有什么影响?

是否可以将body包装器的结果作为boost::shared_future<std::string>

我是否只需将阻塞调用包装在自己的线程中?

1 个答案:

答案 0 :(得分:1)

查看当前的http客户端doc:http://cpp-netlib.org/0.12.0/reference/http_client.html

  1. 默认情况下,http客户端始终是异步的
  2. 您可以选择在getpost来电中提供回调功能或对象:

    struct body_handler {
    
        explicit body_handler(std::string & body)
        : body(body) {}
    
        BOOST_NETWORK_HTTP_BODY_CALLBACK(operator(), range, error) {
            // in here, range is the Boost.Range iterator_range, and error is
            // the Boost.System error code.
            if (!error)
                body.append(boost::begin(range), boost::end(range));
        }
    
        std::string & body;
    };
    
    // somewhere else
    std::string some_string;
    response_ = client_.get(request("http://cpp-netlib.github.com/"),
                        body_handler(some_string));
    
  3. client::response对象已经封装了future对象:

  4.   

    响应对象封装了一旦值可用就会填充的期货。