2013-08-28 40 views
0

在以下代碼中,專用功能handle_read_content使用asio::async_read,這取決於handle_read_content。這可以調用遞歸行爲還是創建競爭條件?類中的遞歸函數或競態條件?

class client 
{ 
public: 
    //constrcutor, io_service, server name? path? 

    client(asio::io_service& io_service, 
      const std::string& server, const std::string& path) 
    : resolver_(io_service), 
    socket_(io_service) 
    { 
     //form request, connection close header server close the socket 
     //std:ostream reuest_stream 


     // Form the request. We specify the "Connection: close" header so that the 
     // server will close the socket after transmitting the response. This will 
     // allow us to treat all data up until the EOF as the content. 
     std::ostream request_stream(&request_); 
     request_stream << "GET " << path << " HTTP/1.0\r\n"; 
     request_stream << "Host: " << server << "\r\n"; 
     request_stream << "Accept: */*\r\n"; 
     request_stream << "Connection: close\r\n\r\n"; 

     // Start an asynchronous resolve to translate the server and service names 
     // into a list of endpoints. 
     //tcp:resolver:query 

     tcp::resolver::query query(server, "http"); 

     resolver_.async_resolve(query, 
           boost::bind(&client::handle_resolve, this, 
              asio::placeholders::error, 
              asio::placeholders::iterator)); 
    } 

private: 


    void handle_read_content(const asio::error_code& err) 
    { 
     if (!err) 
     { 
      // Write all of the data that has been read so far. 
      std::cout << &response_; 

      // Continue reading remaining data until EOF. 
      asio::async_read(socket_, response_, 
          asio::transfer_at_least(1), 
          boost::bind(&client::handle_read_content, this, 
             asio::placeholders::error)); 
     } 
     else if (err != asio::error::eof) 
     { 
      std::cout << "Error: " << err << "\n"; 
     } 
    } 

    tcp::resolver resolver_; 
    tcp::socket socket_; 
    asio::streambuf request_; 
    asio::streambuf response_; 
}; 

回答