这是我真正难以理解的内容:
- 为什么handle_read会再次回调start_read?
- 计时器到期时会发生什么?我没有看到提供给计时器的回调例程。
void start_read()
{ // 设置读取操作的截止日期。截止日期_.expires_from_now(boost::posix_time::seconds(30));// Start an asynchronous operation to read a newline-delimited message. boost::asio::async_read_until(socket_, input_buffer_, '\n', boost::bind(&client::handle_read, this, _1));
}
void handle_read(const boost::system::error_code& ec)
{ if (stopped_) return;if (!ec) { // Extract the newline-delimited message from the buffer. std::string line; std::istream is(&input_buffer_); std::getline(is, line); // Empty messages are heartbeats and so ignored. if (!line.empty()) { std::cout << "Received: " << line << "\n"; } start_read(); } else { std::cout << "Error on receive: " << ec.message() << "\n"; stop(); }
}