Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost::Asio read_until and streambuf usage
From: Leo Cacciari (leo.cacciari_at_[hidden])
Date: 2012-12-06 09:27:54


Il 22/11/2012 19:38, Gonzalo Garramuno ha scritto:
> On 21/11/12 14:56, Igor R wrote:
>> Do you expect std::getline to give you more than 1 line?
> No, sorry I was not clear. I expect the streambuf or read_until to have
> some way of telling me there's more lines to read. With that piece of
> code, the streambuf swallows multiple lines, but getline only gets to
> the first line. Only when read_until is run again, the other lines
> appear. I need to poll? the socket for more data, but not sure how that
> is done in asio.

This is not the way read_until works. The problem is that it can have
read more data thant what is until the "\n", but it only committed
reply_length bytes. Thus you should do something like the following

typedef boost::asio::streambuf::const_buffers_type buffer_type;
typedef boost::asio::buffers_iterator<buffer_type> iterator;

 boost::asio::streambuf buf;

      while( 1 ) {
         boost::system::error_code ec;

         size_t reply_length = boost::asio::read_until( socket_,
                                                        buf, '\n',ec);
         if (ec) {
            // probably connection closed, do something smart
            // about it
         }
         std::string line;
         buffer_type input buf.data();

         std::copy(iterator::begin(input),iterator::end(input),
                   std::back_inserter(line));
         buf.consume(reply_length);
     }

-- 
Leo Cacciari
Aliae nationes servitutem pati possunt populi romani est propria libertas

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net