Hi, I am using Asio in Boost version 1.36.0 and I have a question regarding boost::asio::streambuf. Code extract: Class A { boost::asio::streambuf buf;//class member void Read() { boost::asio::async_read_until(socket, buf, boost::regex("\0101"),boost::bind(DoSomething,this,boost::asio::placehol ders::error,boost::asio::placeholders::bytes_transfered));//class function } void DoSomething(const boost::system::error_code& ec, std::size_t bytes_transferred)//class function { If(!ec && (bytes_transferred != 0)) { boost::asio::streambuf::const_buffers_type bufs = buf.data(); std::string s(boost::asio::buffers_begin(bufs),boost::asio::buffers_begin(bufs) + bytes_transferred); //Do something with the data in s; //Here I need to call async_read_until again, but buf still has the data read in the earlier call so, it calls the DoSomething handler again thus repeating //the process. Read(); } } }; My question is how do I clear the contents of buf before calling async_read_until again. I tried reading into a local variable but boost::asio::streambuf is noncopyable. Is there a better way to do what I am trying to do. Any help is greatly appreciated. Esta mensagem pode conter informação confidencial e/ou privilegiada. Se você não for o destinatário ou a pessoa autorizada a receber esta mensagem, não deverá utilizar, copiar, alterar, divulgar a informação nela contida ou tomar qualquer ação baseada nessas informações. Se você recebeu esta mensagem por engano, por favor avise imediatamente o remetente, respondendo o e-mail e em seguida apague-o. Agradecemos sua cooperação. This message may contain confidential and/or privileged information. If you are not the addressee or authorized to receive this for the addressee, you must not use, copy, disclose, change, take any action based on this message or any information herein. If you have received this message in error, please advise the sender immediately by reply e-mail and delete this message. Thank you for your cooperation.
If(!ec && (bytes_transferred != 0))
{
boost::asio::streambuf::const_buffers_type bufs = buf.data();
std::string s(boost::asio::buffers_begin(bufs),boost::asio::buffers_begin(bufs) + bytes_transferred);
<...>
My question is how do I clear the contents of buf before calling async_read_until again.
If you just want to read some text from from streambuf, do not access its data() - instead use standard istream methods, like this (the data you read is removed from the streambuf): std string someString; std::istream(&buf) >> someString; int someStatus; std::istream(&buf) >> someStatus; std::string wholeLine; std::getline(std::istream(&buf), wholeLine); // and so on...
participants (2)
-
Amit Sonawane
-
Igor R