Yes, I want to keep reading data an process them. Here is my implementation base on my understanding of your suggestion:
class session
{
public:
session(boost::asio::io_service& io_service)
: socket_(io_service)
{
}
tcp::socket& socket()
{
return socket_;
}
void start()
{
socket_.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void handle_read(const boost::system::error_code& error,
size_t bytes_transferred)
{
if (!error)
{
std::cout << boost::format("handle_read bytes_transferred ='%1%'") % bytes_transferred << std::endl;
process(data_);
socket_.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
std::cout << boost::format("handle_read data_ ='%1%'") % data_ << std::endl;
}
else
{
delete this;
}
}
void process(char *data)
{
std::cout << boost::format("process data_ ='%1%'") % data_ << std::endl;
}
private:
tcp::socket socket_;
enum { max_length = 1024 };
char data_[max_length];
};