[asio] serial_port::async_read_some() and scalable buffers

Hello, I have a std::vector of bytes that I am passing into boost::asio::buffer(). The vector originally is of size 0. Basically, I want the vector to be resized by asio depending on how much data is read in. I don't want to choose an arbitrary upper-limit for the buffer. Is this possible? Below is an example of what I am doing: std::vectorboost::uint8_t stream; m_port.async_read_some( boost::asio::buffer( stream ), boost::bind( &SerialClient::ReadCompleted, this, stream, error, bytes_transferred ) ); However, the above code will never read any data because my vector is 0 and it expects it to have a size. --------- Robert Dailey

-----Original Message----- I have a std::vector of bytes that I am passing into boost::asio::buffer(). The vector originally is of size 0. Basically, I want the vector to be resized by asio depending on how much data is read in. I don't want to choose an arbitrary upper-limit for the buffer. Is this possible?
Below is an example of what I am doing:
std::vectorboost::uint8_t stream; m_port.async_read_some( boost::asio::buffer( stream ), boost::bind( &SerialClient::ReadCompleted, this, stream, error, bytes_transferred ) );
However, the above code will never read any data because my vector is 0 and it expects it to have a size.
You could use a boost::asio::streambuf object with the free function async_read. Something like: async_read(m_port, streambuf, completion_condition, handler); You could probably make a completion condition that always returns true to get notified whenever there is data available. However, I've never tried that. ~Dan

On Thu, Aug 27, 2009 at 2:48 PM, Casimiro, Daniel C CIV NUWC
NWPT
-----Original Message----- I have a std::vector of bytes that I am passing into boost::asio::buffer(). The vector originally is of size 0. Basically, I want the vector to be resized by asio depending on how much data is read in. I don't want to choose an arbitrary upper-limit for the buffer. Is this possible?
Below is an example of what I am doing:
std::vectorboost::uint8_t stream; m_port.async_read_some( boost::asio::buffer( stream ), boost::bind( &SerialClient::ReadCompleted, this, stream, error, bytes_transferred ) );
However, the above code will never read any data because my vector is 0 and it expects it to have a size.
You could use a boost::asio::streambuf object with the free function async_read. Something like:
async_read(m_port, streambuf, completion_condition, handler);
You could probably make a completion condition that always returns true to get notified whenever there is data available. However, I've never tried that.
So, in other words there is no way of doing this without using a completion condition? There's no simple function that will just fill up the read buffer with all of the data it can obtain?

So, in other words there is no way of doing this without using a completion condition? There's no simple function that will just fill up the read buffer with all of the data it can obtain?
No. But you can do as follows: 1) start an async. read with the condition transfer_at_least(1) 2) in the handler check socket.available() and perform synchronous read of this amount of data
participants (3)
-
Casimiro, Daniel C CIV NUWC NWPT
-
Igor R
-
Robert Dailey