I would like to use the very convenient Boost async_read_until to read the message until I get the delimiter \r\n\r\n .
I like to use this separator because it is easily debugged with telnet and makes multi-line commands. I simply signal the completion of the command in two new lines.
I call async_read_until as follows:
void do_read() { boost::asio::async_read_until(m_socket, m_input_buffer, "\r\n\r\n", std::bind(&player::handle_read, this, std::placeholders::_1, std::placeholders::_2)); }
And my handler looks like this:
void handle_read(boost::system::error_code ec, std::size_t nr) { std::cout << "handle_read: ec=" << ec << ", nr=" << nr << std::endl; if (ec) { std::cout << " -> emit on_disconnect\n"; } else { std::istream iss(&m_input_buffer); std::string msg; std::getline(iss, msg); std::cout << "dump:\n"; std::copy(msg.begin(), msg.end(), std::ostream_iterator<int>(std::cout, ", ")); std::cout << std::endl; do_read(); } }
I wanted to use std::getline just like in the example, but on my system this saves the \r character. As you can see, if I connect to the server and write hello plus two CRLFs, I get this side of the dump server:
handle_read: ec=system:0, nr=9 dump: 104, 101, 108, 108, 111, 13, ^^^ \r here
By the way, this will also save the next new line in the buffer. So I think std::getline will not do the job for me.
I am looking for a convenient and efficient way to read from boost::asio::streambuf until I get this delimiter \r\n\r\n . Since I use async_read_until once at a time when the handler is called, it is assumed that the buffer has accurate and complete data, isn't it? What do you recommend reading until I get \r\n\r\n ?