Boost logo

Boost Users :

Subject: Re: [Boost-users] Buffering and ASIO
From: Emil Mieilica (Emil.Mieilica_at_[hidden])
Date: 2008-12-17 10:25:36


Hello Vadim,

This is a piece of code from an application that inadvertently does what your application appears to be doing: receives a stream of images over a HTTP connection.
We had the same problems and you have in bold the solution we found: we call in_avail() on the read-buffer of the stream that tells us the amount of data already read but left in the buffer and then we readsome to get the actual data.

   void onConnect(boost::system::error_code ErrorCode,unsigned int fps)
   {
           boost::asio::streambuf response;
            boost::asio::read_until(*Socket, response, "\r\n");
            std::istream response_stream(&response);

            response_stream >> http_version;
            response_stream >> status_code;
            std::getline(response_stream, status_message);
            if (!response_stream || http_version.substr(0, 5) != "HTTP/")
            {
               THROW_EXCEPTION(logic_error("Invalid HTTP response"));
            }

            // Read the response headers, which are terminated by a blank line.
            boost::asio::read_until(*Socket, response, "\r\n\r\n");
            while (std::getline(response_stream, header) && header != "\r")
            {
               LOG_DEBUG(log,"Got http header: "<<header);
            }

            //Get the number of bytes we have received after the HTTP header
            size_t avail = response_stream.rdbuf()->in_avail();
            //Read that data into the buffer
            response_stream.readsome(Buffer.data(), avail );
            LOG_DEBUG(log,"Connection succeded. Already received "<<avail);

            //Call the asynchronous receive handler as if an read operation was completed with avail bytes of data
            onReadComplete(ErrorCode,avail ,fps);
   }

void onReadComplete(const boost::system::error_code& ErrorCode,size_t bytesTransfered,unsigned int fps)
   {
      if (!ErrorCode) {
         boost::asio::async_read(
            *Socket,
            boost::asio::buffer(Buffer),
            boost::asio::transfer_all(),
            boost::bind(
               & onReadComplete,
               this,
               boost::asio::placeholders::error,
               boost::asio::placeholders::bytes_transferred,
               fps
            )
         );
     }
}

Best regards,

----
Emil Mieilica
IT Manager
Office and mailing : Calea Bucuresti no 3A,
Otopeni, Ilfov, Romania
Tel:  +40 21 350 40 57 ; +40 21 350 40 54 ; +40 21  350 40 55 ;
+40 21  350 40 56 ; +4031.620.0212; +4031.620.0213; +4743.26.4142
Fax: +40 21 350 15 80
E-mail:
emilm_at_[hidden]<mailto:emilm_at_[hidden]>
office_at_[hidden]<mailto:office_at_[hidden]>
soft_at_[hidden]<mailto:soft_at_[hidden]>
Web:  http://www.mbtelecom.com>
From: boost-users-bounces_at_[hidden] [mailto:boost-users-bounces_at_[hidden]] On Behalf Of Vadim Shmelev
Sent: Monday, December 15, 2008 1:55 PM
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] Buffering and ASIO
> Good day.
> I'm trying to read video data transferred by HTTP. I use the following function in order
> to read HTTP header:
[sip]


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