次のコードでは、プライベート関数handle_read_content
はasio::async_read
、に依存する which を使用します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_;
};