2016-05-05 49 views
3

我正在考慮將cpp netlib用於新項目。所有的例子顯示讀取阻塞的方式響應的正文:與cpp-netlib異步客戶端響應?

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

如果你建造我的客戶對象與異步標籤:

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

什麼影響不會有?

是否可以將body包裝的結果作爲boost::shared_future<std::string>

我是否需要將阻塞調用包裝在它自己的線程中?

回答

1

看待當前的HTTP客戶端的文檔: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對象已經incapsulates的future對象:

響應對象封裝期貨這得到填補,一旦值可用。